1

Im using the development server and the SmtpClient.SendAsync, the mail gets Sent, but it still blocks all other operations on the page until sending is complete. Ive searched alot and cant seem to see what the problem is

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

    }
Warren
  • 384
  • 4
  • 17
  • 1
    I got this when it couldn't make a connection with the smtp. Before the real asynchronous action begins it does some other things, probably connect to the smtp. When that is completed, the asynchronous action will execute internally. Try putting it (for the sake of testing) in a backgroundworker and see if that operation will continue. – Silvermind Apr 12 '13 at 13:45
  • Thanks.. Did that.. using Thread.Queue.. – Warren May 26 '13 at 13:09

0 Answers0