tags:

views:

208

answers:

2

I have a function that sends email using asp.net built in mail framework. I've included it below.

public void SendMessage()
{
    var message = new MailMessage();
    var client = new SmtpClient();

    // Get the Message Envelope Details
    this.LoadMessageDetailsFromFile();

    // Process rules (if any): Rules engine not implemented yet!
    this.SetConfiguration(message);

    // Formats the message body template using XSLT
    this.FormatMessageBody(message);

    // Adds the attachments 
    this.AddAttachments(message);


    // Send the mail
    client.Timeout = 999999999;
    client.Send(message);

    //Clean up attachments
    foreach (var attachment in message.Attachments)
    {
        attachment.Dispose();
    }
}

Now at no point is the total message size greater than 10MB, however exchange is producing an error saying that 20MB is the max size for each session. Does .net somehow batch send messages to SMTP? Why would it be producing this error, even when each time I send an email, I create a new SMtpClient object?

Exact Exchange Error: Session size exceeds fixed maximum session size

It seems the underlining connection is reused, even if you create new instances of SMTPClient.

Anyone know a workaround, the following does not work:

Smtp client = new SmtpClient("hostname");
client.ServicePoint.MaxIdleTime = 0;
client.ServicePoint.ConnectionLimit = 1;

Thanks in advance

+1  A: 

Attachments on emails get encoded using base64, which could make them considerably larger depending on the content of the file. From the Wikipedia article on base64:

Thus, the actual length of MIME-compliant base64-encoded binary data is usually about 137% of the original data length, though for very short messages the overhead can be a lot higher because of the overhead of the headers. Very roughly, the final size of base64-encoded binary data is equal to 1.37 times the original data size + 814 bytes (for headers). In other words, you can approximate the size of the decoded data with this formula: bytes = (string_length(encoded_string) - 814) / 1.37. Please note that these formulas should not be used to calculate the exact memory size to hold encoded data since they are very rough and produce errors in both directions

Rory
Interesting info indeed, however If I started with 10MB max attachment size, then surely this should not double the message size?
JL
It's unlikely, but possible. It depends on the content of the file, text files generally don't grow much but binary files might. Why don't you try encoding your attachment and checking the size? You might be surprised by the outcome.
Rory
Think I found the problem - http://www.vbforums.com/showthread.php?t=584384
JL
Damn, that's scary... Ummm, "nice" find.
Rory
A: 

Any SMTPClient provided with the .net framework below v4 Beta 2 is buggy and NEVER sends a QUIT command to the SMTP Server. There is no workaround. The solution is to use a 3rd party application to send emails. .net fails for mass emailing on SMTP servers that limit their session size to 20MB.

JL
And when I talk about mass here, not talking about spamming the planet, talking about sending 10 messages in an intranet...
JL