2

I'm developping an ASP.NET core 2.0 web app, in which I want to send emails. But when I run my method (That I Saw on different tutorials on the web), it times out:

An unhandled exception occurred while processing the request. SmtpException: The operation has timed out.

System.Net.Mail.SmtpClient.Send(MailMessage message)

    SmtpException: The operation has timed out.

    System.Net.Mail.SmtpClient.Send(MailMessage message)
    WebApplication6.Pages.IndexModel.MailSender() in Index.cshtml.cs

                client.Send(mailMessage);

WebApplication6.Pages.IndexModel.OnPost() in Index.cshtml.cs

                        MailSender();

Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory+VoidHandlerMethod.Execute(object receiver, Object[] arguments)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker+<InvokeHandlerMethodAsync>d__29.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker+<InvokeNextPageFilterAsync>d__31.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker+<InvokeInnerFilterAsync>d__22.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeNextResourceFilter>d__22.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeFilterPipelineAsync>d__17.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+<InvokeAsync>d__15.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Builder.RouterMiddleware+<Invoke>d__4.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+<Invoke>d__7.MoveNext()

And here is my C#:

public void MailSender()
{
    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential("myaccount@gmail.com ", "password");
    client.EnableSsl = true;
    client.Timeout = 3000;

    MailMessage mailMessage = new MailMessage();
    mailMessage.From = new MailAddress("myaccount@gmail.com");
    mailMessage.To.Add("address@whatever.com");
    mailMessage.Body = "test";
    mailMessage.Subject = "subject";
    client.Send(mailMessage);
}

I can't find out what is wrong

Thanks for your help !

EDIT: Console test crash

Unhandled Exception: System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
   at System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP)
   at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
   at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
   at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port)
   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)
   at ConsoleApp2.Program.Main(String[] args)

EDIT [Solved] : Had to set a deliveryMethod, like this:

client.DeliveryMethod = SmtpDeliveryMethod.Network;

Full code, if it can help anyone:

static void MailSender()
        {
            string address = "example@gmail.com";
            string password = "gmailpassword";
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(address, password);
            client.EnableSsl = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(address);
            mailMessage.To.Add(anyone@anything.com);
            mailMessage.Body = "test";
            mailMessage.Subject = "subject";
            client.Send(mailMessage);
        }
Swurderer
  • 159
  • 1
  • 2
  • 11
  • Please check this https://stackoverflow.com/questions/32231489/smtp-exception-failure-sending-mail – Vikas Lalkiya Oct 11 '17 at 13:30
  • @VikasLalkiya: The OP is already doing that. – JuanR Oct 11 '17 at 13:33
  • You set your timeout to 3 seconds. Is it conceivable to you that it might take longer than 3 seconds to do the DNS lookup for `smtp.gmail.com`, connect to it, and send the email? You should probably think harder about what a reasonable timeout should be. You want it to be long enough that emails go through in normal circumstances, but not so long that if the service is unreachable it ties up your code for an extraordinary amount of time. This comment holds true even if it turns out that something like the firewall is blocking the connection. – mason Oct 11 '17 at 13:33
  • It looks like the client cannot reach the server. Firewall perhaps? – JuanR Oct 11 '17 at 13:33
  • @mason that was indeed dumb of me, but this didn't cause an internal server error – Swurderer Oct 11 '17 at 13:38
  • @Juan Thanks, but I already disabled it – Swurderer Oct 11 '17 at 13:40
  • @Swurderer You disabled it on the server? Are you also sitting behind a corporate firewall? – mason Oct 11 '17 at 13:44
  • Just for giggles, what happens if you create a new .NET Framework console app (assuming you're on Windows) and use the code from [this answer](https://stackoverflow.com/a/32336/1139830)? I'm curious if perhaps there's a difference between .NET Core and .NET Framework here. – mason Oct 11 '17 at 13:47
  • @mason I'm indeed behind a corporate firewall, i'm gonna try it at home tonight. I'm currently doing what you just asked – Swurderer Oct 11 '17 at 13:49
  • The firewall needs to allow outgoing connections from your application on the machine where you are running the code. The timeout suggests improper or non-existent communication between client and server. – JuanR Oct 11 '17 at 13:51
  • @mason The console crashed with the error in the EDIT – Swurderer Oct 11 '17 at 13:54
  • @Juan Well, I'm gonna update tonight after a few testing at home, thanks – Swurderer Oct 11 '17 at 14:01
  • The console crashing with the same error rules out the technology. – JuanR Oct 11 '17 at 14:03
  • Just tried it at home, got the exact same error, and there is no firewall – Swurderer Oct 11 '17 at 15:27
  • I don't know if this is related (probably not), but you have an extra space in the username of your credentials. – mason Oct 12 '17 at 03:39
  • Solved, I had to set the deliveryMethod, and my corporate firewall was messing it up. – Swurderer Oct 12 '17 at 08:30

0 Answers0