471,570 Members | 934 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 software developers and data experts.

E-mail problem - encoding to Base64..

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.ToCharArr ay());
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.

Jan 7 '06 #1
6 3135
> 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.

Jan 7 '06 #2
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?

Jan 9 '06 #3
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!

Jan 9 '06 #4
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?

Jan 9 '06 #5
> 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.

Jan 9 '06 #6
<br************@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 9 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by SPG | last post: by
8 posts views Thread by Xarky | last post: by
1 post views Thread by C CORDON | last post: by
2 posts views Thread by Torsten Bronger | last post: by
reply views Thread by tourerukcom | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.