Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
3.86/5 (7 votes)
See more:
I'm creating an ATL project and creating class so that i have implemented the _MailItem interface for that class. I m not able to access those interface functions such as get_Attachments, get_CC etc...

_IDTExtensibility2 methods can able to access but _MailItem methods not able to access. I want to access mail items when sending mail so that i need to get body, To, Cc, subject & attachments of mail from outlook before sending mail using C++?

How to access these functions using C++?
Thanks in advance Swathi

Here is my Code:

C++
#pragma once
#include "resource.h"       // main symbols

#include "NativeAddIn_i.h"


#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif

// CConnect

class ATL_NO_VTABLE CConnect :
    public CComObjectRootEx<ccomsinglethreadmodel>,
    public CComCoClass<cconnect,>,
    public IDispatchImpl,
    public IDispatchImpl<_IDTExtensibility2, &__uuidof(_IDTExtensibility2), &LIBID_AddInDesignerObjects, /* wMajor = */ 1>,
    public IDispatchImpl<_Application, &__uuidof(_Application), &LIBID_Outlook, /* wMajor = */ 9, /* wMinor = */ 3>,
    public IDispatchImpl<_MailItem, &__uuidof(_MailItem), &LIBID_Outlook, /* wMajor = */ 9, /* wMinor = */ 3>
{
public:
    CConnect()
    {
    }

    DECLARE_REGISTRY_RESOURCEID(IDR_CONNECT)

    BEGIN_COM_MAP(CConnect)
        COM_INTERFACE_ENTRY(IConnect)
        COM_INTERFACE_ENTRY2(IDispatch, _IDTExtensibility2)
        COM_INTERFACE_ENTRY(_IDTExtensibility2)
        COM_INTERFACE_ENTRY(_Application)
        COM_INTERFACE_ENTRY(_MailItem)
    END_COM_MAP()

    DECLARE_PROTECT_FINAL_CONSTRUCT()

    HRESULT FinalConstruct()
    {
        return S_OK;
    }

    void FinalRelease() { }

    // _IDTExtensibility2 Methods
public:
    STDMETHOD(OnConnection)(LPDISPATCH Application, ext_ConnectMode ConnectMode, LPDISPATCH AddInInst, SAFEARRAY * * custom) { MessageBoxW(NULL,L"OnConnection",L"Native Addin",MB_OK); return S_OK; } 
    STDMETHOD(OnDisconnection)(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom) { return  S_OK; } STDMETHOD(OnAddInsUpdate)(SAFEARRAY * * custom) { return  S_OK; }
    STDMETHOD(OnStartupComplete)(SAFEARRAY * * custom) { return  S_OK; }
    STDMETHOD(OnBeginShutdown)(SAFEARRAY * * custom) { return  S_OK; }  

    // _MailItem Methods
public:
    // Duplicate Method : HRESULT get_Application(_Application * *)
    // Duplicate Method : HRESULT get_Class(OlObjectClass *)
    // Duplicate Method : HRESULT get_Session(_NameSpace * *)
    // Duplicate Method : HRESULT get_Parent(LPDISPATCH *)
    STDMETHOD ( get_Actions                           )  ( Actions * * Actions                              )  { MessageBoxW    ( NULL,L"get_Actions",L"Native Addin",MB_OK     ) ; return S_OK; }
    STDMETHOD ( get_Attachments )  ( Attachments * * Attachments )  { MessageBoxW    ( NULL,L"get_Attachments",L"Native Addin",MB_OK ) ; return S_OK; }
    STDMETHOD ( get_BillingInformation )  ( BSTR * BillingInformation )  { return S_OK; }
    STDMETHOD ( put_BillingInformation )  ( BSTR BillingInformation )  { return S_OK; }
    STDMETHOD ( get_Body )  ( BSTR * Body )  { MessageBoxW(NULL,L"get_Body",L"Native Addin",MB_OK ) ; return S_OK; }
    STDMETHOD ( put_Body )  ( BSTR Body )  { MessageBoxW  ( NULL,L"get_Attachments",L"Native Addin",MB_OK ) ; return S_OK; }
    STDMETHOD ( get_Categories )  ( BSTR * Categories )  { return S_OK; }
    STDMETHOD ( put_Categories )  ( BSTR Categories )  { return S_OK; }
    STDMETHOD ( get_Companies )  ( BSTR * Companies )  { return S_OK; }
    STDMETHOD ( put_Companies )  ( BSTR Companies )  { return S_OK; }

    STDMETHOD ( get_Mileage )  ( BSTR * Mileage )  { return S_OK; }
    STDMETHOD ( put_Mileage )  ( BSTR Mileage )  { return S_OK; }
    STDMETHOD ( get_NoAging )  ( VARIANT_BOOL * NoAging )  { return S_OK; }
    STDMETHOD ( put_NoAging )  ( VARIANT_BOOL NoAging )  { return S_OK; }
    STDMETHOD ( get_OutlookInternalVersion )  ( long * OutlookInternalVersion )  { return S_OK; }
    STDMETHOD ( get_OutlookVersion )  ( BSTR * OutlookVersion )  { return S_OK; }
    STDMETHOD ( get_Saved )  ( VARIANT_BOOL * Saved )  { return S_OK; }
    STDMETHOD ( get_Sensitivity )  ( OlSensitivity * Sensitivity )  { return S_OK; }
    STDMETHOD ( put_Sensitivity )  ( OlSensitivity Sensitivity )  { return S_OK; }
    STDMETHOD ( get_Size )  ( long * Size )  { return S_OK; }
    STDMETHOD ( get_Subject )  ( BSTR * Subject )  { MessageBoxW(_Subject",L"NativeAddin",MB_OK  ) ;  return S_OK; }
    STDMETHOD ( put_Subject )  ( BSTR Subject)  { return S_OK; }
    STDMETHOD ( get_UnRead)  ( VARIANT_BOOL * UnRead)  { return S_OK; }
    STDMETHOD ( put_UnRead)  ( VARIANT_BOOL UnRead )  { return S_OK; }
    STDMETHOD ( get_UserProperties )  ( UserProperties * * UserProperties  )  { return S_OK; }
    STDMETHOD ( Close )  ( OlInspectorClose SaveMode )  { return S_OK; }
    STDMETHOD ( Copy  )  ( LPDISPATCH * Item )  { return S_OK; }
    STDMETHOD ( Delete)  ()  { return S_OK; }
    STDMETHOD ( Display )  ( VARIANT Modal )  { return S_OK; }
    STDMETHOD ( Move )  ( MAPIFolder * DestFldr, LPDISPATCH * Item )  { return S_OK; }
    STDMETHOD ( PrintOut)  ()  { return S_OK; }
    STDMETHOD ( Save )  ( )  { return S_OK; }
    STDMETHOD ( SaveAs)  ( BSTR Path, VARIANT Type )  { return S_OK; }

    STDMETHOD ( get_BCC)  ( BSTR * BCC)  { return S_OK; }
    STDMETHOD ( put_BCC)  ( BSTR BCC )  { MessageBoxW    ( NULL,L"get_Attachments",L"Native Addin",MB_OK ) ; return S_OK; }
    STDMETHOD ( get_CC )  ( BSTR * CC )  { return S_OK; }
    STDMETHOD ( put_CC )  ( BSTR CC )  { return S_OK; }

    STDMETHOD ( get_HTMLBody )  ( BSTR * HTMLBody )  { return S_OK; }
    STDMETHOD ( put_HTMLBody )  ( BSTR HTMLBody )  { return S_OK; }
    STDMETHOD ( get_OriginatorDeliveryReportRequested )  ( VARIANT_BOOL * OriginatorDeliveryReportRequested )  { return S_OK; }
    STDMETHOD ( put_OriginatorDeliveryReportRequested )  ( VARIANT_BOOL OriginatorDeliveryReportRequested   )  { return S_OK; }
    STDMETHOD ( get_ReadReceiptRequested )  ( VARIANT_BOOL * ReadReceiptRequested)  { return S_OK; }

    STDMETHOD ( put_RemoteStatus )  ( OlRemoteStatus RemoteStatus)  { return S_OK; }
    STDMETHOD ( get_ReplyRecipientNames)  ( BSTR * ReplyRecipientNames                       )  { return S_OK; }
    STDMETHOD ( get_ReplyRecipients )  ( Recipients * * ReplyRecipients                   )  { return S_OK; }
    STDMETHOD ( get_SaveSentMessageFolder )  ( MAPIFolder * * SaveSentMessageFolder             )  { return S_OK; }
    STDMETHOD ( putref_SaveSentMessageFolder )  ( MAPIFolder * SaveSentMessageFolder               )  { return S_OK; }

    STDMETHOD ( get_To )  ( BSTR * To )  { return S_OK; }
    STDMETHOD ( put_To )  ( BSTR To )  { return S_OK; }
    STDMETHOD ( get_VotingOptions )  ( BSTR * VotingOptions )  { return S_OK; }
    STDMETHOD ( put_VotingOptions )  ( BSTR VotingOptions )  { return S_OK; }
    STDMETHOD ( get_VotingResponse )  ( BSTR * VotingResponse                            )  { return S_OK; }
    STDMETHOD ( put_VotingResponse )  ( BSTR VotingResponse )  { return S_OK; }
    STDMETHOD ( ClearConversationIndex )  (                                                  )  { return S_OK; }
    STDMETHOD ( Forward )  ( _MailItem * * Item )  { return S_OK; }
    STDMETHOD ( Reply )  ( _MailItem * * Item )  { return S_OK; }
    STDMETHOD ( ReplyAll )  ( _MailItem * * Item )  { return S_OK; }
    STDMETHOD ( Send )  ()  { return S_OK; }
    STDMETHOD ( get_Links )  ( Links * * Links )  { return S_OK; }
    STDMETHOD ( get_ItemProperties )  ( ItemProperties * * ItemProperties)  { return S_OK; }
    STDMETHOD ( get_BodyFormat )  ( OlBodyFormat * BodyFormat )  { return S_OK; }
    STDMETHOD ( put_BodyFormat )  ( OlBodyFormat BodyFormat )  { return S_OK; }
    STDMETHOD ( get_DownloadState )  ( OlDownloadState * DownloadState )  { return S_OK; }
    STDMETHOD ( ShowCategoriesDialog )  ()  { return S_OK; }
    STDMETHOD ( get_InternetCodepage )  ( long * InternetCodepage)  { return S_OK; }
    STDMETHOD ( put_InternetCodepage )  ( long InternetCodepage)  { return S_OK; }
    STDMETHOD ( get_MarkForDownload )  ( OlRemoteStatus * MarkForDownload                 )  { return S_OK; }
    STDMETHOD ( put_MarkForDownload )  ( OlRemoteStatus MarkForDownload )  { return S_OK; }
    STDMETHOD ( get_IsConflict )  ( VARIANT_BOOL * IsConflict )  { return S_OK; }
    STDMETHOD ( get_IsIPFax )  ( VARIANT_BOOL * IsIPFax )  { return S_OK; }
    STDMETHOD ( put_IsIPFax )  ( VARIANT_BOOL IsIPFax )  { return S_OK; }
    STDMETHOD ( get_FlagIcon )  ( OlFlagIcon * FlagIcon )  { return S_OK; }
    STDMETHOD ( put_FlagIcon )  ( OlFlagIcon FlagIcon )  { return S_OK; }
    STDMETHOD ( get_HasCoverSheet )  ( VARIANT_BOOL * HasCoverSheet)  { return S_OK; }
    STDMETHOD ( put_HasCoverSheet )  ( VARIANT_BOOL HasCoverSheet)  { return S_OK; }
    STDMETHOD ( get_AutoResolvedWinner )  ( VARIANT_BOOL * AutoResolvedWinner                )  { return S_OK; }
    STDMETHOD ( get_Conflicts )  ( Conflicts * * Conflicts )  { return S_OK; }
    STDMETHOD ( get_SenderEmailAddress )  ( BSTR * SenderEmailAddress )  { return S_OK; }
    STDMETHOD ( get_SenderEmailType )  ( BSTR * SenderEmailType ) { return S_OK; }
    STDMETHOD ( get_EnableSharedAttachments )  ( VARIANT_BOOL * EnableSharedAttachments  )  { return S_OK; }
    STDMETHOD ( put_EnableSharedAttachments)  ( VARIANT_BOOL EnableSharedAttachments             )  { return S_OK; }
    STDMETHOD ( get_Permission )  ( OlPermission * Permission )  { return S_OK; }
    STDMETHOD ( put_Permission )  ( OlPermission Permission )  { return S_OK; }
    STDMETHOD ( get_PermissionService )  ( OlPermissionService * PermissionService          )  { return S_OK; }
    STDMETHOD ( put_PermissionService )  ( OlPermissionService PermissionService            )  { return S_OK; }
    STDMETHOD ( get_PropertyAccessor )  ( _PropertyAccessor * * PropertyAccessor           )  { return S_OK; }
    STDMETHOD ( AddBusinessCard )  ( _ContactItem * contact )  { return S_OK; }
    STDMETHOD ( get_SendUsingAccount)  ( _Account * * SendUsingAccount )  { return S_OK; }
    STDMETHOD ( put_SendUsingAccount )  ( _Account * SendUsingAccount)  { return S_OK; }
    STDMETHOD ( get_TaskSubject )  ( BSTR * TaskSubject )  { return S_OK; }

};

OBJECT_ENTRY_AUTO(__uuidof(Connect), CConnect)
Posted
Updated 31-Aug-12 0:12am
v4
Comments
Legor 31-Aug-12 3:15am    
Please do not just dump code here. Only show releveant parts of your code and poeple will probably look at them. And please format so its better readable.
Slacker007 31-Aug-12 6:11am    
I don't think this is just a code dump. A legitimate question has been posted.
Swathi Joshi 31-Aug-12 6:34am    
If u have any idea on this pls help me...
Swathi Joshi 31-Aug-12 6:36am    
For this code there is no any compilation error but the message boxes on MailItem interface not displaying anything

You dont need to implement Application and MailItem interfaces.
Instead You have to use existing Outlook interfaces.

To access mail items before sending mail You have to subscribe on Outlook::ItemEvents calling IDispEventSimpleImpl<...>::DispEventAdvise function.

Header File
C++
extern _ATL_FUNC_INFO	fiMailItemEvents;

class ATL_NO_VTABLE CConnect :
    public CComObjectRootEx<ccomsinglethreadmodel>,
    public CComCoClass<cconnect,>,
    public IDispatchImpl,
    public IDispatchImpl<_IDTExtensibility2, &__uuidof(_IDTExtensibility2), &LIBID_AddInDesignerObjects, /* wMajor = */ 1>,
    public IDispEventSimpleImpl<1, CConnect, &__uuidof(Outlook::ItemEvents)>
{
    ....
    VOID WINAPI OnSend(
		VARIANT_BOOL		*Cancel);

    BEGIN_SINK_MAP(CConnect)
	SINK_ENTRY_INFO(1, __uuidof(Outlook::ItemEvents),
		0x0000F005, OnSend, &fiMailItemEvents)
    END_SINK_MAP()
}


Source File
C++
_ATL_FUNC_INFO	fiMailItemEvents = 
	{CC_STDCALL, VT_EMPTY, 1, {VT_BOOL|VT_BYREF}};

VOID WINAPI CConnect::OnSend(
    VARIANT_BOOL		*Cancel)
{
    ....
}
 
Share this answer
 
v3
C++
void __stdcall COAddin::OnSend(IDispatch * Item, bool Cancel)    
{
	MessageBoxA(NULL, "On Send Clicked", "Info", MB_OK);

	CComPtr<outlook::_mailitem xmlns:outlook="#unknown"> mail;

	HRESULT hr;

	if (Item->QueryInterface(IID__MailItem,(void**)&mail) == S_OK)
	{
		IUnknown* mapiItem;
		if (mail->get_MAPIOBJECT(&mapiItem) == S_OK)
		{

			BSTR subj;
			mail->get_Subject(&subj);
			MessageBoxW(NULL, (LPCWSTR)subj, L"Subject", MB_OK);
		}

	}
	else
	{
		MessageBoxA(NULL, "QueryInterface failed", "Info", MB_OK);
		return;
	}
	MessageBoxA(NULL, "On Send Clicked succeeded", "Info", MB_OK);


	CComPtr<outlook::attachments> spAttachments;
	CComPtr<outlook::attachment> spAttachment;

	BSTR bstrPathName;
	ATLASSERT(mail);

	VARIANT index;
	index.iVal = 1; // value
	index.vt = VT_I2; // type of variant = integer

	mail->get_Attachments(&spAttachments);
	ATLASSERT(spAttachments);
	long count;

	spAttachments->get_Count(&count);
	char buf[10] = {0};
	itoa ( count, buf, 10 );
	MessageBoxW(NULL, (LPCWSTR)buf, L"count", MB_OK);
	
	if (count > 0) 
	{
		spAttachments->Item(index, &spAttachment);
		ATLASSERT(spAttachment);
	
		spAttachment->get_FileName(&bstrPathName);
		MessageBoxW(NULL, (LPCWSTR)bstrPathName, L"Info", MB_OK);

		spAttachment->get_PathName(&bstrPathName);
	        MessageBoxW(NULL, (LPCWSTR)bstrPathName, L"Info", MB_OK);

	}
}

Now from this code i m getting suject, to, cc, bcc and also the attached file name but i m not getting the attached file path from get_PathName function, its showing blank message box... How to get the attached file path from this.. let me know if there is any other way to get the file path and also how to get the file names and attachments path if there is more than one attachment..Pls help me in this..

Thanks in advance
 
Share this answer
 
v3
Comments
Swathi Joshi 5-Sep-12 5:31am    
Now from this code i m getting suject, to, cc, bcc and also the attached file name but i m not getting the attached file path from get_PathName function, its showing blank message box... How to get the attached file path from this.. let me know if there is any other way to get the file path and also how to get the file names and attachments path if there is more than one attachment..Pls help me in this..

Thanks in advance
cariolihome 7-Sep-12 17:02pm    
You have to use Attachment::SaveAsFile method to get attachment contents
(http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.attachment_members.aspx)
Please follow below steps: you will able to retrieve:


//In Class that defines addin inteface
public IDispEventSimpleImpl<17, CAddin, &__uuidof(Outlook::ApplicationEvents_11)>


_ATL_FUNC_INFO OnNewMail = {CC_STDCALL, VT_EMPTY, 1, {VT_BSTR}};

SINK_ENTRY_INFO(17, __uuidof(Outlook::ApplicationEvents_11), 0xfab5, OnNewMailEx, &OnNewMail)

//Header file function declaration
STDMETHOD_(void, OnNewMailEx)(BSTR bstrEntryID);


//CPP file function definition
STDMETHODIMP_(void) CAddin::OnNewMailEx( BSTR bstrEntryID )
{
ATLTRACE("NewMail Entryid is %S\n", bstrEntryID);
CComPtr<outlook::_namespace xmlns:outlook="#unknown"> pNameSpace;
HRESULT hr = E_FAIL;


hr = m_pApp->get_Session( &pNameSpace );

if( pNameSpace )
{
CComPtr<outlook::_store> pStore;

hr = pNameSpace->get_DefaultStore( &pStore );

if( pStore )
{
CComBSTR storeID;
CComVariant vStore;
CComPtr<idispatch> pItemDisp;
CComQIPtr<outlook::_mailitem> pMailItem;

hr = pStore->get_StoreID( &storeID );

vStore = storeID;

hr = pNameSpace->GetItemFromID( bstrEntryID, vStore, &pItemDisp);

pMailItem = pItemDisp;

if(pMailItem)
hr = pMailItem->Display();
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900