Search code examples
c#asp.netemailemail-attachments

How to attach a received mail as an attachment to a new mail using c#


I have a (Microsoft.Office.Interop.Outlook.MailItem mail) mail object.
I want this mail to attach as an attachment to another mail.
but not able to find any solution.So can anyone please help.

I have created another mail object as shown below : Microsoft.Office.Interop.Outlook.MailItem toSendMail = this.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);


Solution

  • As per your requirement you want to send an existing mail object as an attachment to another mail in outlook.

    One way of doing this is by saving the existing mailItem as an attachment to other. Try this:

    private void AddMessageAsAttachment(Microsoft.Office.Interop.Outlook.MailItem 
                         mailContainer,Microsoft.Office.Interop.Outlook.MailItem mailToAttach)
            {
                Microsoft.Office.Interop.Outlook.Attachments attachments = null;
                Microsoft.Office.Interop.Outlook.Attachment attachment = null;
                try
                {
                    attachments = mailContainer.Attachments;
                    attachment = attachments.Add(mailToAttach,
                       Microsoft.Office.Interop.Outlook.OlAttachmentType.olEmbeddeditem, 1, "The attached e-mail");
                    mailContainer.Save();
                }
                catch (Exception ex)
                {
                        Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (attachment != null) Marshal.ReleaseComObject(attachment);
                    if (attachments != null) Marshal.ReleaseComObject(attachments);
                }
            }
    

    reference : https://www.add-in-express.com/creating-addins-blog/2011/08/12/how-to-add-existing-e-mail-message-as-attachment/