473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 3226
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: SPG | last post by:
Hi, I have an application that is sending out emails to my clients. Some mails are text based and some are html based depending on their preferences. Text emails are fine. I am setting up...
2
by: New User | last post by:
Hi, I have a PDF file that I am trying to send as an attachment through a C# program. Even though the PDF file can be opened by itself, sometimes the same file cannot be opened as an...
0
by: john s | last post by:
Hi, I was wondering if it is possible to decode a MIME email message using built-in .net methods? I have a .eml file produced by IIS' SMTP server, and I would like to divide and decode the parts...
8
by: Xarky | last post by:
Hi, I am downloading a GIF file(as a mail attachement) with this file format, Content-Transfer-Encoding: base64; Now I am writing the downloaded data to a file with this technique: ...
0
by: Bill L. | last post by:
We have an application that uses CDO to email large quantities of data as email attachments. We are migrating to a .Net application that does the same using the System.Web.Mail (SMTP) functionality...
1
by: C CORDON | last post by:
I am sending an email with .NET the body is html, what do I have to do or what enconding do i have to use so that is displays spanis characters (í,ó,ñ, etc.) ? Thanks
1
by: krishan123456 | last post by:
i have tried to send email with attached doc file but when i receive the mail i find the attached file in encoded text instead of actuall attachment.the code that i used to send an email is given...
2
by: Torsten Bronger | last post by:
Hallöchen! I thought that with the following code snippet, I could generate a MIME email stub: #!/usr/bin/env python # -*- coding: utf-8 -*- from email.MIMEText import MIMEText from...
0
by: tourerukcom | last post by:
Hi i wonder if any one can help. I have script that that sends a email with mulitple attachments. It seems to work fine as i get the email with the attachments on. But other people i send it to...
4
by: luke noob | last post by:
This is my HTML... <head> <script type="text/javascript" src="js/jquery-1.2.6.pack.js"></script> <script type="text/javascript" src="js/script.js"></script> </head> <body>
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.