Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I use OpenPOP library for Send and Recive Email.
A part of my program is save recived email to database.
In another part of my program i must show attachment files that saved into database to user.
In my save method, i save RawMessage (include headers, sender, attachments and etc) into database.

C#
var MailContent = GetMessage(MessageNumber);
DB.RawMessage = (from a in MailContent.RawMessage
                 select (byte)a).ToArray();

.
.
.
//finaly save DB into Database


Now my question is: How i can extract attachment files from saved RawMessage?
Posted

Hi, check this blog[^] for the code on attachment.

Something like
OpenPOP.MIMEParser.Message msg = MailContent.GetMessage(x, false);
if (msg != null) {
   for (int y = 0; y < msg.AttachmentCount; y++) {
      Attachment attachment = (Attachment)msg.Attachments[y];
      
      if (string.Compare(attachment.ContentType, "text/xml") == 0) {
         System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
         string xml = attachment.DecodeAsText();
         doc.LoadXml(xml);
         doc.Save(@"C:\POP3Temp\test.xml");
      }
   }
}
 
Share this answer
 
Hi Thank you for your response.
I solve my problem with below code:

First creat a message object from OpenPOP.Mime.Message:
C#
string StringRawContent = System.Text.ASCIIEncoding.ASCII.GetString(DB.RawMessage.ToArray());
OpenPOP.MIME.Message raw = new OpenPOP.MIME.Message(true, StringRawContent, false);


Now we have all parts of email such as Subject, Body, Sender, Attachments and etc in raw object.
For example i get first attachment file via below code:


C#
if (raw.Attachments.Count > 0)
    AttachmentFileContent = (from a in raw.Attachments.First().RawContent
                             select (byte)a).ToArray();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900