簡體   English   中英

這兩種方法有什么區別?

[英]what is the difference between these two methods?

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對這兩種方法的描述非常模糊(至少對我來說是這樣)。

區別在於SendMailAsync使用新的async/await技術,而另一種使用舊的回調技術。 更重要的是,傳遞的Object只是在方法完成時作為userState傳遞給事件處理程序。

首先,它們都是異步工作的。

但是,自.NET 2以來, SendAsync已經存在。為了在支持新任務系統的同時保持向后兼容性,添加了SendMailAsync

SendMailAsync返回一個Task而不是void並允許SmtpClient支持新的asyncawait功能(如果需要)。

SendMailAsyncSendAsync的簡單TAP包裝器更多信息: SmtpClient.SendMailAsync方法線程安全嗎?

  //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; }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM