2013-11-21 2 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])를 찾으려고합니다.

솔루션 pls는 당신의 client.Send() 방법 당신이 try-catch 블록의 모든 특정 전송 메소드를 호출하고 실패를 봉사해야 내부

답변

0

.

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

Send() 방법 client 객체에 대해 우리에게 많은 것을 알려주기 : 또 다른 옵션은 최대 이러한 예외를 다시 발생하고 부착 한 코드에서 catch 블록에 그것을 잡을 것입니다. 그것은 우리를 더욱 구체적으로 만들 것입니다. 당신은 C#을려면 SmtpClient를 사용하고 있고 람다는 새 SendCompletedEventHandler (methodName로)로 대체 될 수있다 선호하는 경우가 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); 

를 사용할 수 있는지

0

이 꽤 간단합니다; 과 같은 방법이 있고

... 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 
} 
관련 문제