2013-11-21 37 views
0

我使用smtp将邮件发送到多个地址,我想获取sendig失败的邮件地址。如果失败,赶上邮件地址

message.To.Add(new System.Net.Mail.MailAddress("[email protected]")); 
message.To.Add(new System.Net.Mail.MailAddress("[email protected]")); 
message.To.Add(new System.Net.Mail.MailAddress("[email protected]")); 
client.Send(message); 

在上面的列表中,第一个和第三个邮件发送了,第二个邮件发送不了。 所以我想抓住失败的邮件地址([email protected])。

解决方案请

回答

0

里面你client.Send()方法,你应该调用每一个特定的发送方法在try-catch块,然后服务于失败。 另一种选择是重新抛出这样的例外,并抓住它在代码中的catch块你附:

try { 
client.Send(message); 
} 
catch (Exception e) 
{ 
//do smth with it 
} 

告诉我们更多的东西你Send()方法和client对象。这将让我们更具体。

0

这是很容易,如果你使用的是C#SmtpClient并且可以使用SendAsync方法

//client and MailMessage construction 
client.SendCompleted += (sender, eventArgs) => { 
    string emailAddress = eventArgs.UserState as String; 
    if (eventArgs.Error != null) { 
     //an error occured, you can log the email/error   
    } 
    else //the email sent successfully you can log the email/success 
}; 
client.SendAsync(mail, mail.Sender.Address); 

,如果你喜欢的拉姆达可以用新SendCompletedEventHandler(方式)来代替; 并有一个方法,如

... methodName(object sender, System.ComponentModel.AsyncCompletedEventArgs eventArgs) 
{ 
    string emailAddress = eventArgs.UserState as String; 
     if (eventArgs.Error != null) { 
      //an error occured, you can log the email/error 
     } 
     else //the email sent successfully you can log the email/success 
} 
相关问题