CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2002
    Posts
    1,798

    How to send attachment with MS Outlook automation?

    I have found the following article to be a very excellent run through on how to use Outlook automation with C++ in Visual Studio 2010. However, I cannot find any descrption of how to add an attachment(s) to an email.

    How to automate Outlook 2010 by using C++ in Visual Studio 2010 together with MFC
    http://support.microsoft.com/kb/2575036

    Any help greatly appreciated. Thanks.
    mpliam

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How to send attachment with MS Outlook automation?

    This is what I can find in Office folder that could give some idea about the direction you are to move to.
    Code:
    // "C:\Program Files (x86)\Microsoft Office\Office12\MSOUTL.OLB"
    
    [
      uuid(00063024-0000-0000-C000-000000000046),
      helpcontext(0x000005e4),
      dual
    ]
    dispinterface _PostItem {
        properties:
        methods:
            [id(0x0000f000), propget, helpcontext(0x000005e5)]
            _Application* Application();
            [id(0x0000f00a), propget, helpcontext(0x000005e6)]
            OlObjectClass Class();
            [id(0x0000f00b), propget, helpcontext(0x000005e7)]
            _NameSpace* Session();
            [id(0x0000f001), propget, helpcontext(0x000005e8)]
            IDispatch* Parent();
            [id(0x0000f817), propget, helpcontext(0x000005e9)]
            Actions* Actions();
            [id(0x0000f815), propget, helpcontext(0x000005ea)]
            Attachments* Attachments();
            [id(0x00008535), propget, helpcontext(0x000005eb)]
            BSTR BillingInformation();
            [id(0x00008535), propput, helpcontext(0x000005eb)]
            void BillingInformation([in] BSTR rhs);
            [id(0x00009100), propget, helpcontext(0x000005ec)]
            BSTR Body();
            [id(0x00009100), propput, helpcontext(0x000005ec)]
            void Body([in] BSTR rhs);
            [id(0x00009001), propget, helpcontext(0x000005ed)]
            BSTR Categories();
            [id(0x00009001), propput, helpcontext(0x000005ed)]
            void Categories([in] BSTR rhs);
            [id(0x0000853b), propget, helpcontext(0x000005ee)]
            BSTR Companies();
            [id(0x0000853b), propput, helpcontext(0x000005ee)]
            void Companies([in] BSTR rhs);
            [id(0x0000fac0), propget, helpcontext(0x000005ef)]
            BSTR ConversationIndex();
            [id(0x00000070), propget, helpcontext(0x000005f0)]
            BSTR ConversationTopic();
            [id(0x00003007), propget, helpcontext(0x000005f1)]
            DATE CreationTime();
            [id(0x0000f01e), propget, helpcontext(0x000005f2)]
            BSTR EntryID();
            [id(0x0000f095), propget, helpcontext(0x000005f3)]
            FormDescription* FormDescription();
            [id(0x0000f03e), propget, helpcontext(0x000005f4)]
            _Inspector* GetInspector();
            [id(0x00000017), propget, helpcontext(0x000005f5)]
            OlImportance Importance();
            [id(0x00000017), propput, helpcontext(0x000005f5)]
            void Importance([in] OlImportance rhs);
            [id(0x00003008), propget, helpcontext(0x000005f6)]
            DATE LastModificationTime();
            [id(0x0000f100), propget, hidden, helpcontext(0x000005f7)]
            IUnknown* MAPIOBJECT();
            [id(0x0000001a), propget, helpcontext(0x000005f8)]
            BSTR MessageClass();
            [id(0x0000001a), propput, helpcontext(0x000005f8)]
            void MessageClass([in] BSTR rhs);
            [id(0x00008534), propget, helpcontext(0x000005f9)]
            BSTR Mileage();
            [id(0x00008534), propput, helpcontext(0x000005f9)]
            void Mileage([in] BSTR rhs);
            [id(0x0000850e), propget, helpcontext(0x000005fa)]
            VARIANT_BOOL NoAging();
            [id(0x0000850e), propput, helpcontext(0x000005fa)]
            void NoAging([in] VARIANT_BOOL rhs);
            [id(0x00008552), propget, helpcontext(0x000005fb)]
            long OutlookInternalVersion();
            [id(0x00008554), propget, helpcontext(0x000005fc)]
            BSTR OutlookVersion();
            [id(0x0000f0a3), propget, helpcontext(0x000005fd)]
            VARIANT_BOOL Saved();
            [id(0x00000036), propget, helpcontext(0x000005fe)]
            OlSensitivity Sensitivity();
            [id(0x00000036), propput, helpcontext(0x000005fe)]
            void Sensitivity([in] OlSensitivity rhs);
            [id(0x00000e08), propget, helpcontext(0x000005ff)]
            long Size();
            [id(0x00000037), propget, helpcontext(0x00000600)]
            BSTR Subject();
            [id(0x00000037), propput, helpcontext(0x00000600)]
            void Subject([in] BSTR rhs);
            [id(0x0000f01c), propget, helpcontext(0x00000601)]
            VARIANT_BOOL UnRead();
            [id(0x0000f01c), propput, helpcontext(0x00000601)]
            void UnRead([in] VARIANT_BOOL rhs);
            [id(0x0000f816), propget, helpcontext(0x00000602)]
            UserProperties* UserProperties();
            [id(0x0000f023), helpcontext(0x00000603)]
            void Close([in] OlInspectorClose SaveMode);
            [id(0x0000f032), helpcontext(0x00000604)]
            IDispatch* Copy();
            [id(0x0000f04a), helpcontext(0x00000605)]
            void Delete();
            [id(0x0000f0a6), helpcontext(0x00000606)]
            void Display([in, optional] VARIANT Modal);
            [id(0x0000f034), helpcontext(0x00000607)]
            IDispatch* Move([in] MAPIFolder* DestFldr);
            [id(0x0000f033), helpcontext(0x00000608)]
            void PrintOut();
            [id(0x0000f048), helpcontext(0x00000609)]
            void Save();
            [id(0x0000f051), helpcontext(0x0000060a)]
            void SaveAs(
                            [in] BSTR Path, 
                            [in, optional] VARIANT Type);
            [id(0x00000015), propget, helpcontext(0x0000060b)]
            DATE ExpiryTime();
            [id(0x00000015), propput, helpcontext(0x0000060b)]
            void ExpiryTime([in] DATE rhs);
            [id(0x0000f404), propget, helpcontext(0x0000060c)]
            BSTR HTMLBody();
            [id(0x0000f404), propput, helpcontext(0x0000060c)]
            void HTMLBody([in] BSTR rhs);
            [id(0x00000e06), propget, helpcontext(0x0000060d)]
            DATE ReceivedTime();
            [id(0x00000c1a), propget, helpcontext(0x0000060e)]
            BSTR SenderName();
            [id(0x00000039), propget, helpcontext(0x0000060f)]
            DATE SentOn();
            [id(0x0000f822), helpcontext(0x00000610)]
            void ClearConversationIndex();
            [id(0x0000f813), helpcontext(0x00000611)]
            MailItem* Forward();
            [id(0x0000f075), helpcontext(0x00000612)]
            void Post();
            [id(0x0000f810), helpcontext(0x00000613)]
            MailItem* Reply();
            [id(0x0000f405), propget, helpcontext(0x00000614)]
            Links* Links();
            [id(0x0000fa09), propget, helpcontext(0x00000615)]
            ItemProperties* ItemProperties();
            [id(0x0000fa49), propget, helpcontext(0x00000616)]
            OlBodyFormat BodyFormat();
            [id(0x0000fa49), propput, helpcontext(0x00000616)]
            void BodyFormat([in] OlBodyFormat rhs);
            [id(0x0000fa4d), propget, helpcontext(0x00000617)]
            OlDownloadState DownloadState();
            [id(0x0000fa0b), helpcontext(0x00000618)]
            void ShowCategoriesDialog();
            [id(0x00003fde), propget, helpcontext(0x00000619)]
            long InternetCodepage();
            [id(0x00003fde), propput, helpcontext(0x00000619)]
            void InternetCodepage([in] long rhs);
            [id(0x00008571), propget, helpcontext(0x0000061a)]
            OlRemoteStatus MarkForDownload();
            [id(0x00008571), propput, helpcontext(0x0000061a)]
            void MarkForDownload([in] OlRemoteStatus rhs);
            [id(0x0000faa3), hidden, helpcontext(0x0000061b)]
            VARIANT_BOOL SetACLs();
            [id(0x0000faa4), propget, helpcontext(0x0000061c)]
            VARIANT_BOOL IsConflict();
            [id(0x0000faba), propget, helpcontext(0x0000061d)]
            VARIANT_BOOL AutoResolvedWinner();
            [id(0x0000fabb), propget, helpcontext(0x0000061e)]
            Conflicts* Conflicts();
            [id(0x00000c1f), propget, helpcontext(0x0000061f)]
            BSTR SenderEmailAddress();
            [id(0x00000c1e), propget, helpcontext(0x00000620)]
            BSTR SenderEmailType();
            [id(0x0000fafd), propget, helpcontext(0x00000621)]
            PropertyAccessor* PropertyAccessor();
            [id(0x0000fc1f), propget, helpcontext(0x00000623)]
            BSTR TaskSubject();
            [id(0x0000fc1f), propput, helpcontext(0x00000623)]
            void TaskSubject([in] BSTR rhs);
            [id(0x00008105), propget, helpcontext(0x00000624)]
            DATE TaskDueDate();
            [id(0x00008105), propput, helpcontext(0x00000624)]
            void TaskDueDate([in] DATE rhs);
            [id(0x00008104), propget, helpcontext(0x00000625)]
            DATE TaskStartDate();
            [id(0x00008104), propput, helpcontext(0x00000625)]
            void TaskStartDate([in] DATE rhs);
            [id(0x0000810f), propget, helpcontext(0x00000626)]
            DATE TaskCompletedDate();
            [id(0x0000810f), propput, helpcontext(0x00000626)]
            void TaskCompletedDate([in] DATE rhs);
            [id(0x000085a0), propget, helpcontext(0x00000be2)]
            DATE ToDoTaskOrdinal();
            [id(0x000085a0), propput, helpcontext(0x00000be2)]
            void ToDoTaskOrdinal([in] DATE rhs);
            [id(0x0000851c), propget, helpcontext(0x00000628)]
            VARIANT_BOOL ReminderOverrideDefault();
            [id(0x0000851c), propput, helpcontext(0x00000628)]
            void ReminderOverrideDefault([in] VARIANT_BOOL rhs);
            [id(0x0000851e), propget, helpcontext(0x00000629)]
            VARIANT_BOOL ReminderPlaySound();
            [id(0x0000851e), propput, helpcontext(0x00000629)]
            void ReminderPlaySound([in] VARIANT_BOOL rhs);
            [id(0x00008503), propget, helpcontext(0x0000062a)]
            VARIANT_BOOL ReminderSet();
            [id(0x00008503), propput, helpcontext(0x0000062a)]
            void ReminderSet([in] VARIANT_BOOL rhs);
            [id(0x0000851f), propget, helpcontext(0x0000062b)]
            BSTR ReminderSoundFile();
            [id(0x0000851f), propput, helpcontext(0x0000062b)]
            void ReminderSoundFile([in] BSTR rhs);
            [id(0x00008502), propget, helpcontext(0x0000062c)]
            DATE ReminderTime();
            [id(0x00008502), propput, helpcontext(0x0000062c)]
            void ReminderTime([in] DATE rhs);
            [id(0x0000fbfe), helpcontext(0x00000be3)]
            void MarkAsTask([in] OlMarkInterval MarkInterval);
            [id(0x0000fc09), helpcontext(0x00000ca4)]
            void ClearTaskFlag();
            [id(0x0000fc0a), propget, helpcontext(0x00000be5)]
            VARIANT_BOOL IsMarkedAsTask();
    };
    
    
    
    [
      uuid(0006302B-0000-0000-C000-000000000046),
      helpcontext(0x000001d9)
    ]
    dispinterface ItemEvents_10 {
        properties:
        methods:
            [id(0x0000f003), helpcontext(0x000001da)]
            HRESULT Open([in, out] VARIANT_BOOL* Cancel);
            [id(0x0000f006), helpcontext(0x000001db)]
            HRESULT CustomAction(
                            [in] IDispatch* Action, 
                            [in] IDispatch* Response, 
                            [in, out] VARIANT_BOOL* Cancel);
            [id(0x0000f008), helpcontext(0x000001dc)]
            HRESULT CustomPropertyChange([in] BSTR Name);
            [id(0x0000f468), helpcontext(0x000001dd)]
            HRESULT Forward(
                            [in] IDispatch* Forward, 
                            [in, out] VARIANT_BOOL* Cancel);
            [id(0x0000f004), helpcontext(0x000001de)]
            HRESULT Close([in, out] VARIANT_BOOL* Cancel);
            [id(0x0000f009), helpcontext(0x000001df)]
            HRESULT PropertyChange([in] BSTR Name);
            [id(0x0000f001), helpcontext(0x000001e0)]
            HRESULT Read();
            [id(0x0000f466), helpcontext(0x000001e1)]
            HRESULT Reply(
                            [in] IDispatch* Response, 
                            [in, out] VARIANT_BOOL* Cancel);
            [id(0x0000f467), helpcontext(0x000001e2)]
            HRESULT ReplyAll(
                            [in] IDispatch* Response, 
                            [in, out] VARIANT_BOOL* Cancel);
            [id(0x0000f005), helpcontext(0x000001e3)]
            HRESULT Send([in, out] VARIANT_BOOL* Cancel);
            [id(0x0000f002), helpcontext(0x000001e4)]
            HRESULT Write([in, out] VARIANT_BOOL* Cancel);
            [id(0x0000f00a), helpcontext(0x000001e5)]
            HRESULT BeforeCheckNames([in, out] VARIANT_BOOL* Cancel);
            [id(0x0000f00b), helpcontext(0x000001e6)]
            HRESULT AttachmentAdd([in] Attachment* Attachment);
            [id(0x0000f00c), helpcontext(0x000001e7)]
            HRESULT AttachmentRead([in] Attachment* Attachment);
            [id(0x0000f00d), helpcontext(0x000001e8)]
            HRESULT BeforeAttachmentSave(
                            [in] Attachment* Attachment, 
                            [in, out] VARIANT_BOOL* Cancel);
            [id(0x0000fa75), helpcontext(0x000001e9)]
            void BeforeDelete(
                            [in] IDispatch* Item, 
                            [in, out] VARIANT_BOOL* Cancel);
            [id(0x0000fbae), helpcontext(0x000001ea)]
            void AttachmentRemove([in] Attachment* Attachment);
            [id(0x0000fbb0), helpcontext(0x000001eb)]
            void BeforeAttachmentAdd(
                            [in] Attachment* Attachment, 
                            [in, out] VARIANT_BOOL* Cancel);
            [id(0x0000fbaf), helpcontext(0x000001ec)]
            void BeforeAttachmentPreview(
                            [in] Attachment* Attachment, 
                            [in, out] VARIANT_BOOL* Cancel);
            [id(0x0000fbab), helpcontext(0x000001ed)]
            void BeforeAttachmentRead(
                            [in] Attachment* Attachment, 
                            [in, out] VARIANT_BOOL* Cancel);
            [id(0x0000fbb2), helpcontext(0x000001ee)]
            void BeforeAttachmentWriteToTempFile(
                            [in] Attachment* Attachment, 
                            [in, out] VARIANT_BOOL* Cancel);
            [id(0x0000fbad), helpcontext(0x000001ef)]
            void Unload();
            [id(0x0000fc02), helpcontext(0x000001f0)]
            void BeforeAutoSave([in, out] VARIANT_BOOL* Cancel);
    };
    Best regards,
    Igor

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to send attachment with MS Outlook automation?

    Create an Attachments object from the mailitem object
    Code:
      Attachments atts( myMailItemObject.GetAttachments() ;
    then for each file you want to attach, add it to the Attachments object

    Code:
       atts.Add( COleVariant("c:\filename.txt"), COleVariant((long)olByValue), COleVariant((long)DISP_E_PARAMNOTFOUND, VT_ERROR)/*==optional*/, COleVariant("Display name of the file"));

  4. #4
    Join Date
    May 2002
    Posts
    1,798

    Re: How to send attachment with MS Outlook automation?

    Thanks OReubens. I came across the same post somewhere. But it's an old post that doesnt appear to work using Win 7, Microsoft Outlook 12.0 Object Library <9.3> where I have added the classes from the Class Wizard:

    CApplication
    CAppointmentItem
    CContactItem
    CMailItem
    CMainSpace

    But trying to instantiate the object Attachments
    Code:
    Attachments atts( myMailItemObject.GetAttachments() ;
    is accompanied by the message:
    Error: object of abstract class type "Attachments" is not allowed
    I suspect that M$ made some changes in the OutLook automation classes and interfaces, e.g., myMailItemObject.GetAttachments() is now myMailItemObject.get_Attachements(). Further, there exist interfaces in the type library for Attachments which might be useful but I would prefer that someone who has used them might share their code rather than my endless fooling around with it.

    This post addresses the problem more directly, particularly with respect to earlier vs later versions of Visual Studio.

    http://social.msdn.microsoft.com/For...8-6205326f065d

    However, some here have expressed the opinion that social.msdn.microsoft.com/Forums are worth what you pay for them. : )

    OK. I added Attachment and Attachments from the Class Wizard type library and now I can instantiate a CAttachments object. But how to add file objects to this - given that I have a CString full pathname for the file?

    Code:
    LPDISPATCH atts.Add(VARIANT &Source, VARIANT &Type, VARIANT &Postion, VARIANT &DisplayName);
    I have tried this but clearly lack understanding of COM where m_vcsAttachments is a std::vector<CString> pathname:
    Code:
    	CAttachments atts;
    	COleVariant covOption((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    
                  // Using the CComVariant: Smart Wrapper Class for VARIANT
    	CComVariant varSmartVariantString (m_vcsAttachments[0]);
    
    	//LPDISPATCH atts.Add(VARIANT &Source, VARIANT &Type, VARIANT &Postion, VARIANT &DisplayName);
    
    	atts.Add(varSmartVariantString,
    	covOption,
    	covOption,
    	covOption);
    
    	olMailItem.get_Attachments();
    
    	// Send the message
    	olMailItem.Send();
    Thanks to Siddartha: http://forums.codeguru.com/showthrea...ing-to-CString

    And now it compiles and runs and sends valid emails - BUT NO ATTACHMENTS ARE RECEIVED WITH THE EMAIL!

    I must be missing something.
    Still would appreciate your help on this.
    Last edited by Mike Pliam; January 11th, 2013 at 07:32 PM.
    mpliam

  5. #5
    Join Date
    May 2002
    Posts
    1,798

    Re: How to send attachment with MS Outlook automation?

    Found a method that works. Turns out you do need both Attachment and Attachments interfaces from the Class Wizard type library.
    The following code adopted from this site works to send an email with an attachment.
    see: http://us.generation-nt.com/answer/s...-49590791.html
    Code:
    	CAttachments atts;
    
    	 //COleVariant oAttachLocation(oStr);
    	 COleVariant oAttachLocation(m_vcsAttachments[0]);
    	 COleVariant covOption((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    	 CAttachments olAths = olMailItem.get_Attachments();
     	 CAttachment olNewAth = olAths.Add(oAttachLocation,
    	 covOption,
    	 covOption,
    	 covOption);
    
    	// Send the message
    	olMailItem.Send();
    I'll need to do some extensive testing to see which types of files are acceptable and how many attachments including images, pdf, txt, etc will go through. I've posted this painful step by step in the hope that it will help some others.

    BTW Igor, I use the Microsoft Outlook type library as a wrapper for all that COM code which frankly makes me a bit dizzy whenever I see it. But thanks for your input which I always appreciate. : )
    Last edited by Mike Pliam; January 11th, 2013 at 08:01 PM.
    mpliam

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How to send attachment with MS Outlook automation?

    Quote Originally Posted by Mike Pliam View Post
    BTW Igor, I use the Microsoft Outlook type library as a wrapper for all that COM code which frankly makes me a bit dizzy whenever I see it.
    Well, it's just a matter of habit and practice. I find viewing OLB/TLB content in OleView extremely useful for getting certain object model hints that may help you to start thinking in right direction. In case the type library is huge enough, I grab pure IDL and do some simple text search on it to find the points/words I'm interested in. And when I have a substantial and precise information about coclasses and methods, I start to look for documentation/articles/forums/etc. And making my little tests of course.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured