sending mail using smtp is too slow

2019-07-04 19:05发布

I'm trying to send mail to user using SMTP. I'm able to send mail when the user clicks the send button, but it's taking almost 7 seconds to get success message to the user which is too long and the user may click the button more than once unknowingly if it takes so long. Without this sendmail() method when the user clicks the submit button it's taking less than a second but with this sendmail() its taking almost 7 seconds. What might be the reason for this issue?

        string from = ConfigurationManager.AppSettings.Get("From");
        string pwd = ConfigurationManager.AppSettings.Get("Password");
        string Client= ConfigurationManager.AppSettings.Get("client");
        string port = ConfigurationManager.AppSettings.Get("port");
        string toMail = ConfigurationManager.AppSettings.Get("toaddress");

        NetworkCredential loginInfo = new NetworkCredential(from,pwd);
        MailMessage msg = new MailMessage();
        SmtpClient smtpClient = new SmtpClient(client, int.Parse(port));

        msg.From = new MailAddress(from );
        msg.To.Add(new MailAddress(toMail));
        msg.Subject = "Test Subject";
        msg.Body = "Test Mail"



        msg.IsBodyHtml = true;

        smtpClient.EnableSsl = true;
        smtpClient.UseDefaultCredentials = false;
        smtpClient.Credentials = loginInfo;
        smtpClient.Send(msg);

标签: asp.net smtp
3条回答
一纸荒年 Trace。
2楼-- · 2019-07-04 19:35

I had the same issue while using Send(), though each email was taking up to 20 seconds for me :/. After chatting back and forth with the hosting provider (same one for the cloud web server and mail server) I still had no luck.

I researched for 2 days and finally came across a footnote in the documentation on SendGrid.com that mentioned that some hosting providers limit/throttle SMTP port 25.

(https://sendgrid.com/docs/Integrate/index.html)

I changed to the other SMTP port 587 and the time went down from 20 seconds to less than a second per email.

Perhaps try that.

查看更多
疯言疯语
3楼-- · 2019-07-04 19:38

Because the process can be inherently slow; the call to Send(msg) will authenticate with the mail server, then validate and send the email - this doesn't happen in milliseconds.

I would launch a new thread to send the mail: -

public static void SendMail(MailMessage message)
{
    var thread = new Thread(() => Mailer.SendMailThread(message));
    thread.Start();
}

// note - ConfigWrapper just wraps app.config settings
private static void SendMailThread(MailMessage message)
{
    using (var server = new SmtpClient(ConfigWrapper.MailServer))
    {
        server.Credentials = new NetworkCredential(ConfigWrapper.MailUser, ConfigWrapper.MailPassword);
        server.Send(message);
    }
}

(You could achieve the same thing using the newer Task framework if you wish)

You should be aware that any exceptions inside the spawned thread can't be (easily) handled by the calling thread i.e. the thread the page runs in. You should implement some form of logging inside the SendMail method to log any exceptions.

查看更多
登录 后发表回答