Usman Nadeem
Usman Nadeem

Reputation: 21

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

Upvotes: 2

Views: 1011

Answers (4)

Christian
Christian

Reputation: 65

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

Upvotes: -1

Matt Evans
Matt Evans

Reputation: 7585

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.

Upvotes: 3

ehh
ehh

Reputation: 3480

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

Upvotes: 0

Hatted Rooster
Hatted Rooster

Reputation: 36483

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

Upvotes: 0

Related Questions