supercoco
supercoco

Reputation: 512

Fetch raw e-mail text with EWS (headers, body and encoded attachments)

Is there a way to fetch the raw email text using EWS? I would like to get the whole text including headers, body, and encoded attachments.

Is this possible?

Upvotes: 4

Views: 1035

Answers (1)

Francisco Prado
Francisco Prado

Reputation: 86

I don't know if this is what you are looking for, but it should help.

It downloads the entire message file, including encoded attachments, header, subject, sender, receiver, etc...

try this:

static void Main(string[] args)
{
    try
    {
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
        service.Credentials = new NetworkCredential("USR", "PWD", "Domain");
        service.AutodiscoverUrl("[email protected]");

        FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(int.MaxValue));

        Console.WriteLine("Found : " + findResults.TotalCount + " messages");

        foreach (EmailMessage message in findResults.Items)
        {
            try
            {
                message.Load(new PropertySet(ItemSchema.MimeContent));
                MimeContent mc = message.MimeContent;
                // I use this format to rename messages files, you can do whatever you want
                string n = string.Format("-{0:yyyy-MM-dd_HH-mm-ss-ffff}.eml", DateTime.Now);
                string path = @"C:\folder\message" + n;
                FileStream fs = new FileStream(path, FileMode.Create);
                fs.Write(mc.Content, 0, mc.Content.Length);
                fs.Flush();
                fs.Close();

                //message.Delete(DeleteMode.HardDelete);   // It deletes the messages permanently
                //message.Delete(DeleteMode.MoveToDeletedItems);  // It moves the processed messages to "Deleted Items" folder

            }
            catch (Exception exp)
            {
                Console.WriteLine("Error : " + exp);
            }
        }
    }
    catch (Exception exp2)
    {
        Console.WriteLine("Error : " + exp2);
    }
}

Hope it helps, cheers.

Upvotes: 4

Related Questions