【问题标题】:SmtpClient - The operation has timed outSmtpClient - 操作已超时
【发布时间】:2021-01-20 17:03:18
【问题描述】:

我正在尝试使用 C# 创建一个小型应用程序来发送电子邮件,但即使使用端口 587 (GMail) TLS 或 465 (GMail SSL),我也无法让它工作。我不确定是什么导致了我的问题,我应该编辑我的 app.config 文件吗?这是我的代码:

try
{
  SmtpClient client = new SmtpClient("smtp.gmail.com", 465);
  client.EnableSsl = true;
  client.Timeout = 10000;
  client.DeliveryMethod = SmtpDeliveryMethod.Network;
  client.UseDefaultCredentials = false;
  client.Credentials = new NetworkCredential("myemailid", "mypwd");

  MailMessage msg = new MailMessage();
  msg.To.Add(txtTo.Text);
  msg.From = new MailAddress("myemailid");
  msg.Subject = txtSub.Text;
  msg.Body = txtMsg.Text;

  client.Send(msg);
  MessageBox.Show("sent");
}
catch (Exception ex) 
{
    MessageBox.Show(ex.Message);
}

【问题讨论】:

  • 您收到什么错误?你能发布例外吗?
  • 操作超时
  • 可能原因:SmtpClient无效,端口无效,用户名密码组合无效,client.Timeout变小,无网络连接,谷歌宕机(我不认为谷歌宕机) )
  • 您检查邮箱地址和登录名是否正确
  • 是的,我检查了一下还是不行

标签: c# smtp


【解决方案1】:

端口 25 和 465 引起了问题。将端口更改为 587 对我有用。

Dim myCredential As System.Net.NetworkCredential
myCredential = New System.Net.NetworkCredential("emailid", "pwd")
smtp.Host = "server"
smtp.Port = 587
smtp.EnableSsl = True
smtp.UseDefaultCredentials = False
smtp.Credentials = myCredential
smtp.Send(mm)

【讨论】:

    【解决方案2】:

    如果您收到超时异常,则表示 SmtpClient 无法连接到 SMTP 服务器或在发送过程中的某个时间点连接丢失。

    这可能是由于您端的防火墙阻止了与服务器的传出连接或阻止了端口 465 上的传出连接。

    【讨论】:

    • 我花了一天半的时间调试一个类似的问题,却发现是 Symantec Endpoint Protection 阻止了出站邮件。奇怪的是,我的单元测试有效,但是当作为 Windows 服务运行时,我会收到此错误。
    • 我从 Google 帮助页面获得了帮助,该页面建议使用端口 25 和 SSL,这对我有用。
    • 我目前遇到了这个问题。让我烦恼的是,它是断断续续的
    • 首先我使用了端口号 465,它抛出了超时错误,最后我使用了 25,现在它对我来说工作正常。但我找不到它发生的原因。
    【解决方案3】:

    我遇到了同样的问题,以管理员身份运行它并为我排序

    【讨论】:

      【解决方案4】:
      1. 在防火墙设置中允许端口 25、465、587。

      2. 我可以通过 System.Net.Mail.SmtpClient 或 MailKit.Net.Smtp.SmtpClient 发送邮件

        aruba.it等一些邮件服务中,System.Net.Mail.SmtpClient根本不起作用。

      以下是使用 MailKit.Net.Smtp.SmtpClient 的代码部分

      using MailKit.Security;
      using MimeKit;
      using MailKit.Net.Smtp;
      using MimeKit.Text;
      using System;
      using System.Net.Mail;
      ...
      // create email message
      var email = new MimeMessage();
      email.From.Add(MailboxAddress.Parse("notifiche@articolo75.it"));
      //email.From.Add(MailboxAddress.Parse("yourmail@gmail.com"));// if you want to use gmail service, enable this line.
      email.To.Add(MailboxAddress.Parse(destinatario));
      email.Subject = oggetto;
      email.Body = new TextPart(TextFormat.Html) { Text = "<h1>Example HTML Message Body</h1>" };
      if (password.Length > 0)
      {
          msg += "Password : " + password;
      }
      
      if (link.Length > 0)
          email.Body = new TextPart(TextFormat.Html) { Text = messaggio + "<a href=\"" + link + "\"" + ">Clicca qui</a>" + msg }; 
      else
          email.Body = new TextPart(TextFormat.Html) { Text = messaggio }; 
      
      // send email
      var smtp = new MailKit.Net.Smtp.SmtpClient();
      smtp.Connect("smtps.aruba.it", 465, SecureSocketOptions.SslOnConnect); 
      smtp.Authenticate("notifiche@articolo75.it", "password");
      //smtp.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);// if you want to use gmail service, enable this line.
      //smtp.Authenticate("yourmail@gmail.com", "password");// if you want to use gmail service, enable this line.
      smtp.Send(email);
      smtp.Disconnect(true);
      

      以下是使用 System.Net.Mail.SmtpClient 的代码部分

      using System;
      using System.Net.Mail;
      ...
      try
      {
          MailMessage mail = new MailMessage();
          mail.IsBodyHtml = true;
          mail.From = new MailAddress(strFromMail);
          mail.To.Add(new MailAddress(strToMail));
          mail.Subject = oggetto;
          mail.Body = messaggio;
      
          System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.gmail.com", 25/* or 587 */);
          System.Net.NetworkCredential basicCredential1 = new System.Net.NetworkCredential(strFromMail, "password");
          client.EnableSsl = true;
          client.UseDefaultCredentials = false;
          client.Credentials = basicCredential1;
          
          client.Send(mail);
      }
      catch (Exception ex)
      {
          MessageBox.Show("Error sending email \n"+ex.ToString());
      }
      

      【讨论】:

        猜你喜欢
        • 2012-10-21
        • 1970-01-01
        • 2012-05-15
        • 1970-01-01
        • 2012-09-21
        • 2011-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多