SmtpException:发送邮件失败

时间:2017-10-11 13:26:38

标签: c# asp.net email smtp asp.net-core-2.0

我正在开发一个ASP.NET核心2.0 Web应用程序,我想在其中发送电子邮件。但是当我运行我的方法(我在Web上看到不同的教程)时,它会超时:

  

处理请求时发生未处理的异常。   SmtpException:操作已超时。

     

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()

这是我的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);
}

我找不到有什么问题

感谢您的帮助!

编辑:控制台测试崩溃

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)

编辑[已解决]: 不得不设置deliveryMethod,如下所示:

client.DeliveryMethod = SmtpDeliveryMethod.Network;

完整代码,如果它可以帮助任何人:

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);
        }

0 个答案:

没有答案