简体   繁体   中英

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);

Get the mail, which should be added as attachment. then call «SaveAs({filename}, Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG)» and add this file to your new mail

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM