我似乎无法从smtp.gmail.com服务器发送电子邮件。在家尝试了这个并且它有效,但现在它在办公室没有工作。我不知道它是否与我的代码或服务器有关。已禁用防火墙。完全没有线索为什么!
以下代码:
public void sendemail(string toEmail, string Subject, string Body)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress(WebConfigurationManager.AppSettings["SMTPFrom"]);
msg.To.Add(new MailAddress(toEmail));
msg.Subject = Subject;
msg.Body = Body;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("xxx@gmail.com", "xxx999+");
client.EnableSsl = true;
msg.IsBodyHtml = true;
List<string> multipleImage = new List<string>();
multipleImage = FindMultipleImageLocationandFileName(Body);
try
{
client.Send(msg);
}
catch (SmtpException err)
{
System.Web.HttpContext.Current.Response.Write(err.Message);
}
}
在Web.config
下
<system.net>
<mailSettings>
<smtp from="xxx@gmail.com">
<network host="smtp.gmail.com" port="587" userName="xxx@gmail.com" password="xxx999+" defaultCredentials="true" enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>