【发布时间】:2016-03-17 21:38:01
【问题描述】:
这似乎是重复的,但事实并非如此。我尝试重新启动 IIS,将 SMTPClient 包含在 using 块等中。但我仍然收到错误消息。
try
{
//I have replaced sender@gmail.com and receiver@gmail.com by a valid gmail addresses
MailAddress sender = new MailAddress("sender@gmail.com");
MailAddress receiver = new MailAddress("receiver@gmail.com");
MailMessage mailMsg = new MailMessage(sender, receiver);
mailMsg.Subject = "subject";
string text = "text body";
string html = @"<p>html body</p>";
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));
using(SmtpClient smtpClient = new SmtpClient())
{
smtpClient.Host = "smtp.sendgrid.net";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
//UserName = Username of SendGrid account, Password = Password of SendGrid account
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("UserName", "password");
smtpClient.Credentials = credentials;
smtpClient.ServicePoint.MaxIdleTime = 1; /* without this the connection is idle too long and not terminated, times out at the server and gives sequencing errors */
smtpClient.Send(mailMsg);
}
}
catch (SmtpException ex)
{
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
这是一个用VS 2015开发的MVC应用。
对此的任何意见都会有所帮助。
【问题讨论】:
标签: smtp sendgrid smtpclient