我无法发送电子邮件请给出解决方案 [英] I can not send email please give solution
问题描述
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Configuration;
public partial class contact : System.Web.UI.Page
{
protected void SendEmail()
{
try
{
MailMessage message = new MailMessage();
MailAddress Sender = new MailAddress(ConfigurationManager.AppSettings["smtpUser"], "Inquiry");
MailAddress receiver = new MailAddress("example@gmail.com", "Inquiry");
SmtpClient smtp = new SmtpClient()
{
Host = ConfigurationManager.AppSettings["smtpServer"],
Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]),
EnableSsl = true,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPass"])
};
message.From = Sender;
message.To.Add(receiver);
message.Subject = "New Inquiry @ " + txtname.Text;
string body = "From: " + txtname.Text + " <br />";
body += "Mobile Phone: " + txtmobile.Text + " <br />";
body += "Message: " + txtmsg.Text + " <br />";
message.Body = body;
message.IsBodyHtml = true;
smtp.Send(message);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SendEmail();
ScriptManager.RegisterStartupScript(this, GetType(), "alertmsg", "alert('Thank you for Inquiry!Our team contact you soon');", true);
}
}
我的尝试:
有什么问题呢
邮件不发送
请帮忙解决这个问题
What I have tried:
what is the problem in it
mail does not send
please help to solve this problem
推荐答案
尝试下面的代码片段,在这里我使用Gmail帐户发送邮件
我的代码始终是我TRY..CATCH块以获得确切的错误
try below snippet, here I am sending mail using Gmail account
put your code always I TRY..CATCH block to get exact error
MailMessage mail = new MailMessage();
//put mail from Email
mail.From = new MailAddress("MailFrom@gmail.com");
//put mail to Email
mail.To.Add(new MailAddress("MailTo@gmail.com"));
//you can add blank carbon copy mails here
//mail.Bcc.Add(new MailAddress("YOUR BCC EMAIL"));
//Get an attachment in attachment variable class
Attachment att = new Attachment("D:\\test.txt");
//add attachment to email
mail.Attachments.Add(att);
//Put email subject
mail.Subject = "Send Email with Gmail using ASP.NET";
//Put email body
string Body = "Put your Email Body TEXT here";
// Here you can put HTML string if you want email to be sent in HTML format.
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "PASSWORD");
//keep ssl true if you have to send email with secured socket layer
smtp.EnableSsl = false;
smtp.Send(mail);
例外点
有这么多因素可能会影响你的邮件发送代码,这里是列表,
1.防火墙阻塞端口587(这是smtp gmail服务器的端口)
2.互联网连接在机器上没有可用或互联网连接在ping时遇到一些障碍
3.由于一些拼写错误用户名和密码可能出错
4.启用SSL是阻止电子邮件发送
5.您的Gmail帐户中smtp服务器的POP设置已停用
Exception points
There are so many factor that may affects your email sending code, here is list,
1. Firewall block port 587 (This is port for smtp gmail server)
2. Internet connection is not available on machine or internet connection has face some obstacles while pinging
3. Due to some typo mistake UserName and password may be get wrong
4. Enable SSL is block email sending
5. POP settings for smtp server in your Gmail account is disabled
这篇关于我无法发送电子邮件请给出解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!