Search code examples
c#exchangewebservices

Create an email using EWS Exchange and attach another email


I'm having trouble attaching an email to a new email using EWS.

So i have the Microsoft.Exchnage.Webservice.Data.Item in my findResults.

If I find an issue in the form data of the email then I want to attach that item to a new email and send it to a supervisor for manual input.

I have tried;

EmailMessage newMessage = new EmailMessage(exchange);
newMessage.Subject = "Failed lead creation";
ItemAttachment attachment = new ItemAttachment("New Lead", message);

I can't seem to create the ItemAttachment as the erro I am getting is "ItemAttachment does not contain a constructor that takes 2 arguments".

How do I create a new message in EWS, attach the current Item to it and send to another recipient?

Thaks


Solution

  • You can't another message directly you need to use the MimeContent of the Original Message and then create an ItemAttachment based on that eg something like

        FolderId  folderid= new FolderId(WellKnownFolderName.Inbox,"MailboxName");    
        Folder Inbox = Folder.Bind(service,folderid);  
        ItemView ivItemView =  new ItemView(1) ;     
        FindItemsResults<Item> fiItems = service.FindItems(Inbox.Id,ivItemView);
        if(fiItems.Items.Count == 1){  
        EmailMessage mail = new EmailMessage(service);   
        EmailMessage OriginalEmail = (EmailMessage)fiItems.Items[0];
        PropertySet  psPropset= new PropertySet(BasePropertySet.IdOnly);    
        psPropset.Add(ItemSchema.MimeContent);
        psPropset.Add(ItemSchema.Subject);
        OriginalEmail.Load(psPropset);  
        ItemAttachment Attachment = mail.Attachments.AddItemAttachment<EmailMessage>();
        Attachment.Item.MimeContent = OriginalEmail.MimeContent;  
        ExtendedPropertyDefinition PR_Flags = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);    
        Attachment.Item.SetExtendedProperty(PR_Flags,"1");    
        Attachment.Name = OriginalEmail.Subject;  
        mail.Subject = "See the Attached Email";  
        mail.ToRecipients.Add("glen.scales@domain.com");
        mail.SendAndSaveCopy();     
    

    Cheers Glen