1
votes

In my program, I am reading an Exchange mailbox using EWS .NET API and forwarding the emails as attachment to an external email address. Code that I have used is below

  private void ForwardMessage(ExchangeService exchangeService, EmailMessage item)
  {
    ResponseMessage responseMessage = item.CreateForward();
    item.Load(new PropertySet(BasePropertySet.FirstClassProperties, new 
                     PropertySet(){ItemSchema.MimeContent,                                                                            
                    ItemSchema.Subject}));
    var mail = new EmailMessage(exchangeService);
    var attachment = mail.Attachments.AddFileAttachment(String.Format("
                        {0}.eml", item.Subject),  
    item.MimeContent.Content);
    String forwardEmailAddresses = 
                      MailProcessorSettings.Default.ForwardEmailAddress;
    char[] delimiters = { ',', ';' };
    foreach (var emailAddress in forwardEmailAddresses.Split(delimiters, 
              StringSplitOptions.RemoveEmptyEntries))
    {
        mail.ToRecipients.Add(emailAddress);
    }
    mail.Subject = item.Subject;
    mail.Send();
  }

I am able to open the email forwarded as attachment in Outlook. However, if I send it to Gmail or to other users using different email clients it shows up as a blank attachment.

How do I ensure that the email forwarded as an attachment preserves the original content?

1

1 Answers

1
votes

Adding a line indicating the content type of the message seems to fix this issue. Either of the following settings seems to work. After adding the ContentType, I was able to download the attachment from Gmail although I had to use the Outlook Client to open it.

attachment.ContentType = "multipart/alternative"
OR
attachment.ContentType = "message/rfc822"