Reputation: 823
I want to display the approximate size of the email before sending it. I am using java/jsp/js.
I have input like below
<html>
......
<img src="xyz.jpg"/>
......
</html>
Right now, i am calculating the email size as said below.
Since html is sent as message body, i am taking whole html content and getting byte size
strSize = content.getBytes().length;
I am getting the image size by reading image and getting length
URL url = new URL(imageurl);
InputStream is = url.openStream();
byte[] b = new byte[2048];
int length = 0;
while((length = is.read(b)) != -1) {
totalLength += length;
}
Later i will add both string size and image size. BUT size of the email and size i get before sending it are both different. Is there anything else i should consider or am i doing it in wrong way?
Thank you in advance.
Upvotes: 3
Views: 2255
Reputation: 718906
You are trying to do something that is rather tricky. Consider this:
An email consists of a header and a body:
The header consists of some lines that contain information that you provide (title, email addresses, etc) other lines that are added by the mail sending agent, and more lines that are added by the transfer agents and the receiving program.
The simplest for of body just consists of lines of text that are passed through (almost) verbatim. But most email bodies are actually MIME encoded, and the MIME encoding adds stuff that say where each "part" starts and ends, what its type is and so on. And some parts (such as images) that are non-textual in nature need to be encoded as text characters so that they can be transmitted safely.
When you combine all of these things, you can see that the process of figuring out how big the various parts of the email are going to be is very complicated.
Possibly, a simpler approach is to create a dummy mail transfer agent, send the mail there and then measure its overall size, and the size of its components by roughly parsing the transmitted mail. But even that is a non-trivial task.
Upvotes: 2
Reputation: 29971
The only way to get an accurate size is to create your own OutputStream that counts all the bytes written but does nothing with them, then use msg.writeTo() to write the message to that OutputStream and when you're done ask it how many bytes were written.
Upvotes: 3
Reputation: 798744
You should consider that the message may be encoded in quoted-printable and/or uuencoded. You should get the raw message after packaging but right before sending, and check the length of that.
Upvotes: 1