简体   繁体   English

这两种方法有什么区别?

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

system.net.mail.smtpclient has two methods for which I am very confused. system.net.mail.smtpclient有两种方法,我很困惑。

1 . 1。 SendAsync(MailMessage, Object) 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 -MSDN

2 . 2。 SendMailAsync(MailMessage) SendMailAsync(MAILMESSAGE)

Sends the specified message to an SMTP server for delivery as an asynchronous operation. -MSDN -MSDN

Notice that the names of two methods are different so it is not an overload. 请注意,两个方法的名称不同,因此它不是重载。 What exactly is the difference here? 这有什么区别?

I am looking for very clear answer as the description given by MSDN for both methods is very ambiguous (at least for me it is.) 我正在寻找非常明确的答案,因为MSDN对这两种方法的描述非常模糊(至少对我来说是这样)。

The difference is one SendMailAsync uses the new async/await technology and the other uses the old callback technology. 区别在于SendMailAsync使用新的async/await技术,而另一种使用旧的回调技术。 And more importantly, the Object that's passed is simply passed into the event handler as the userState when the method completes. 更重要的是,传递的Object只是在方法完成时作为userState传递给事件处理程序。

Firstly, they both work asynchronously. 首先,它们都是异步工作的。

However, SendAsync has existed since .NET 2. In order to maintain backwards compatiblity whilst supporting the new Tasks system SendMailAsync was added. 但是,自.NET 2以来, SendAsync已经存在。为了在支持新任务系统的同时保持向后兼容性,添加了SendMailAsync

SendMailAsync returns a Task rather than void and allows the SmtpClient to support the new async and await functionality if required. 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