Search code examples
exchange-serverews-managed-api

Forward email using EWS managed api keeping the headers


I am looking for a sample code on how to forward an existing email message (one that is already in my inbox) using the managed api.

When forwarded is there some way to keep a message original headers while forwarding it? for example someone sent an email to me -i would like that ews will forward it to another recipient without changing the headers (original receive time from ,bcc etc...).


Solution

  • Given an EmailMessage object, you can just call the CreateForwareMessage() method:

    var forwareMessage = item.CreateForward();
    

    Regarding the other question: Get the MIME content of the mail and attach it to a new message:

    item.Load(new PropertySet(BasePropertySet.IdOnly, ItemSchema.MimeContent));
    var mail = new EmailMessage(service);
    var attachment = mail.Attachments.AddFileAttachment("Original message.eml", item.MimeContent.Content);
    attachment.ContentType = string.Format("message/rfc822; charset={0}", item.MimeContent.CharacterSet);
    mail.ToRecipients.Add("hkrause@infinitec.de");
    mail.Subject = "testmail";
    mail.SendAndSaveCopy();
    

    EDIT:

    Create forward message and set reply to header:

    var fw = item.CreateForward();
    var fwMsg = fw.Save(WellKnownFolderName.Drafts);
    fwMsg.ReplyTo.Add("personA@company.com");
    fwMsg.SendAndSaveCopy();