无法从MVC发送邮件

时间:2019-08-02 08:19:10

标签: c# asp.net-mvc email system.net.mail

我正在尝试通过MVC发送电子邮件。我之所以专门说MVC,是因为相同的代码(对于Net.Mail来说是相当通用的)可以在Windows窗体应用程序中工作,而不能在MVC中工作。

我四处搜寻,尝试了各种设置,但没有运气。我认为这可能与MVC或我正在使用的某些平台有关。

SmtpClient smtp = new SmtpClient();
smtp.Port = Convert.ToInt16(ConfigurationManager.AppSettings["mailPort"]);
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["mailAccount"], ConfigurationManager.AppSettings["mailPassword"]);
smtp.Host = ConfigurationManager.AppSettings["mailHost"];
smtp.Timeout = (20 * 1000);

MailMessage mail = new MailMessage();
mail.IsBodyHtml = true;
mail.From = new System.Net.Mail.MailAddress(ConfigurationManager.AppSettings["mailAccount"]);
mail.To.Add(new MailAddress(item.Recipient));
mail.Subject = item.Subject;
mail.Body = item.Body;

smtp.Send(mail);

我所需要的只是一种从MVC发送邮件的方式

更新: 我收到错误消息:

  

System.Net.Mail.SmtpException:发送邮件失败。 ->   System.IO.IOException:无法从传输读取数据   连接:远程强行关闭了现有连接   主办。 ---> System.Net.Sockets.SocketException:现有连接   被远程主机强行关闭   System.Net.Sockets.Socket.Receive(Byte []缓冲区,Int32偏移量,Int32   大小,SocketFlags socketFlags)在   System.Net.Sockets.NetworkStream.Read(Byte []缓冲区,Int32偏移量,   Int32大小)-内部异常堆栈跟踪的结尾-在   System.Net.Sockets.NetworkStream.Read(Byte []缓冲区,Int32偏移量,   Int32大小),位于System.Net.FixedSizeReader.ReadPacket(Byte []缓冲区,   Int32偏移量,Int32计数)在   System.Net.Security.SslState.StartReceiveBlob(Byte []缓冲区,   AsyncProtocolRequest asyncRequest),网址为   System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken   消息,AsyncProtocolRequest asyncRequest)   System.Net.Security.SslState.StartSendBlob(Byte []传入,Int32   计数,AsyncProtocolRequest asyncRequest)   System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst,   Byte []缓冲区,位于AsyncProtocolRequest asyncRequest)   System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult   位于System.Net.TlsStream.CallProcessAuthentication(Object的lazyResult)   州)   System.Threading.ExecutionContext.RunInternal(ExecutionContext   executionContext,ContextCallback回调,对象状态,布尔值   reserveSyncCtx)位于   System.Threading.ExecutionContext.Run(ExecutionContext   executionContext,ContextCallback回调,对象状态,布尔值   reserveSyncCtx)位于   System.Threading.ExecutionContext.Run(ExecutionContext   executeContext,ContextCallback回调,对象状态),位于   System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult结果)位于   System.Net.TlsStream.Write(Byte []缓冲区,Int32偏移量,Int32大小)位于   System.Net.PooledStream.Write(Byte []缓冲区,Int32偏移量,Int32大小)   在System.Net.Mail.SmtpConnection.Flush()在   System.Net.Mail.ReadLinesCommand.Send(SmtpConnection conn)在   System.Net.Mail.EHelloCommand.Send(SmtpConnection conn,字符串域)   在System.Net.Mail.SmtpConnection.GetConnection(ServicePoint   servicePoint)在   System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)   在System.Net.Mail.SmtpClient.GetConnection()在   System.Net.Mail.SmtpClient.Send(MailMessage消息)-内部结束   异常堆栈跟踪---   System.Net.Mail.SmtpClient.Send(MailMessage消息)

[更新] 我的发现:

Platform    Port    SSL Result
MVC         587 true    Error
MVC         587 false   got mail
MVC         465 true    Time out
MVC         465 false   Time out

WinForm     587 true    got mail
WinForm     587 false   got mail
WinForm     465 true    Time out
WinForm     465 false   Time out

在雷鸟上,我将端口587与STARTTLS一起使用。

任何想法

0 个答案:

没有答案