简体   繁体   中英

How can I ignore sending email method and continue execution?

I'm sending email using 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.

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. 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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM