Search code examples
c#imap

Save attach of attached mail


I'm using Mail.dll from limilabs to manage an IMAP folder. There is one mail with an attachment that is an eml file, so a mail. It has in turn one attached eml file that I need to extract.

So the email structure is as follows:

Email
  |- Attachment: file.eml
                   |- Attachment file2.eml

This is my code:

IMail email = new MailBuilder().CreateFromEml(imap.GetMessageByUID(uid));
Console.WriteLine(email.Subject);
// save all attachments to disk
foreach(MimeData mime in email.Attachments)
{
if (uid == 1376)
{
        System.IO.Directory.CreateDirectory(string.Format(@"c:\EMAIL\{0}", uid));
        mime.Save(@"c:\EMAIL\" + uid + "\\" + mime.SafeFileName);
        MimeData help;
        if (mime.ContentType.ToString() == "message/rfc822")
        {
        //i need to cast this attach in a imail
        }
    }
}

How can I extract the inner-most eml file (file2.eml in the structure mentioned above)?


Solution

  • From this link, it looks like you should be able to do the following:

    if (attachment.ContentType == ContentType.MessageRfc822)
    {
        string eml = ((MimeText)attachment).Text;
        IMail attachedMessage = new MailBuilder().CreateFromEml(eml);
        // process further
    }