Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the size of an email in .NET

Tags:

c#

email

asp.net

Say I have an email class that has the following properties:

public string From { get; set; }
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public Dictionary<string, byte[]> Attachments { get; set; }

I need to calculate the size of the email and if it is less than 10MB email it otherwise send the content as a fax (to prevent it being bounced from its destination).

I can calculate the size of attachments relatively easily.

Is there an accurate way to calculate the entire email size? I'm guessing I'll need to add the size of the strings as well as any header information that will be appended?

like image 935
woggles Avatar asked Oct 13 '11 07:10

woggles


1 Answers

You cannot accurate know the size of the header. Since all servers that pass the mail to the next server might add some data to the header. This can range from one simple line, to the complete score of the spam scanning.

So you will always get it wrong a few bytes.

As for the size of the attachments: They are encoded, so the nr of bytes is not the actual size taken. If you convert them to Base64 and take the length of that string, that's about the size they will take in the email (without attachment header, depending on the attachment name). An estimate is nr of bytes * 1.33.

You can get a good clue, if the mail approaches 10 MB, but when the final and received mail is exactly 10 MB is not known.

like image 86
GvS Avatar answered Nov 15 '22 01:11

GvS