SmtpClient发送异步阻止应用程序,而不是异步发送

时间:2013-04-12 13:30:18

标签: c# asp.net smtpclient sendasync

我使用开发服务器和SmtpClient.SendAsync,邮件被发送,但它仍会阻止页面上的所有其他操作,直到发送完成。我搜索了很多,似乎无法看到问题是什么

    protected void SendMailButton_Click(object sender, EventArgs e) {

        SendMailTemplate("verify@site.com", "your-email@gmail.com", "Your company Listing is Approved !", "CompanyApproved.txt", new string[] { "Send Mail company" });           
    }

    public void SendMailTemplate(string From, string To, string Subject, string EmailTemplate, string[] Params) {

        var message = new MailMessage(From, To);
        message.IsBodyHtml = true;
        message.Subject = Subject;

        var sr = new StreamReader(HttpRuntime.AppDomainAppPath + "/Resources/EmailTemplates/" + EmailTemplate);         //Cannot use Server.MapPath here
        message.Body = sr.ReadToEnd(); sr.Close();
        for (int i = 0; i < Params.Length; i++) {
            message.Body = message.Body.Replace("<%Param" + (i + 1) + "%>", Params[i]);
        }

        var smtpClient = new SmtpClient("mail.site.com", 25);

        smtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
        smtpClient.SendCompleted += (s, e) => { smtpClient.Dispose(); message.Dispose(); };
        smtpClient.SendAsync(message, null);

    }

0 个答案:

没有答案