I'm trying to send an email through MVC. I specifically say MVC because the same code (which is pretty generic to Net.Mail
) works in a windows forms application and not in MVC.
I googled around and tried various settings, with no luck. I think this might be a problem with MVC or some platform I'm using.
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);
All I need is a way of sending mails from MVC
Update: Error I'm getting:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count) at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) at System.Net.TlsStream.CallProcessAuthentication(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result) at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.Mail.SmtpConnection.Flush() at System.Net.Mail.ReadLinesCommand.Send(SmtpConnection conn) at System.Net.Mail.EHelloCommand.Send(SmtpConnection conn, String domain) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message)
[Update] My findings:
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
On Thunder bird I'm using port 587 with STARTTLS.
Any ideas