0

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

phuzi
  • 12,078
  • 3
  • 26
  • 50
F34R
  • 23
  • 5
  • "Can't send mail from MVC" does not describe the problem. It's very unlikely to be failing silently, so either you're catching and throwing away the error, the code is never being run, or the SMTP server isn't sending the e-mail onwards for some reason. – ProgrammingLlama Aug 02 '19 at 08:20
  • Are you getting any errors? you have not described the exact nature of problem you are experiencing? – KJSR Aug 02 '19 at 08:23
  • Apologies, updated accordingly. – F34R Aug 02 '19 at 08:24
  • [this answer](https://stackoverflow.com/a/5420788/5947043) is a good general description of the possible causes of your error message (I just googled the error, it's easy to find). So the first things I'd do would be to check that the mailserver is definitely running, check you're using the right port, and check there are no firewalls blocking your route. – ADyson Aug 02 '19 at 08:25
  • If you're using Google, also see [this answer](https://stackoverflow.com/questions/26250322/sending-email-asp-net-c-existing-connection-was-forcibly-closed-by-the-remot) – ProgrammingLlama Aug 02 '19 at 08:26
  • @ADyson I'll go through it, but it's not the server. As mentioned the exact same code works in a windows forms application with no issue at all. – F34R Aug 02 '19 at 08:28
  • @John Not google. It's my own domain. – F34R Aug 02 '19 at 08:28
  • And do `mailHost` and `mailPort` have the _exact_ same values as the WinForms app? – ProgrammingLlama Aug 02 '19 at 08:29
  • @John Exactly! That's the curve ball I'm getting. Perhaps ConfigurationManager.AppSettings[""] changes the values slightly? – F34R Aug 02 '19 at 08:32
  • Are you running this code on the same machine as the windows forms app? If not, then look for differences e.g. firewalls, available network routes, rules on the mailserver about allowed IPs, that sort of thing. – ADyson Aug 02 '19 at 08:34
  • ConfigurationManager.AppSettings[""] will not alter any values, no, they are just strings. MVC uses it the exact same way as a Forms app would – ADyson Aug 02 '19 at 08:39
  • 1
    Try sending the mail using gmail, if it works, you will know that the code works and there is something related to your domain settings. – drinu16 Aug 02 '19 at 08:40
  • Can you `smtp.EnableSsl = false;` to rule out SSL/TLS issues. – phuzi Aug 02 '19 at 09:08
  • @Adyson Same machine in my dev environment – F34R Aug 02 '19 at 09:24
  • have you checked the the winforms app is currently working and sending mail from your dev machine? If the code is genuinely identical, the settings are genuinely identical, the machine is identical, but the socket connection cannot be made then it _sounds_ like an environment problem, possibly external to your machine (e.g. temporary outage or something). The only other local issue I could thing of might be windows firewall on the machine - does it have rules about which specific applications can make outbound connections, perhaps? – ADyson Aug 02 '19 at 09:29
  • @phuzi Thank you for the suggestion. I actually rolled my eyes when I saw it but did it out of frustration and it worked. I suppose it's a solution, but I want SSL. Perhaps someone can help making sense of this? More to follow, just give me a sec while I investigate. – F34R Aug 02 '19 at 09:30
  • Hmm, are you using SSL in the WinForms version? Obviously the mailserver needs to support SSL connections. – ADyson Aug 02 '19 at 09:32
  • 1
    Could you confirm what version of .Net/ASP.Net you are using (Core/Framework with version number). There are some differences in support out of the box with SSL/TLS versions. – phuzi Aug 02 '19 at 09:39

0 Answers0