简体   繁体   中英

C# get mail and its attachment from outlook

I've made program which one of the purposes is to open OutLook client where user can write his email, add attachments etc. After sending I want to get all attachments which were added to email, as well as all email in my program. I tried to handle close event

((Microsoft.Office.Interop.Outlook.ItemEvents_10_Event)email.oMsg).Close += new Microsoft.Office.Interop.Outlook.ItemEvents_10_CloseEventHandler(GetAttachmentsInfo);

and then

    if (email.oMsg.Attachments.Count > 0)
    {
        foreach (Microsoft.Office.Interop.Outlook.Attachment at in email.oMsg.Attachments )
        { attachments.Add(at); }
    }



     email is OutlookEMail
oMsg is Email Item
attachments is List<Attachment>

but when I close client Attachment throws exception that cannot find object. as well as these in List.

I know that Microsoft.Office.Interop.Outlook.Attachment is not a file, but only something like path to this file containing it name and size. So question : is it possible to save attachments after client is closed in my program? (Without using Email.SaveAttachments os SaveEmail methods, because it uses time and computer space)?

Here are two other options you could try:

1. You could listen for MailItem additions to the SentItems Folder via Folder.ItemAdd .

Outlook.Folder sentItems = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) as Outlook.Folder;
sentItems.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(GetAttachmentsInfo);

2. You could attach to the MailItem.Send event.

((Outlook.ItemEvents_10_Event)MailItem).Send  += new Outlook.ItemEvents_10_SendEventHandler(GetAttachmentsInfo);

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