none
Saving attachements when saving email message RRS feed

  • Question

  • I am writing a little add-in for outlook that will attach an email to a Visual Studio - TFS workitem.

    I am first saving the Outlook message to disk as an MSG and then adding the save MSG file to the TFS workitem.

    This works, except that when I save the original outlook message to disk, it does not include the attachments. I know that I can save the individual attachments but then when I import the MSG file to TFS, the original attachments are not included unless I specify them explicitly.

    This is not ideal since, the attachments get uploaded as individual attachments and there is no way to associate them or even know that they were originally attachments to the email.

    is there a way to save the original emal message as an MSG that includes the attachments? or is there another way to do this?

    Sunday, May 17, 2015 10:40 PM

Answers

  • Hi,

    >> is there a way to save the original emal message as an MSG that includes the attachments? or is there another way to do this?

    You may use MailItem.SaveAs method to save a mailitem as SMG format. The following code you can refer to, it save the mail items which have more than one attachment as smg format with the attachments.

    private void SaveMails()
            {
                Outlook.MAPIFolder inBox = this.Application.ActiveExplorer()
                    .Session.GetDefaultFolder(Outlook
                    .OlDefaultFolders.olFolderInbox);
                Outlook.Items inBoxItems = inBox.Items;
                Outlook.MailItem newEmail = null;
              
                try
                {
                    foreach (object collectionItem in inBoxItems)
                    {
                        newEmail = collectionItem as Outlook.MailItem;
                        if (newEmail != null)
                        {
                            if (newEmail.Attachments.Count > 0)
                            {
                                for (int i = 1; i <= newEmail
                                   .Attachments.Count; i++)
                                {
                                    newEmail.SaveAs("D:\\" + newEmail.Subject+".msg");
                                   
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string errorInfo = (string)ex.Message
                        .Substring(0, 11);
                    if (errorInfo == "Cannot save")
                    {
                        MessageBox.Show("Can't Save");
                    }
                }
            }
    

    Best Regards,

    Lan


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, May 19, 2015 3:27 AM
    Moderator

All replies

  • Saving in the olMsg or olMsgUnicode format does include the attachments.

    Please show your code. Did you try to open the MSG file after saving it to make sure that the attachments are there?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Sunday, May 17, 2015 10:51 PM
  • Hi,

    >> is there a way to save the original emal message as an MSG that includes the attachments? or is there another way to do this?

    You may use MailItem.SaveAs method to save a mailitem as SMG format. The following code you can refer to, it save the mail items which have more than one attachment as smg format with the attachments.

    private void SaveMails()
            {
                Outlook.MAPIFolder inBox = this.Application.ActiveExplorer()
                    .Session.GetDefaultFolder(Outlook
                    .OlDefaultFolders.olFolderInbox);
                Outlook.Items inBoxItems = inBox.Items;
                Outlook.MailItem newEmail = null;
              
                try
                {
                    foreach (object collectionItem in inBoxItems)
                    {
                        newEmail = collectionItem as Outlook.MailItem;
                        if (newEmail != null)
                        {
                            if (newEmail.Attachments.Count > 0)
                            {
                                for (int i = 1; i <= newEmail
                                   .Attachments.Count; i++)
                                {
                                    newEmail.SaveAs("D:\\" + newEmail.Subject+".msg");
                                   
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string errorInfo = (string)ex.Message
                        .Substring(0, 11);
                    if (errorInfo == "Cannot save")
                    {
                        MessageBox.Show("Can't Save");
                    }
                }
            }
    

    Best Regards,

    Lan


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, May 19, 2015 3:27 AM
    Moderator
  • Sorry for the delay in responding, I was traveling

    I retested and now it seems to work. I am not sure whay it did not work in previous tests, maybe file type, size, etc.?

    thanks for your help

    m

    Sunday, May 31, 2015 1:51 AM