我如何知道我发送的电子邮件是否成功

本文关键字:电子邮件 是否 成功 何知道 | 更新日期: 2023-09-27 18:31:59

我正在尝试开发一个向多个收件人发送电子邮件的应用程序。

我正在逐行读取文本文件中的所有邮件地址。但我想知道..例如,我的列表中有 50 个邮件地址,数字 45 出了点问题。

我如何通知用户,是否可以发送列表的其余部分?

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)
    {
      //
    }
}

我如何知道我发送的电子邮件是否成功

由于您使用的是Gmail,因此您的选择非常有限。如果无法访问SMTP主机,则最多会收到异常,但是如果您收到200响应,则从技术上讲,您已经履行了交付合同的终止,并且任何后续响应都将在事后传递(例如,电子邮件不存在)。

您可能会在 MailMessage 的 DeliveryNotificationOptions 属性下找到一些额外的选项并处理异常,但这些选项是有限的,实际上并没有提供我相信您正在寻找的内容。