简体   繁体   English

如何忽略发送电子邮件方法并继续执行?

[英]How can I ignore sending email method and continue execution?

I'm sending email using C#.我正在使用 C# 发送电子邮件。 It's working fine when internet is available, but it is returning exception message again and again and stop execution when internet connection is not available.当互联网可用时它工作正常,但它一次又一次地返回异常消息并在互联网连接不可用时停止执行。 How do I ignore sending email and continue execution if connection is not available, or any other suitable method to do that..如果连接不可用,我如何忽略发送电子邮件并继续执行,或任何其他合适的方法来做到这一点..

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

Any solution which depends on repeated attempts may end up looping endlessly.任何依赖于重复尝试的解决方案最终可能会无休止地循环。

Your code is sending the email synchronously, why not send asynchronously using the pickup directory您的代码正在同步发送电子邮件,为什么不使用取件目录异步发送

This will drop the email into the SMTP pickup directory, and the SMTP server will handle transient network issues, by retrying for a configurable period of time.这会将电子邮件放入 SMTP 接收目录,并且 SMTP 服务器将通过在可配置的时间段内重试来处理暂时性网络问题。

Just break out of the loop when there's no internet :没有互联网时就跳出循环:

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

It will be better to have a testconnection method and if it returns true to send the email.最好有一个 testconnection 方法,如果它返回 true 发送电子邮件。 Something like:就像是:

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

Now the TestConnection implementation, you can try to get help from the following link:现在 TestConnection 实现,您可以尝试从以下链接获取帮助:

enter link description here 在此处输入链接描述

Try this:尝试这个:

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

Or you can set a bool to true and then check in the end if the bool is true of false FX:或者您可以将 bool 设置为 true,然后最后检查 bool 是否为 false FX:

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

And then in the end of the code do:然后在代码的最后做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何告诉我的方法忽略异常并继续运行? - How can I tell my method to ignore an exception and continue running? 返回数据后如何继续执行方法? - How to continue execution of method after returning data? 如何将当前执行状态推送到堆栈中以便以后可以继续执行? - How can I push the current execution state into a stack so that I can continue from it later? 如何忽略自定义属性中方法的执行? - How to ignore execution of method from custom attribute? 如何等待异步方法完成然后继续执行指令? - How can i wait an async method to finish and then continue executing an instruction? 如何在 .ForEach() 方法中使用 continue 语句 - How can I use continue statement in .ForEach() method 如何在扩展方法中使用'continue'和`break`? - How can I use 'continue' and `break` in an extension method? 如何拦截,跳过方法执行并继续执行其余的堆栈 - How to Intercept, skip method execution and continue with the rest of the stack 使用SmtpClient时如何保存电子邮件而不是发送? - How can I save an email instead of sending when using SmtpClient? 如何告诉编译器忽略堆栈跟踪中的方法? - How can I tell the compiler to ignore a method in stack traces?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM