2

I've been using MailKit to retrieve some emails using IMAP and forwarding them using SMTP (more info here), but it takes really long for the SMTP to send the email.

I'm using mailkit via NuGet

This is the code I'm using

<!-- language: c# -->

var before = DateTime.Now;
Console.Write("\tForwarding email... ");

smtpClient.Send(forwardMessage, fromMailboxAddress, new[] { toMailboxAddress });

Console.WriteLine(" done! ({0})", DateTime.Now - before);

And the time it takes is usually more than 30s. What is making me suspect there's somethign wrong is that the email is actually forwarded almost instantly: few seconds (or even less) than the code reaches the smtpClient.Send method, I can see the message appearing in the destination email account (I have Thunderbird opened at the same time), but something makes the code to be still doing something in the Send code line.

Is there a way to know what the code is doing and why does it take so long?

Community
  • 1
  • 1
Xavi Ivars
  • 634
  • 8
  • 22

1 Answers1

1

You can see what the SmtpClient.Send() code is doing here: SmtpClient.cs:1543

I can't think of any reason it would take more than 30 seconds to send if you are seeing the message show up at the destination within seconds after client.Send() is being called.

My only guess is that the server is taking a long time to send a response to the DATA (or BDAT) command (which is the command that actually sends the raw message data).

In other words, my guess is that it would have to be this line: SmtpClient.cs:1517 or this line: SmtpClient.cs:1488

This is the ReadResponse() method: SmtpClient.cs:320. Most likely the waiting will be in the Poll() call or, if the stream does not support polling (SslStream), then it will be stuck in the stream.Read() call waiting for a response from the server.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • I've been digging a bit more on this, and used WireShark (network sniffer) to capture the packets, and you're right. The server is taking more than 30 seconds to snd the SMTP response, even if it has done the action. I'll try to go with another approach (maybe SendAsync) to try to speedup that part. Thanks! – Xavi Ivars Apr 08 '15 at 13:59
  • @XaviIvars I'm facing the same problem although using async..... any suggestions? – Techy Jan 23 '17 at 13:59
  • You could try using wire shark to see if the problem is the server just like the other person discovered. – jstedfast Jan 23 '17 at 15:11
  • I am facing the same problem even though when using async. Do you find why this happens – Smart Dev Dec 03 '19 at 00:54
  • SMTP servers have been known to throttle clients if you are sending a lot of mail, and "a lot" is server defined, so it could be as little as a few messages in an arbitrary amount of time. – jstedfast Dec 03 '19 at 01:59