电子邮件不是异步发送

时间:2018-10-11 16:40:26

标签: c# asp.net .net asynchronous

我有两种发送电子邮件的方法。一个在发送电子邮件时使UI停止,另一个应该是异步的,在电子邮件在后台发送时保持UI处于活动状态(在我的用例中,稍后重定向到另一页)。线。我对异步函数不太熟悉,但是我想这应该可以工作。

我的具体问题是:

此方法发送电子邮件

    public static void sendEmail(string nameFrom, string passwordFrom, string to, string subject, string text, string smtpHost, string attachments = null, int port = 25)
    {            
        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(nameFrom, passwordFrom);
        System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(nameFrom);
        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();

        message.To.Add(to);
        message.From = from;
        message.Subject = subject;
        message.Body = text;
        message.IsBodyHtml = true;

        smtp.Host = smtpHost; 
        smtp.Port = port;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = credentials;

        if (attachments != null && attachments != "")
        {
            if (System.IO.File.Exists(attachments) == true)
            {
                System.Net.Mail.Attachment attFile = new System.Net.Mail.Attachment(attachments);
                message.Attachments.Add(attFile);
            }
        }

        smtp.Send(message);
    }

这不是

 public static async Task sendEmailAsync(string nameFrom, string passwordFrom, string to, string subject, string text, string smtpHost, string attachments = null, int port = 25)
    {              
        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(nameFrom, passwordFrom);
        System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress(nameFrom);
        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();

        message.To.Add(to);
        message.From = from;
        message.Subject = subject;
        message.Body = text;
        message.IsBodyHtml = true;

        smtp.Host = smtpHost; 
        smtp.Port = port;
        smtp.EnableSsl = true;
        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = credentials;

        if (attachments != null && attachments != "")
        {
            if (System.IO.File.Exists(attachments) == true)
            {
                System.Net.Mail.Attachment attFile = new System.Net.Mail.Attachment(attachments);
                message.Attachments.Add(attFile);
            }
        }                    
        await smtp.SendMailAsync(message);                    
    }

第二个为什么不发送电子邮件,我该如何解决?

1 个答案:

答案 0 :(得分:-2)

我建议使用Backgroundworker在其他线程中发送电子邮件。我认为这篇文章很好地解释了。 Async/await vs BackgroundWorker 我本来会在评论中放这个,但在这里没有足够的街头信誉。