none
Convert Outlook.Attachment to Outlook.MailItem? RRS feed

  • General discussion

  • Using VSTO with VS 2008 and Outlook 2007.

    I have a plugin I created that reads specific emails that have an email attached to them.  I need to access that attached email's attachments.  To do that I was going to cast the original attachment as a mailitem and then traverse those attachments to get what I need, however this isn't working out so well.

    I am able to verify that it is pulling in the correct attachment before casing (e.g. the_subject_name.msg is the filename).

    When I try and do:

    Outlook.Mailitem real_email = attachment as Outlook.MailItem;
    real_email is left null.

    When I try and do:
    Outlook.Mailitem real_email = (Outlook.MailItem)attachment;
    an exception is thrown indicating that it can't cast that particular com type to the mailitem interface.

    How can I do what I want?
    • Changed type Bessie Zhao Monday, September 14, 2009 10:19 AM
    Wednesday, September 2, 2009 1:59 PM

All replies

  • Hi Justin,

    Based on my research, we can achieve this by another way. you could get an answer from the same thread: Get attachment from forwarded mail. I also make a code sample which the original attachment is a Excel file.   

                Outlook.NameSpace outlookNS = this.Application.GetNamespace("MAPI");
                Outlook.MAPIFolder mFolder = outlookNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                Outlook.MailItem mail = mFolder.Items.GetLast() as Outlook.MailItem;
                if (mail.Attachments.Count > 0)
                {
                    Outlook.OlAttachmentType attach_type = mail.Attachments[1].Type;
                    if (attach_type == Outlook.OlAttachmentType.olEmbeddeditem)
                    {                    
                        Outlook.Attachment at = mail.Attachments[1];
                        at.SaveAsFile(@"C:\Temp\1.msg");                   
                    }
                }
                Outlook.MailItem mt=this.Application.CreateItemFromTemplate(@"C:\Temp\1.msg", mFolder) as Outlook.MailItem;
                //mt.Attachments[1].SaveAsFile(@"C:\Temp\1.xls");

    Best regards,
    Bessie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    Tuesday, September 8, 2009 11:50 AM
  • We are changing the issue type to "General Discussion" because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to "Question" by opening the Options list at the top of the post window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, September 14, 2009 10:19 AM
  • Working with email attachments that are also emails which in turn contains user defined properties that I want to access, then I perform the following steps:

    Outlook.Application mailApplication = new Outlook.Application();

    Outlook.NameSpace mailNameSpace = mailApplication.GetNamespace(“mapi”);

    // make sure it is an embedded item

    If(myAttachment.Type == Outlook.OlAttachmentType.olEmbeddeditem)

    {

        myAttachment.Type.SaveAsFile(“temp.msg”);

        Outlook.MailItem attachedEmail = (Outlook.MailItem)mailNameSpace.OpenSharedItem(“temp.msg”);

        String customProperty = attachedEmail.PropertyAccessor.GetProperty(

            “http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-c000-000000000046}/myProp

    }

    If you open the MailItem using, then I will not have access to the properties as mentioned above:

    Outlook.MailItem attachedEmail = (Outlook.MailItem)mailApplication.CreateFromTemplate(“temp.msg”); 

    I hope this helps others that might be heading down my path.

    Friday, October 26, 2012 3:28 PM
  • Is there any alternative way without saving the attachment to Disk. i.e, like traversing into the attached email, to get its attachment ?

    Regards,

    Saturday, January 18, 2014 4:47 PM
  • Not using the Outlook Object Model - if you were using C++ or Delphi, you could use Extended MAPI to open the embedded message attachment using IAttach::OpenProperty(PR_ATTACH_DATA_OBJ, IID_IMessage). Or you can use Redemption (which wraps Extended MAPI and can be used from any language) and do something like the following:

    RDOSession session = new RDOSession();
    RDOMail msg = (RDOMail)session.GetRDOObjectFromOutlookObject(YourOutlookMailItem);
    foreach (RDOAttachment att in msg.Attachments)
    {
    	if (att.Type == rdoAttachmentType.olEmbeddedItem)
    	{
    		RDOMail embeddedMsg = att.EmbeddedMsg;
    		ProcessEmbeddedMessage(embeddedMsg);
    	}
    }


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



    Saturday, January 18, 2014 5:45 PM
  • Thanks.
    Saturday, January 18, 2014 5:54 PM