85391

sending mail using smtp is too slow

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

Answer1:

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.

Answer2:

Send the mail asynchronously.

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

Answer3:

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 <strong>SMTP port 25</strong>.

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

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

Perhaps try that.

Recommend

  • How to embed Images in a powershell email using MailMessage
  • SMTP Authentication Error “Failure Sendng Mail”
  • Sending email via GMail: A connection attempt failed because the connected party did not properly re
  • .NET Error sending email
  • Send e-mail using SSL
  • Add multiple horizontal lines in a boxplot
  • powershell email with html picture showing red x
  • The ftpes:// URI prefix is not recognized by FtpWebRequest
  • Azure runbook output to email
  • how to store an array from a multiple select
  • Sending e-mail without refresh the page
  • Unable to send mail from SSIS using script task
  • fortran “array of arrays” and “pack” issues
  • Always return true in first call Returns correct value after first call
  • C++ Trouble with operator code inheritage: am I require to copy same code for all derived classes?
  • VirtualDocumentRoot handling non-existent folders
  • SmtpClient sends email to junk
  • How to best manage SMTP clients
  • Can't access Tomcat 7 Manager app when running from Eclipse
  • ASP.NET windows authentication should always ask for credentials
  • auth.provider is not set to 'password' when user signs-in with email and password
  • Getting unread count in Sent Folder using Google Apps Script - GMail
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • Email verification using google app script and google forms
  • Get data from AJAX - How to
  • Using variable in a value field in jMeter
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Updating server-side rendering client-side
  • How to model a transition system with SPIN
  • How to pass list parameters for each object using Spring MVC?
  • Invalid access key error using credentials redeemed from an amazon open id token
  • Cant find why the layout is getting smaller
  • How to stop GridView from loading again when I press back button?
  • Bitwise OR returns boolean when one of operands is nil
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?