0

I am trying to develop an application which sends email to multiple recepients.

I am reading all the mail addresses from a text file line by line.But I am wondering ..for example I have 50 mail addresses in my list and something went wrong with the number 45.

How can I inform the user about about it and is it possibble sending the rest of the list?

private void Senmail(IEnumerable<string> list)
{
    var mes = new MailMessage {From = new MailAddress(_uname.Trim())};
    var col = new MailAddressCollection();
    foreach (string adres in list)
    {
        col.Add(adres);
    }

    for (int i = 0; i < GetList().Count(); i++)
    {
        mes.To.Add(col[i].Address);
    }
    mes.Subject = txtbody.Text;
    mes.Body = txtmail.Text;
    mes.Attachments.Add(new Attachment(_filename));

    var client = new SmtpClient
    {
        Port = 587,
        Host = "smtp.gmail.com",
        EnableSsl = true,
        Timeout = 5000,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(_uname, _pass),
    };
    object userState = mes;
   client.SendAsync(mes,userState);
   client.SendCompleted += client_SendCompleted;

    MessageBox.Show("ok");
}

void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    if (e.Error!=null)
    {
      //
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
user3733078
  • 249
  • 2
  • 11
  • 5
    Define "successful" in this context. – Lasse V. Karlsen Nov 25 '14 at 15:43
  • 3
    If the email was sent to the SMTP with no errors, then it was successfully sent to the SMTP. After that, it is out of your hands. – Drew Kennedy Nov 25 '14 at 15:44
  • The only way to be reasonably certain is to deliver the message "manually" by talking to the designated SMTP server in the destination domains DNS MX record rather than using a 3rd party to relay for you. – Alex K. Nov 25 '14 at 15:51

1 Answers1

0

Since you're using Gmail your options are very limited. At best you will get an exception if the SMTP host cannot be reached, but if you get a 200 response then you have technically fulfilled the end of your delivery contract and any subsequent response will be delivered after the fact (email does not exist, for example).

You might find some extra options under MailMessage's DeliveryNotificationOptions property and handle exceptions, but these are limited and really don't provide what I believe you are looking for.

Elliot Rodriguez
  • 608
  • 6
  • 25