Pregunta

I've written code for sending a newsletter, all is working fine, but there is a problem if one of the email addresses in the list, doesn't exit or the domain does not exist.

In this case the script stops immediately and the sending of the mail list is not finished.

Here is the part of the code I want to modify.

public static void SendMessage(String sender, String recipient, String message, String object) 
{
    try
    {
        MailMessage mail = new MailMessage(sender, recipient);
        mail.Subject = object;
        mail.IsBodyHtml = true;
        mail.Body = message;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "my.smtp.com";
        smtp.Send(mail);
    }
    catch (Exception e)
    { throw new Exception("AdminEmail - SendMessage >> recipient: " + recipient + " - generic error: " + e.Message); }
}

Hope somone can help me, thank you very much!

¿Fue útil?

Solución

Welcome to SO. From what I can infer from your description your are not handling the exception that is thrown by SendMessage.

Handle the exception in the caller method. Or do a dirty fix as shown below...

This is not a real fix. But will help you understand the issue...You have to determine in your calling method what to do if SendMessage throws an exception.

public static void SendMessage(String sender, String recipient, String message, String object) 
        {

       try
            {
                MailMessage mail = new MailMessage(sender, recipient);
                mail.Subject = object;
                mail.IsBodyHtml = true;
                mail.Body = message;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "my.smtp.com";
                smtp.Send(mail);
            }
            catch (Exception e)
            { 
                  //Just log error and continue to process

            }   
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top