【问题标题】:How can I ignore sending email method and continue execution?如何忽略发送电子邮件方法并继续执行?
【发布时间】:2015-10-29 16:41:04
【问题描述】:

我正在使用 C# 发送电子邮件。当互联网可用时它工作正常,但它一次又一次地返回异常消息并在互联网连接不可用时停止执行。如果连接不可用,我如何忽略发送电子邮件并继续执行, 或任何其他合适的方法来做到这一点..

while (true)
{
    try
    {
        Thread.Sleep(50);
        MailMessage mail = new MailMessage();
        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
        mail.From = new MailAddress("mymail");
        mail.To.Add("mymail");
        mail.Subject = "data - 1";
        mail.Body = "find attachement";
        System.Net.Mail.Attachment attachment;
        attachment = new System.Net.Mail.Attachment(filepath);
        mail.Attachments.Add(attachment);
        SmtpServer.Port = 587;
        SmtpServer.Credentials = new System.Net.NetworkCredential("myemail", "mypassword");
        SmtpServer.EnableSsl = true;
        SmtpServer.Send(mail);
    }
    catch (Exception)
    {
        MessageBox.Show("Internet Connection is not found");
    }
}  

【问题讨论】:

    标签: c# .net email


    【解决方案1】:

    任何依赖于重复尝试的解决方案都可能最终无休止地循环。

    您的代码正在同步发送电子邮件,为什么不使用pickup directory send asynchronously

    这会将电子邮件放入 SMTP 提取目录,并且 SMTP 服务器将通过重试一段可配置的时间来处理暂时的网络问题。

    【讨论】:

      【解决方案2】:

      在没有互联网的情况下跳出循环:

      while (true)
          {
              try
              {
                  Thread.Sleep(50);
                  MailMessage mail = new MailMessage();
                  SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                  mail.From = new MailAddress("mymail");
                  mail.To.Add("mymail");
                  mail.Subject = "data - 1";
                  mail.Body = "find attachement";
                  System.Net.Mail.Attachment attachment;
                  attachment = new System.Net.Mail.Attachment(filepath);
                  mail.Attachments.Add(attachment);
                  SmtpServer.Port = 587;
                  SmtpServer.Credentials = new System.Net.NetworkCredential("myemail", "mypassword");
                  SmtpServer.EnableSsl = true;
                  SmtpServer.Send(mail);
              }
              catch (Exception)
              {
                  MessageBox.Show("Internet Connection is not found");
                  break;
              }
          }  
      

      【讨论】:

      • 如果Port 错误、SSL 不可用或NetworkCredentials 错误会怎样?
      【解决方案3】:

      最好有一个 testconnection 方法,如果它返回 true 来发送电子邮件。比如:

      while(true)
      { 
         if(TestConnection())
         {            
              SendEmail();    // you should leave the try...catch.. anyway in case 
                              // the connection failed between the TestConnection     
                              // and the SendEmail() operation. But then do not 
                              // prompt a messagebox, just swallow
              Thread.Sleep(50);       
         }
      }
      

      现在TestConnection的实现,你可以尝试从以下链接获取帮助:

      enter link description here

      【讨论】:

        【解决方案4】:

        试试这个:

        while (true)
            {
                try
                {
                    Thread.Sleep(50);
                    MailMessage mail = new MailMessage();
                    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                    mail.From = new MailAddress("mymail");
                    mail.To.Add("mymail");
                    mail.Subject = "data - 1";
                    mail.Body = "find attachement";
                    System.Net.Mail.Attachment attachment;
                    attachment = new System.Net.Mail.Attachment(filepath);
                    mail.Attachments.Add(attachment);
                    SmtpServer.Port = 587;
                    SmtpServer.Credentials = new System.Net.NetworkCredential("myemail", "mypassword");
                    SmtpServer.EnableSsl = true;
                    SmtpServer.Send(mail);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error " + ex.InnerException);
                    return false;
                }
            }  
        

        或者您可以将 bool 设置为 true,然后检查 bool 是否为 true 或 false 外汇:

        string noInterNet = "";
        while (true)
                {
                    try
                    {
                        Thread.Sleep(50);
                        MailMessage mail = new MailMessage();
                        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                        mail.From = new MailAddress("mymail");
                        mail.To.Add("mymail");
                        mail.Subject = "data - 1";
                        mail.Body = "find attachement";
                        System.Net.Mail.Attachment attachment;
                        attachment = new System.Net.Mail.Attachment(filepath);
                        mail.Attachments.Add(attachment);
                        SmtpServer.Port = 587;
                        SmtpServer.Credentials = new System.Net.NetworkCredential("myemail", "mypassword");
                        SmtpServer.EnableSsl = true;
                        SmtpServer.Send(mail);
                    }
                    catch (Exception ex)
                    {
                        noInterNet = ex.InnerException;
                    }
                }  
        

        然后在代码的最后做:

        if(noInterNet != "")
        MessageBox.Show("Error " + noInterNet);
        

        【讨论】:

        • 这是已经给出的答案的错误版本。几乎相同的代码,但有一些缺陷,你只能在返回类型为布尔的函数中返回 false。在您的第二个示例中,它仍然多次显示异常而不是一次。
        • 嗯,我需要信息(我没有得到)能够为用户预定义回报,我希望他能聪明地将回报翻译成其他 fx null 或错误某种字符串。不管是什么意思,我都试图让他找到一个方向,顺便说一句,如果你觉得我的帖子有小瑕疵,编辑比我的帖子 -1 好。
        • 为什么我的程序卡住了??当我尝试发送电子邮件时甚至没有进入调试器......当我注释掉发送电子邮件时它运行正常......为什么我面临这个问题
        • 这可能是因为如果您要发送大量邮件,则需要很长时间。或 Thread.Sleep(50);如果您要发送 2000 多封邮件 fx,可能会增加一个很大的数字。这会卡住的原因有很多,还有文件附件。如果找不到文件,这可能需要一段时间。我需要更多信息来帮助您更直接地解决您的问题,抱歉。您能否编辑您的问题以便我们获得内部代码?
        猜你喜欢
        • 1970-01-01
        • 2019-04-10
        • 2016-09-27
        • 2021-01-12
        • 1970-01-01
        • 1970-01-01
        • 2017-01-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多