我正在开发一个ASP .Net网站 我的一个ASPX页面包含一个按钮 单击此按钮后,我想使用Gmail SMTP服务器发送电子邮件 我想将我的Gmail帐户用作凭据。
这是按钮的点击事件处理程序:
protected void m_myButton_Click(object p_sender, EventArgs p_args)
{
string l_subject = "My subject";
StringBuilder l_builder = new StringBuilder();
// ...
string l_body = l_builder.ToString();
string l_host = ConfigurationManager.AppSettings["SmtpHost"];
int l_port = int.Parse(ConfigurationManager.AppSettings["SmtpPort"]);
NameValueCollection l_smtpAccount = (NameValueCollection)ConfigurationManager.GetSection("smtpAccount");
string l_address = l_smtpAccount["SmtpAddress"];
string l_password = l_smtpAccount["SmtpPassword"];
MailMessage l_mail = new MailMessage();
l_mail.From = new MailAddress(l_address);
l_mail.To.Add(l_address);
l_mail.Subject = l_subject;
l_mail.Body = l_body;
SmtpClient l_smtp = new SmtpClient();
l_smtp.Host = l_host; // l_host = "smtp.gmail.com"
l_smtp.Port = l_port; // l_port = 587
l_smtp.EnableSsl = true;
l_smtp.UseDefaultCredentials = false;
l_smtp.Credentials = new NetworkCredential(l_address, l_password);
l_smtp.Send(l_mail);
}
命令行l_smtp.Send(l_mail)失败 引发了一个SMTP异常,并且InnerException属性通知我(从法语翻译成英语):由于目标计算机拒绝连接,因此没有建立任何连接173.194.67.108:587
我的代码基于此页面上的代码:http://www.codeproject.com/Questions/254743/sending-email-in-asp-net-using-smtp-gmail-server
非常感谢任何帮助
答案 0 :(得分:0)
试试这个......
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.To.Add("Email ID where email is to be send");
mail.To.Add("Another Email ID where you wanna send same email");
mail.From = new MailAddress("YourGmailID@gmail.com");
mail.Subject = "Email using Gmail";
string Body = "Hi, this mail is to test sending mail"+
"using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("YourUserName@gmail.com","YourGmailPassword");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
答案 1 :(得分:0)
由于目标计算机已拒绝73.194.67.108:587
尝试其他SMTP端口:465
答案 2 :(得分:0)
我按照Rup的建议关闭了我的防病毒程序,我没有任何例外 我将在我的防病毒程序中创建一个自定义规则,以允许我的ASP .Net网站和Gmail SMTP服务器之间的通信。