34

system.net.mail.smtpclient有两种我很困惑的方法。

1. SendAsync(邮件消息,对象)

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. 发送邮件异步(邮件消息)

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

请注意,两个方法的名称不同,因此它不是重载。这里到底有什么区别?

我正在寻找非常明确的答案,因为 MSDN 对这两种方法的描述非常含糊(至少对我来说是这样。)

4

4 回答 4

30

不同的是一个SendMailAsync使用新async/await技术,另一个使用旧的回调技术。更重要的是,当方法完成时,Object传递的那只是简单地传递给事件处理程序。userState

于 2013-09-04T13:34:21.590 回答
9

首先,它们都是异步工作的。

但是,SendAsync自 .NET 2 以来就存在。为了保持向后兼容性,同时支持新的任务系统SendMailAsync,添加了。

SendMailAsync如果需要,返回 aTask而不是void并允许SmtpClient支持新功能。asyncawait

于 2013-09-04T13:34:48.040 回答
1
  //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; }
        }
    }
}
于 2017-11-23T05:10:11.400 回答
0

SendMailAsync一个简单的 TAP 包装器以获取SendAsync 更多信息:SmtpClient.SendMailAsync 方法线程安全吗?

于 2014-05-13T07:56:54.043 回答