6

When I sent a mail using System.Net.Mail, it seems that the messages do not send immediately. They take a minute or two before reaching my inbox. Once I quit the application, all of the messages are received within seconds though. Is there some sort of mail message buffer setting that can force SmtpClient to send messages immediately?

public static void SendMessage(string smtpServer, string mailFrom, string mailFromDisplayName, string[] mailTo, string[] mailCc, string subject, string body)
{
    try
    {
        string to = mailTo != null ? string.Join(",", mailTo) : null;
        string cc = mailCc != null ? string.Join(",", mailCc) : null;

        MailMessage mail = new MailMessage();
        SmtpClient client = new SmtpClient(smtpServer);

        mail.From = new MailAddress(mailFrom, mailFromDisplayName);
        mail.To.Add(to);

        if (cc != null)
        {
            mail.CC.Add(cc);
        }

        mail.Subject = subject;
        mail.Body = body.Replace(Environment.NewLine, "<BR>");
        mail.IsBodyHtml = true;

        client.Send(mail);
    }
    catch (Exception ex)
    {
        logger.Error("Failure sending email.", ex);
    }

Thanks,

Mark

mservidio
  • 12,817
  • 9
  • 58
  • 84
  • Are you using [Send](http://msdn.microsoft.com/en-us/library/swas0fwc.aspx) or [SendAsync](http://msdn.microsoft.com/en-us/library/x5x13z6h.aspx)? Are you sending directly to your email server or via some other SMTP server? – Rup Sep 13 '11 at 14:42
  • Note that when I send myself an email message it comes through almost immediately, so it's not the smtp server. – mservidio Sep 13 '11 at 14:42
  • using Send. I just added my helper method... – mservidio Sep 13 '11 at 14:44
  • Is your server local (your local IIS for example) or is it a remote server (or service -- like SendMail)? – David Hoerster Sep 13 '11 at 14:46
  • a remote on site exchange server – mservidio Sep 13 '11 at 14:59
  • Contact the server owner for support. – Hans Passant Sep 13 '11 at 15:02
  • 1
    The `SmtpClient` pools connections. Consequently, the `SmtpClient` instance does not know when you are done. `Dispose` tells the `SmtpClient` instance you are done and to send a `QUIT` message on all of the established connections, followed by closing the TCP connections and then freeing the sockets. Thus, wrap your usage of `SmtpClient` in a `using` block or add a `finally` block where you invoke `SmtpClient.Dispose`. – jason Sep 13 '11 at 15:29
  • Additionally, get yourself a tool (Reflector, a static code analytis tool, whatever) that will point out when you are using a disposable object but not disposing of it. – jason Sep 13 '11 at 15:47
  • Thanks. Typically I actually wrap these sorts of things in using blocks anyways. Didn't realize that I forgot on this helper method. – mservidio Sep 13 '11 at 15:49

2 Answers2

10

Try this, if you're on Dotnet 4.0

using (SmtpClient client = new SmtpClient(smtpServer))  
{
    MailMessage mail = new MailMessage();
    // your code here.

    client.Send(mail);
}

This will Dispose your client instance, causing it to wrap up its SMTP session with a QUIT protocol element.

If you're stuck on an earlier dotnet version, try arranging to re-use the same SmtpClient instance for each message your program sends.

Of course, keep in mind that e-mail is inherently a store-and-forward system, and there is nothing synchronous (or even formally predictable) about delays from smtp SEND to reception.

mservidio
  • 12,817
  • 9
  • 58
  • 84
O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • Brilliant! That does it. There probably is further deferred work that isn't executed immediately unless disposed as you mention. – mservidio Sep 13 '11 at 15:18
0

I agree with Ollie. To answer your question, No, I don't believe there is any buffer setting you can set via a form.

What is confusing about your question is, you say the message take a minute or two to reach your inbox, but then go on to say that when sending to yourself, they go through instantly... I believe you meant that internally, messages send fine and the issue only occurs for external address. In this case, it sounds like maybe your email server maybe queing these messages behind other emails bound for external addresses (which is normal activity). Waiting a minute or two for an email on an external site isn't that long of a wait.

However, this is unlikely, but is your exchange server set to scan outgoing messages?

mutek
  • 423
  • 5
  • 8
  • When I said sending myself, I meant that when I open a new message in outlook and email myself, it comes through immediately into my own inbox. When I was using the code above to send myself email (to the same address), the message was taking a minute or two before it showed up in my inbox. – mservidio Sep 13 '11 at 15:33
  • That makes more sense. I wouldn't think this be a scanning issue, maybe the relay settings on the exchange server then? – mutek Sep 13 '11 at 15:40
  • See the comments above. @Jason explains the issue in more detail. – mservidio Sep 13 '11 at 15:41