2

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");
    }
}  
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92

4 Answers4

3

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.

Raj More
  • 47,048
  • 33
  • 131
  • 198
Matt Evans
  • 7,113
  • 7
  • 32
  • 64
0

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;
        }
    }  
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
0

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

Community
  • 1
  • 1
ehh
  • 3,412
  • 7
  • 43
  • 91
-1

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);
Christian
  • 65
  • 1
  • 10
  • It's a bad version of the answers already given. almost the same code but with some flaws like that you can only return false in functions that have the return type boolean. And in your second example it still shows the exception a lot of times instead of once. – online Thomas Oct 28 '15 at 13:47
  • Well, i need information (that i was not given) to be able to predefine the return for the user, i was hopeing he was clever enouth to translate the return into something else fx null or a error string of some sort. Whatever the point is i tried to get him in a direction, By the way editing is better than -1 my post if you feel like there is minor flaws. – Christian Oct 28 '15 at 13:53
  • why my program stuck?? even not going in debugger when I try to send the email... when I comment out sending email then it run fine.... why I'm facing this issue – Usman Nadeem Oct 28 '15 at 15:55
  • This could be due to the fact that if you're sending alot of mails, it takes a long time. Or the Thread.Sleep(50); might be added up to a big number if you're sending 2000+ mails fx.Theres really many reasons why this would stuck, theres also the file attachment.. That could take a little while if it cant find the file. I would need more information to help you more direct on your problem, sorry.. Can you please edit your question so that we will get the intire code? – Christian Oct 29 '15 at 08:43