0

I am trying to retrieve an email attachment and save it to specific directory in filesystem using the code below.

Dim objMail As Message = New Message(Encoding.ASCII.GetBytes(strMessage))

For Each att In objMail.FindAllAttachments()
    Dim Stream as FileStream = New FileStream("D:\XX\" & att.FileName.ToString(), 
        FileMode.Create)
    Dim BinaryStream As New BinaryWriter(Stream)
    BinaryStream.Write(BitConverter.ToString(att.Body))
    BinaryStream.Close()
Next

I have also tried att.GetBodyasText()

Using this code I am able to save attachment file in desired folder. But while opening a file, I am getting errorL

File is not in proper Format or not Decoded Properly.

I am new to MIME encoding/decoding.

halfer
  • 19,824
  • 17
  • 99
  • 186
Spot Mark
  • 1
  • 1
  • 3

1 Answers1

8

I am a developer of OpenPop.NET.

There are multiple issues with the code you are using to instantiate the Message class:

  1. Where is the strMessage contents comming from?
  2. How do you know it is encoded in only ASCII?

These are two major issues that will likely make a big difference. You should NOT be using a string to contain the message, instead you should be using raw bytes!

For example (in C#):

byte[] byteMessage = someFileStream.ReadToEnd();
Message message = new Message(byteMessage);

In this way, you will not destroy the message by using a wrong encoding on the bytes. Typically the email will include a header which tells how to decode to bytes to a string, which is what the OpenPop Message class will do for you.

Now let me explain attachments. Attachments are typically raw bytes, for example a PNG picture is some bytes that a PNG picture reader will understand. For the PNG picture reader to understand the picture, the attachments raw bytes must be saved to a file. You can get the raw bytes using att.Body.

There are also attachments where the raw bytes does not make sense - for example a text attachment encoded in BASE64 is not very useful for a text reader program, and such an attachment must be converted to text before saved. You can get the text using att.GetBodyAsText().

What you are doing is taking the raw bytes for the attachment and then using a BitConverter to convert it into hexadecimal numbers - which I cannot make any meaning of. Instead, you should change your:

BinaryStream.Write(BitConverter.ToString(att.Body)) 

to

BinaryStream.Write(att.Body)

in case your attachment is a picture or some more complex file.

I hope this can help with your problem.

foens
  • 8,642
  • 2
  • 36
  • 48
  • Thanks foens for quick response.. I am trying to get mail from database and retrive mail attachments. In database mail content are saved in varbinary datatype. I am getting those mail content convert it to string for some processing and again convert to byte() using ASCIIEncoding.ASCII.getBytes() for retriving attachments.. Also I have tried att.GetBodyAsText(), att.Body as params for BinaryWriter. But still error is same!! Not decoded Properly when open exported pdf document.. Please help .. – Spot Mark Aug 05 '11 at 10:36
  • Also one more thing.. I am getting PlainText and Html MessagePart in Proper format.. I have also tried att.SaveToFile(new FileInfo("File Path")) to save attachment.. but no improvement – Spot Mark Aug 05 '11 at 10:42
  • 1
    Who added the emails to the database? An email (raw) _CANNOT_ be stored as a string, as it makes no sense. They should be stored as BLOB (Binary Large OBject) instead, or something that facilitates storing raw bytes. If you use ASCII.getBytes() you must be sure that the whole email is in ASCII, or else you will generate the wrong bytes - and later OpenPop will have a hard time figuring these bytes out (and therefore making wrong attachments). I think this is your main problem. Simply stated: You are giving OpenPop a email, where all content might have been destroyed by wrong encodings. – foens Aug 05 '11 at 11:11