【问题标题】:smtp.gmail.com exceptions, enable to send emailsmtp.gmail.com 例外,启用发送电子邮件
【发布时间】:2019-01-21 03:07:39
【问题描述】:

我在使用 gmail smtp 遇到异常时遇到了问题。

1) 允许安全性较低的应用:开启

2) 尝试了端口 587、465、25(相同的例外)

3) 尝试禁用 SSL(同样的例外)

4) 用于 smtp.gmail.com 的 telnet 端口 465、587 工作以及从用于 smtp 的电子邮件地址直接发送

想法?

public async Task SendEmail(string subject, string message)
{
    MailAddress fromAddress = new MailAddress("user_from@gmail.com");
    MailAddress toAddress = new MailAddress("user_to@gmail.com");

    MailMessage mail = new MailMessage(fromAddress.Address, toAddress.Address);
    mail.Subject = subject;
    mail.Body = message;

    SmtpClient client = new SmtpClient();
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.EnableSsl = true;
    client.Timeout = 20000;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential("user_from@gmail.com", "password");

    try
    {
        client.Send(mail);
    } 
    catch (Exception ex)
    {
        Console.Write(ex.Message);
    }
}

使用端口 587 的异常:

{System.Net.Mail.SmtpException: The operation has timed out.
    at System.Net.Mail.SmtpClient.Send(MailMessage message)
    at EmailsUtilities.<SendEmail>d__1.MoveNext() in client.Send(mail);

使用端口 465 的异常:

{System.Net.Mail.SmtpException: Failure sending mail. ---> 
System.IO.IOException: Unable to read data from the transport connection: The 
connection was closed.
   at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 
offset, Int32 read, Boolean readLine)
    at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader 
caller, Boolean oneLine)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
   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 EmailsUtilities.<SendEmail>d__1.MoveNext() in client.Send(mail);

更新: 将client.Send(mail) 更改为await client.SendMailAsync(mail) 后获取新异常:

{System.Net.Mail.SmtpException: Syntax error, command 
unrecognized. The server response was: 
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at 
System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult 
 result)
   at System.Net.Mail.SmtpTransport.EndGetConnection(IAsyncResult result)
   at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at 
System.Runtime.CompilerServices.TaskAwaiter
    .HandleNonSuccessAndDebuggerNotificati 
   on(Task task)
       at EmailsUtilities.<SendEmail>d__1.MoveNext() in client.SendMailAsync 
    System.Net.Mail.SmtpException

感谢您的帮助!

【问题讨论】:

  • 为了测试,我建议在MailMessage 中使用IsBodyHtml = false,并尝试启用/禁用 SSL。 SmtpClient 也有一个可等待的 SendMailAsync。当您在异步任务中时,我还建议您使用并等待此方法。关于端口 587 上的超时问题,至少检查一下您的防火墙可能值得一试。虽然我怀疑您是否可以通过此端口上的 telnet 成功连接。
  • IsBodyHtml = false 没有改变任何东西,但SendMailAsync 产生了一些变化。对于 587 端口没有任何异常,只是运行轮子没有任何响应。对于端口 465 获取新异常(固定到主题)
  • 电子邮件服务器(在本例中为 GMAIL)确定必须使用哪些选项和端口才能使 SMTP 正常工作。此外,发件人电子邮件帐户和发送电子邮件的凭据必须使用相同的电子邮件帐户。请参阅以下代码项目:codeproject.com/Tips/520998/…
  • SSL 端口 465,TLS - 587。两者都试过了。 From 和 Credentials 相同。尝试using 不会产生任何变化。

标签: c# .net email smtp gmail


【解决方案1】:

最终使用 Mail.dll(通过 NuGet 安装)让电子邮件正常工作。示例:

IMail email = Mail
    .Html(@"Html with an image: <img src=""cid:lena"" />")
    .AddVisual(@"c:\lena.jpeg").SetContentId("lena")
    .AddAttachment(@"c:\tmp.doc").SetFileName("document.doc")
    .To("to@example.com")
    .From("from@example.com")
    .Subject("Subject")
    .Create();

using(Smtp smtp = new Smtp())
{
    smtp.Connect("smtp.server.com");  // or ConnectSSL for SSL
    smtp.UseBestLogin("user", "password");
    smtp.SendMessage(email);                     
    smtp.Close();   
}         

【讨论】:

    猜你喜欢
    • 2019-03-01
    • 2023-03-25
    • 1970-01-01
    • 2013-11-13
    • 2014-10-18
    • 2018-07-14
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多