System.Net.Mail -- how to continue sending to emails if one of the emails fails with a 511

I'm using the System.Net.Mail interfaces to send emails; however, if I am sending to a list of emails and one of them does not exist, the entire message is aborted and an exception is thrown. Is there a way to tell the Client to not throw an exception on a failed message and to sent to the rest of the recipients

Thanks,
Kevin


Answer this question

System.Net.Mail -- how to continue sending to emails if one of the emails fails with a 511

  • rokohl

    here is a simple method, althought there could be a simpler way, you can do something like I did here:



            static string Host = "";

            public static void SendList(MailMessage[] List)
            {
                bool[] ListErrors = new bool[List.Length];
                bool AllTrue = true;

                for (int I = 0; I < List.Length; I++)
                    ListErrors//emoticons/emotion-55.gif" alt="Idea" /> = SendMail(ListIdea);

                for (int I = 0; I < ListErrors.Length; I++ )
                    if (ListErrorsIdea == false)
                        AllTrue = false;

                if (AllTrue)
                    Console.WriteLine("All messages were sent.");
                else
                {
                    Console.Write("Not all messages were sent, messages not sent are:");
                    for (int I = 0; I < ListErrors.Length; I++)
                        if (ListErrorsIdea == false)
                            Console.Write(" " + I.ToString + ".");
                }
            }

            public static bool SendMail(MailMessage TheMessage)
            {
                SmtpClient Send = new SmtpClient(Host, 25);
                try
                {
                    Send.Send(TheMessage);
                }
                catch (Exception)
                {
                    return false;
                }
                finally
                {
                    return true;
                }
            }

     


    Simple ;)

  • System.Net.Mail -- how to continue sending to emails if one of the emails fails with a 511