none
Save outlook email item with attachment to local disk RRS feed

  • Question

  • Hi, i have a console app to open outlook email item. I can display email item correctly with attachment but not able to open same saved outlook mail item from the locak disk(attachment is not embedded), here is my code and let me know where i am misisng.

                    outlook.Application app = new outlook.Application();
                    outlook.NameSpace ns = app.GetNamespace("MAPI");
                    outlook.MailItem email = (outlook.MailItem)(app.CreateItem(outlook.OlItemType.olMailItem));
                    string currentuser = System.Environment.UserName;
                    string filepath = "http://test.com/Images/Sample.jpg";
                    string filecopy = @"\\test.com\MSG\" + "test" + "-" + currentuser + ".msg";
                    Uri uri = new Uri(filepath);
                    string filename = System.IO.Path.GetFileName(uri.AbsolutePath);
                    email.To = "";
                    email.Subject = "";
                    string htmlBody = "sample1";
                    email.HTMLBody = "<Font face=calibri>" + htmlBody + "</Font>" + "</b><br/>" + "</b><br/>" + "<img src='cid:" + filename + "'</img>";
           


                outlook.Attachment attach = email.Attachments.Add(filepath, outlook.OlAttachmentType.olEmbeddeditem, null, filename);

                    attach.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x370E001F", filename);
                    email.SaveAs(filecopy, outlook.OlSaveAsType.olMSG);

                    email.Display();


    • Edited by cb39 Wednesday, April 5, 2017 9:23 PM
    • Moved by Bob Ding Thursday, April 6, 2017 6:58 AM Relate Office Dev
    Wednesday, April 5, 2017 9:23 PM

All replies

  • Hi,

    This forum is discussing and asking questions about the Windows Form and ClickOnce, it will be more appropriate to ask your question at Outlook for Developers
    Thank you for your understanding.

    Best Regards,

    Stanly


    MSDN Community Support.
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 6, 2017 6:22 AM
  • Hello,

    I have noticed the following line of code:

     outlook.Attachment attach = email.Attachments.Add(filepath, outlook.OlAttachmentType.olEmbeddeditem, null, filename);
    

    You need to pass the olByValue value as the second parameter. That means the attachment is a copy of the original file and can be accessed even if the original file is removed. olEmbeddeditem is for Outlook objects (emails and etc.). In that case the attachment is an Outlook message format file (.msg) and is a copy of the original message.


    Description
    olByValue The attachment is a copy of the original file and can be accessed even if the original file is removed.
    olByReference This value is no longer supported since Microsoft Outlook 2007. Use olByValue to attach a copy of a file in the file system.
    olEmbeddeditem The attachment is an Outlook message format file (.msg) and is a copy of the original message.
    olOLE The attachment is an OLE document.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Thursday, April 6, 2017 10:44 AM