none
C# script to open email attachment(.msg) in a folder and download attachment. RRS feed

  • Question

  • Hi,

    Could any one suggest any Visual studio C# script to download the attachment from the /msg file in an offline folder (any folder on shared drive).

    I have seen many scripts from the mail server but I need to open .msg which is offline and download the attachment.

    Thanks

    AmanM

    Friday, May 18, 2018 7:29 PM

All replies

  • The following does not handle downloading, only extraction from .msg files. I've used it solely for unit test.

    See MsgReader - there are samples in the first link. 

    Source code: https://github.com/Sicos1977/MSGReader

    NuGet: https://www.nuget.org/packages/MSGReader/


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, May 18, 2018 9:04 PM
    Moderator
  • There are several methods.

    The simplest one is to use Outlook Interop =>

    Microsoft.Office.Interop.Outlook.Application oOutlookApp = new Microsoft.Office.Interop.Outlook.Application(); 
    MailItem oMailItem = null;
    
    oMailItem = oOutlookApp.CreateItemFromTemplate("e:\\test.msg");
    Attachments oAttachments = oMailItem.Attachments;
    for (int i = 1; i <= oAttachments.Count; i++)
    {
        Attachment oAttachment = oAttachments[i];
        oAttachment.SaveAsFile(AppDomain.CurrentDomain.BaseDirectory + oAttachment.FileName);
        Marshal.ReleaseComObject(oAttachment);
        oAttachment = null;
    }
    Marshal.ReleaseComObject(oAttachments);
    oAttachments = null;
    Marshal.ReleaseComObject(oMailItem);
    oMailItem = null;
    oOutlookApp.Quit();
    Marshal.ReleaseComObject(oOutlookApp);
    oOutlookApp = null;

    • Proposed as answer by Dolen Zhang Monday, May 21, 2018 7:06 AM
    Sunday, May 20, 2018 9:12 AM