通过Asp.net从localhost发送来自gmail的电子邮件

时间:2015-06-15 17:39:20

标签: asp.net

有时下面的代码会发送电子邮件,有时候我会收到SMTP Exception,说“发送电子邮件失败”。为什么会发生这种情况?以下是我写的代码.Plesae告诉我为什么有时我会收到电子邮件,有时我会收到Exception。

 StringBuilder mailBody = new StringBuilder();
 mailBody.Append("Hi <br/>")
 MailMessage message = new MailMessage("from@gmail.com", "to@gmail.com");
 message.Subject = "Subject";
 message.Body = mailBody.ToString();
 SmtpClient smtpclient = new SmtpClient("smtp.gmail.com", 587);
smtpclient.Credentials = new System.Net.NetworkCredential()
{ UserName = "from@gmail.com",
  Password = "password"
 };
 smtpclient.EnableSsl = true;
  message.IsBodyHtml = true;
  smtpclient.Send(message);

0 个答案:

没有答案