public void GetAttachments() { Microsoft.Office.Interop.Outlook.Application myolApp = default(Microsoft.Office.Interop.Outlook.Application); Microsoft.Office.Interop.Outlook.NameSpace ns = default(NameSpace); MAPIFolder Inbox = default(MAPIFolder); object Item = null; Attachment Atmt = default(Attachment); string FileName = null; string subject = null; string AttachmentName = null; string Body = null; string SenderName = null; string SenderEmailAddress = null; string CreationTime = null; int i = 0; int j = 0; try { myolApp = (Microsoft.Office.Interop.Outlook.Application)Interaction.CreateObject("Outlook.Application",""); ns = myolApp.GetNamespace("MAPI"); ns.Logon("", "", false, true); Inbox = ns.Folders["Mailbox - Name"].Folders["Inbox"]; i = 0; j = 1; //Scan for attachments foreach (object Item_loopVariable in Inbox.Items) { Item = Item_loopVariable; System.Windows.Forms.Application.DoEvents(); if ((Item as MailItem) != null ? ((MailItem)Item).UnRead : false) { Body = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body; ((Microsoft.Office.Interop.Outlook.MailItem)Item).HTMLBody = Body; ((Microsoft.Office.Interop.Outlook.MailItem)Item).SaveAs(@"\\path\"+"filename",Microsoft.Office.Interop.Outlook.OlSaveAsType.olHTML); j = j + 1; } } //Clear Memory Atmt = null; Item = null; ns = null; } catch (System.Exception ex) { MessageBox.Show("An unexpected error has occurred." + "\r\n" + "Please note and report the following information." + "\r\n" + "Script Name: GetAttachments" + "\r\n" + "Error Description: " + ex.Message + "\r\n" + "Error StackTrace: " + ex.StackTrace, "Error!"); Atmt = null; Item = null; ns = null; } }
Body = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body; ((Microsoft.Office.Interop.Outlook.MailItem)Item).HTMLBody = Body; ((Microsoft.Office.Interop.Outlook.MailItem)Item).SaveAs(@"\path\"+"filename", Microsoft.Office.Interop.Outlook.OlSaveAsType.olHTML );
HTMLBody
TextWriter
((Microsoft.Office.Interop.Outlook.MailItem)Item
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)