Search code examples
c#vstooutlook-addin

How manage mails and attachments with a c# application for windows


I should create a C# application to manage the mail and attachments that arrive to me on Office 365 (Outlook). In the app I would like to select from which domains you need to download the mail, based on this the app downloads only the related emails with attachments and shows them to me. This way I can decide what to print or not.

I need this app because I have to record all the projects that come to me from clients, architecture projects and therefore I need to divide everything according to the client. Can you tell me what is the best way to develop this? I mean if it is better to create VSTO for Outlook or something else or if there is other way. I would like to start with the right method.

I had thought about installing Outlook on the client, synchronized with Office 365, creating a VSTO that takes care of copying the interested emails (selecting just the domains of interest) and putting attachments in various folders, showing the attachments in an orderly manner and grouped.

Can you suggest me the best method? I mean at the structural level (how to design system), and not at the code level (which I think I know it).

Thanks so much


Solution

  • You are right, you can develop a VSTO based add-in where you may handle the NewMailEx event of the Application class. This event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem.

    The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item. Use this method with caution to minimize the impact on Outlook performance.

    To save the attached files you can use the MailItem.Attachments property which returns an Attachments object that represents all the attachments for the specified item. Use the Attachment.SaveAsFile method which saves the attachment to the specified path.

     If TypeName(myItem) = "MailItem" Then  
     
     Set myAttachments = myItem.Attachments 
     
     'Prompt the user for confirmation 
     
     Dim strPrompt As String 
     
     strPrompt = "Are you sure you want to save the first attachment in the current item to the Documents folder? If a file with the same name already exists in the destination folder, it will be overwritten with this copy of the file." 
     
     If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then  
       myAttachments.Item(1).SaveAsFile Environ("HOMEPATH") & "\My Documents\" & _  
       myAttachments.Item(1).DisplayName  
     End If 
     
     Else  
       MsgBox "The item is of the wrong type."  
     End If 
     
    End Sub
    

    See Walkthrough: Create your first VSTO Add-in for Outlook to get started quickly.