2013-09-04 4 views
23

system.net.mail.smtpclient에는 두 가지 방법이 있습니다. 나는 매우 혼란 스럽습니다.이 두 가지 방법의 차이점은 무엇입니까?

1. 가 SendAsync (은 MailMessage, Object)를

Sends the specified e-mail message to an SMTP server for delivery. This method does not block the calling thread and allows the caller to pass an object to the method that is invoked when the operation completes. -MSDN

2. SendMailAsync (은 MailMessage)

Sends the specified message to an SMTP server for delivery as an asynchronous operation.가 과부하되지 않도록 두 방법의 이름이 다르다고 -MSDN

통지. 여기 정확히 다른 점은 무엇입니까?

내가 두 가지 방법에 대해 MSDN에 의해 ​​주어진 설명으로 명확 대답을 찾고

은 매우 모호 (적어도 나를 위해 그것을이다.)

+1

는'SendMailAsync'이 랩은'SendAsync' 새로운'async' 기능이 작동합니다. –

답변

21

의 차이는 SendMailAsync 새로운 async/await 기술과 기타를 사용 하나 이전 콜백 기술을 사용합니다. 그리고 더 중요한 것은 전달 된 Object이 메소드가 완료 될 때 userState이라는 이벤트 핸들러로 전달된다는 것입니다.

+6

더 나은 말했다 : SendMailAsync 작업을 반환합니다. 당신이 기다리고 있든 없든간에 당신의 전화입니다. – usr

+1

@ usr, 잘 말했습니다. 확실히 '기다려야'할 필요는 없습니다. –

+0

나는 하나의 이익이 다른 것보다 무엇인지 알고 싶습니까? SendMailAsync가 더 좋다고 가정하고 있습니다. –

7

먼저 둘 다 비동기 적으로 작동합니다.

그러나 .NET 2 이후 SendAsync이 존재했습니다. 새로운 작업 시스템을 지원하면서 역 호환성을 유지하기 위해 SendMailAsync이 추가되었습니다.

SendMailAsyncTask보다는 void 반환하고 필요한 경우 SmtpClient 새로운 asyncawait 기능을 지원 할 수 있습니다.

+0

둘 다 비동기 적으로 작동합니다. 나는 하나의 이익이 다른 것보다 무엇인지 알고 싶습니까? –

0
//SendAsync 
public class MailHelper 
{ 

    public void SendMail(string mailfrom, string mailto, string body, string subject) 
    { 
     MailMessage MyMail = new MailMessage(); 
     MyMail.From = new MailAddress(mailfrom); 
     MyMail.To.Add(mailto); 
     MyMail.Subject = subject; 
     MyMail.IsBodyHtml = true; 
     MyMail.Body = body; 
     MyMail.Priority = MailPriority.Normal; 

     SmtpClient smtpMailObj = new SmtpClient(); 
     /*Setting*/ 
     object userState = MyMail; 
     smtpMailObj.SendCompleted += new SendCompletedEventHandler(SmtpClient_OnCompleted); 
     try 
     { 
      smtpMailObj.SendAsync(MyMail, userState); 
     } 
     catch (Exception ex) { /* exception handling code here */ } 
    } 

    public static void SmtpClient_OnCompleted(object sender, AsyncCompletedEventArgs e) 
    { 
     //Get the Original MailMessage object 
     MailMessage mail = (MailMessage)e.UserState; 

     //write out the subject 
     string subject = mail.Subject; 

     if (e.Cancelled) 
     { 
      Console.WriteLine("Send canceled for mail with subject [{0}].", subject); 
     } 
     if (e.Error != null) 
     { 
      Console.WriteLine("Error {1} occurred when sending mail [{0}] ", subject, e.Error.ToString()); 
     } 
     else 
     { 
      Console.WriteLine("Message [{0}] sent.", subject); 
     } 
    } 

    // 

} 

//SendMailAsync 
public class MailHelper 
{ 
    // 
    public async Task<bool> SendMailAsync(string mailfrom, string mailto, string body, string subject) 
    { 
     MailMessage MyMail = new MailMessage(); 
     MyMail.From = new MailAddress(mailfrom); 
     MyMail.To.Add(mailto); 
     MyMail.Subject = subject; 
     MyMail.IsBodyHtml = true; 
     MyMail.Body = body; 
     MyMail.Priority = MailPriority.Normal; 

     using (SmtpClient smtpMailObj = new SmtpClient()) 
     { 
      /*Setting*/ 
      try 
      { 
       await smtpMailObj.SendMailAsync(MyMail); 
       return true; 
      } 
      catch (Exception ex) { /* exception handling code here */ return false; } 
     } 
    } 
} 
관련 문제