sending mail using smtp is too slow

喜欢而已 提交于 2019-12-23 17:13:36

问题


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

回答1:


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.




回答2:


Send the mail asynchronously.

http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx




回答3:


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.



来源:https://stackoverflow.com/questions/21182626/sending-mail-using-smtp-is-too-slow

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!