Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SmtpClient very slow - about 2 seconds to send one very small email

I'm using SmtpClient to send a simple email.

The email consists of about 25 plaintext characters so it's small.

It however takes the SmtpClient about 2000 milliseconds to send one of them. I do not create the SmtpClient for each send - that is created on program start so the only thing that is done is this:

DateTime start = DateTime.Now;

MailMessage oMsg = new MailMessage();
// TODO: Replace with sender e-mail address.
oMsg.From = new MailAddress(settings._Username);
oMsg.To.Add(new MailAddress(emailEvent._ContactItemToUse.Data));
oMsg.Subject = emo._Subject;
oMsg.BodyEncoding = Encoding.UTF8;
oMsg.IsBodyHtml = emo._IsHtmlText;
oMsg.Body = emo._Text;
client.Send(oMsg);
TimeSpan timeWasted = DateTime.Now.Subtract(start); // between 1000-2000 ms

This is of course very bad, and I can't figure out why. Can I improve it?

like image 700
Ted Avatar asked Apr 18 '11 15:04

Ted


People also ask

Why emails sent by SmtpClient does not appear in sent items?

They are not recorded in the sent items since it is only send using the account from the user on SMTP level, it doesn't really use the mailbox to send the email. The only option you have is not to use SmtpClient and use the Exchange API to send mail.

What is Net mail SmtpClient?

This class allows you to attach files, streams, or text to an email message. MailAddress. Represents the email address of the sender and recipients. MailMessage. Represents an email message.

Why is SMTP slow?

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.

How long does it take SMTP to send email?

The same happens with SMTP servers—though instead of taking days, the process takes a few minutes at most. You might have also come across the term SMTP port. Those are the communication endpoints that handle the transfer of email data over SMTP as it moves through a network, from one server to another.


3 Answers

The SmtpClient class I believe does not reuse the same connection for each mail sent (edit: apparently this is now possible in .NET 4.0, see the differences in documentation for SmtpClient). Opening a new connection is expensive, and that is probably what takes time. There are commercial SMTP components that do and offer much higher performance. Depending on SMTP server and mail size, it is possible to achieve something like at least 50mails/second.

However this might not be an issue for you if you change the architecture slightly. What I do in my application is that SmtpClient delivers mails to a folder, by using smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory and setting the PickupDirectoryLocation to the wanted directory. What this does is, instead of sending the mail messages over the network, it will write them to the specified folder as standard mime messages (.eml format).

Here you can either use the IIS SMTP server or simply make another background thread/process to consume the .eml files created and deliver them to the recipients or to another SMTP server.

This approach I think is much superior, simply because:

  • The client code sending the mail never has to wait for the actual mail to be sent, which can take a fair while depending on connection speed, latency etc.
  • If the mail sent did not succeed, the client code is not affected. The mail can be sent some other time in the background.
  • The mail queue is persistent, if the application is stopped and started again, mails in the queue will not be lost.
  • Easier for testing purposes.

As a simpler approach, you can use SendAsync instead of Send, but it doesn't give all the immediate approaches the PickupDirectory approach will give.

like image 58
Can Gencer Avatar answered Oct 15 '22 11:10

Can Gencer


Speed of SmtpClient class is mostly dependent on the SMTP server that you're connecting to & your internet connection speed. The best way to optimize your through-output using SmtpClient.SendAsync and creating up to 10 or more streamlined connections to the smtp server. After all, this is same strategy of all the modern web browsers do to speed up browsing.

like image 26
Teoman Soygul Avatar answered Oct 15 '22 11:10

Teoman Soygul


A few things come to mind.

First off, some sites will purposely slow down the connection in order to make it less profitable for spammers to send mail to their systems. This is called Tarpitting.

Interestingly, the mail server your site uses to broadcast out might even have this turned on. ( http://winzenz.blogspot.com/2005/12/enabling-smtp-tarpitting-in-windows.html )

Other things that might cause issues is if the receiving mail server has a short TTL for it's DNS settings and/or YOUR web server has some bad (e.g. dead or overloaded) DNS servers in it's IP setup.

like image 29
NotMe Avatar answered Oct 15 '22 11:10

NotMe