.net - 在 System.Net.Mail.SendCompletedEventHandler 中获取电子邮件地址

标签 .net email callback smtpclient

我有一种方法可以将电子邮件发送到多个电子邮件地址。像这样的事情:

foreach(var email in emails){

    // code to create mail variable

    var smtp = new SmtpClient();
    smtp.SendCompleted += new SendCompletedEventHandler(callBack);
    smtp.SendMailAsync(mail);
}

正如预期的那样,每个电子邮件地址都会调用一次我的回调。回调会弹出一个通知客户端,其中包含成功/错误消息。

我希望在回调中获取电子邮件地址,以便能够使用导致成功/错误的电子邮件指定成功/错误消息。 这是我想要的回调,带有 EMAIL 变量:

EmailSender.SendAsyncEmailDoneCallBack callBack = (sender, e) => {
    if (e.Cancelled)
        PushSystemNotification("Message canceled to " + EMAIL, CurrentUser);
    if (e.Error != null) 
        PushSystemNotification("Message not sent to " + EMAIL, CurrentUser);
    else
        PushSystemNotification("Message sent to " + EMAIL, CurrentUser);
};

那么,谁能告诉我是否可以从这个发送回调中获取电子邮件地址?或者是否有其他方法可以提供此功能?

最佳答案

只需使用 SendAsync 的第二个参数(而不是 SendMailAsync)

smtp.SendCompleted += (sndr, e) => {
    Console.WriteLine(e.UserState);
};
smtp.SendAsync(mail,"test");

它可以是您想要传递给 SendCompleted 事件处理程序的任何对象。

关于.net - 在 System.Net.Mail.SendCompletedEventHandler 中获取电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25486967/

相关文章:

python - 如何从电子邮件正文中解析 HTML - Python

linux OOM(内存不足) killer 电子邮件通知?

Java:如何从内部就地类调用 super 方法

c# - Flickr API 回调取消操作 C#

c - 在c中为轮询添加回调函数

.net - StructureMap 单例因参数而异?

.net - .NET对应的Spring框架是哪个?

c# 具有非抽象方法的抽象类抛出 ReflectionTypeLoadException

.net - EF Core linq 和条件包含然后包含问题

iphone - 如何在我的 iPhone 应用程序中发送电子邮件 [csv 格式]?