这两种方法有什么区别?

时间:2022-09-06 14:34:43

system.net.mail.smtpclient has two methods for which I am very confused.

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

1 . SendAsync(MailMessage, Object)

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

将指定的电子邮件发送到SMTP服务器以进行传递。此方法不会阻止调用线程,并允许调用者将对象传递给操作完成时调用的方法。 -MSDN

2 . SendMailAsync(MailMessage)

2。 SendMailAsync(MAILMESSAGE)

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

将指定的邮件发送到SMTP服务器以作为异步操作传递。 -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对这两种方法的描述非常模糊(至少对我而言是这样)。

4 个解决方案

#1


21  

The difference is one SendMailAsync uses the new async/await technology and the other uses the old callback technology. And more importantly, the Object that's passed is simply passed into the event handler as the userState when the method completes.

区别在于SendMailAsync使用新的async / await技术,另一种使用旧的回调技术。更重要的是,传递的Object只是在方法完成时作为userState传递给事件处理程序。

#2


7  

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支持新的异步和等待功能(如果需要)。

#3


0  

SendMailAsync a simple TAP wrapper for SendAsync More info: Are the SmtpClient.SendMailAsync methods Thread Safe?

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

#4


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

#1


21  

The difference is one SendMailAsync uses the new async/await technology and the other uses the old callback technology. And more importantly, the Object that's passed is simply passed into the event handler as the userState when the method completes.

区别在于SendMailAsync使用新的async / await技术,另一种使用旧的回调技术。更重要的是,传递的Object只是在方法完成时作为userState传递给事件处理程序。

#2


7  

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支持新的异步和等待功能(如果需要)。

#3


0  

SendMailAsync a simple TAP wrapper for SendAsync More info: Are the SmtpClient.SendMailAsync methods Thread Safe?

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

#4


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