Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

(C#) E-mail problem - encoding to Base64..

Options
  • 08-01-2006 12:09am
    #1
    Registered Users Posts: 43,781 ✭✭✭✭


    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.

    PS - below, i've recorded the lengths at each stage for a particular image that's 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!


Comments

  • Closed Accounts Posts: 33 sean_or99


    There is already a MailAttachment class; no point trying to reinvent the wheel.

    It can be found here.

    Simple way to do it is:
    myMessage.Attachments.Add(new MailAttachment("C:\image1.jpg", MailEncoding.Base64));
    


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Can you verify that your socket is actually sending the data? Try sending the data in 10,000 byte sections in a while loop (remember to allow for the fact that the last section won't be 10,000 bytes long :P) and make sure everything actually sends. Its possible the sending socket is choking up and not sending all the data.

    If everything sends correctly, it could be a serverside problem, but i'd doubt that.

    EDIT: IIRC a string in base64 encoding would be a different size to a byte array, thats normal. To test, just take your base64 string and convert it to a byte array, and you'll get a bit identical array to the initial one.


  • Registered Users Posts: 43,781 ✭✭✭✭Basq


    sean_or99 wrote:
    There is already a MailAttachment class; no point trying to reinvent the wheel.
    Cheers for that but prefer not to use external classes or DLL's.. am trying to construct it using my own methods.
    Can you verify that your socket is actually sending the data? Try sending the data in 10,000 byte sections in a while loop (remember to allow for the fact that the last section won't be 10,000 bytes long :P) and make sure everything actually sends. Its possible the sending socket is choking up and not sending all the data.
    You're a genius mate. Asked this on a couple of C# Forums and Newsgroups and couldn't get a working answer.

    Sent a 1K smiley image there and it went through fine - was obviously the problem!

    Problem solved - many thanks!
    EDIT: IIRC a string in base64 encoding would be a different size to a byte array, thats normal. To test, just take your base64 string and convert it to a byte array, and you'll get a bit identical array to the initial one.
    Oh.. and thanks for clearing that up!


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    basquille wrote:
    You're a genius mate. Asked this on a couple of C# Forums and Newsgroups and couldn't get a working answer.
    I wouldn't go that far :p I made my first application with sockets a few months ago, and i had similar kinds of problems. In the end i was using while loops to bundle the data off to sockets and while loops for recieving data and some other stupid stuff to make sure my code didn't continue til all the data successfully sent/recieved.

    Its probably not the best way of doing it, but it works.


Advertisement