1
这似乎是重复的,但事实并非如此。我尝试重新启动IIS,在使用块中封入SMTPClient等,但仍然出现错误。SendGrid SMTP错误{“无法从传输连接读取数据:net_io_connectionclosed。”}
try
{
//I have replaced [email protected] and [email protected] by a valid gmail addresses
MailAddress sender = new MailAddress("[email protected]");
MailAddress receiver = new MailAddress("[email protected]");
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);
}
这是在2015年VS
任何投入开发上,这将是有帮助的一个MVC应用程序。