E-mail problem - encoding to Base64..

B

brianbasquille

Hello all,

Am writing an small basic e-mail client and i'm having difficulty
encoding images to Base64. When i send an attachment image with it to
Outlook Express, it seems as if it only converts a certain amount of
the image to Base64.

The message does appear as attached but doesn't render correctly and is
a much smaller filesize than the original image attached.

I seem to be only getting the first 774 bytes of an image i attach and
viewing the headers of the message seems like it didn't send all of the
Base64 encoded data.

// convert attachment to base 64
System.IO.FileStream fs = new System.IO.FileStream(txtAttachment.Text,
System.IO.FileMode.Open);
byte[] by = new byte [fs.Length];
long bytesRead = fs.Read(by, 0, (int)fs.Length);
fs.Close();

string bytes2 = System.Convert.ToBase64String((by), 0, by.Length);
Data+= bytes2 + CRLF + CRLF;
Data+= "--" + uniqueIDContID + "--" + CRLF;
Data+= CRLF + "." + CRLF;

szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length) ;

Note:
txtAttachment.Text is the location of the file (e.g
"C:\images\image001.jpeg")
uniqueIDContID is the unique boundary generated for a multipart
message.
CRLF is just a character return ("\r\n")
I DO issue the "QUIT" command to the server after that block of code..
i just didn't think it was necessary to include it.

Anyone got any suggestions as to where i'm going wrong?

Many thanks in advance.
 
P

Paul Henderson

Anyone got any suggestions as to where i'm going wrong?

Perhaps rather than just
long bytesRead = fs.Read(by, 0, (int)fs.Length);
try reading the data in in chunks, or using a BinaryReader. Easiest
might be
while (bytesRead < fs.Length) bytesRead += fs.Read(by, bytesRead,
fs.Length - bytesRead);
or something like that.

If that doesn't help, use the debugger to see how long the input byte
array is compared with the file, and compare it with the length of the
base64 string.
 
B

brianbasquille

Reading the data in chunks didn't seem to help - still getting just the
first 774 bytes:

Sample lengths for one particular file:

Filestream fs: Length = 271379
long bytesRead = 271379
Base64String Length = 361840

Not too sure but is it normal to be getting a larger length for the
Base64String?
 
B

brianbasquille

Thanks for the quick reply Paul.

I'm afraid reading the data in chunks produces the same result. I print
the header out to a RichTextBox and all appears correct in there.

Below, i've recorded the lengths at each stage for a particular file
being encoded:

FileStream fs: length = 271739
bytes[] by: length = 271739
String bytes2 (Base64String): length = 361840

Should that last String be the same length as the bytes? Not too sure
on the details!
 
B

brianbasquille

Reading the data in chunks didn't seem to help - still getting just the
first 774 bytes:

Sample lengths for one particular file:

Filestream fs: Length = 271379
long bytesRead = 271379
Base64String Length = 361840

Not too sure but is it normal to be getting a larger length for the
Base64String?
 
P

Paul Henderson

Sample lengths for one particular file:
Filestream fs: Length = 271379
long bytesRead = 271379
Base64String Length = 361840

Not too sure but is it normal to be getting a larger length for the
Base64String?

Getting longer length for the Base64 version than the original is
correct (should be a factor of 3 : 4, as you effectively convert each
string of three bytes [24 bits] into four bytes [32 bits], so only
certain characters are used).

So, the numbers there are correct, meaning the problem lies elsewhere.

I wonder if the problem is to do with line breaks - the SMTP protocol
limits line lengths to about 1000 characters; that would equate to
about 750 bytes of decoded data. So, if you split your Base64 string up
every (say) 990 characters and insert a CR/LF pair at that point, then
it might help. I think your email client should ignore the inserted
whitespace and only 'look' at the base64 data.
 
J

Jon Skeet [C# MVP]

Reading the data in chunks didn't seem to help - still getting just the
first 774 bytes:

Sample lengths for one particular file:

Filestream fs: Length = 271379
long bytesRead = 271379
Base64String Length = 361840

Not too sure but is it normal to be getting a larger length for the
Base64String?

Absolutely. Base64 converts each chunk of three bytes into a chunk of
four characters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top