Sending a Message (with Attachments) with OLEDB and CDOEX
Topic Last Modified: 2006-06-12
Visual C++
Note
The following example uses a file URL with the Exchange OLE DB (ExOLEDB) provider. The ExOLEDB provider also supports The HTTP: URL Scheme. Using The HTTP: URL Scheme allows both client and server applications to use a single URL scheme.
//Sending a Message (w/Attachments) using OLEDB and CDOEx
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <activeds.h>
#import <msado15.dll> no_namespace rename("EOF","adoEOF")
#import <cdoex.dll> no_namespace
struct StartOle
{
StartOle() { CoInitialize(NULL); }
~StartOle() { CoUninitialize(); }
} _inst_StartOle;
HRESULT GetDomainName(BSTR* bstrDomainName);
void main(void)
{
LPSTR lpUser = "User1";
LPSTR lpAttachment ="C:\\hello.txt";
_ConnectionPtr conn(_uuidof(Connection));
_RecordsetPtr MyInbox(_uuidof(Recordset));
HRESULT hRes = NULL;
BSTR bstrDomainDNSName;
// Get your Domain Name.
hRes = GetDomainName(&bstrDomainDNSName);
try
{
conn->Provider = "Exoledb.DataSource";
_bstr_t strConnString = "file://./backofficestorage/" + (_bstr_t)bstrDomainDNSName + "/MBX/" + (_bstr_t)lpUser + "/Inbox";
printf(strConnString);
hRes = conn->Open(strConnString,"","",0);
if (conn->State == adStateOpen)
printf("Connection Opened!\n");
else
printf("Connection failed!\n");
_bstr_t strQ = "select * from scope ('shallow traversal of \""+ strConnString + "\"') " ;
hRes = MyInbox->Open(strQ,conn->ConnectionString,adOpenForwardOnly,adLockReadOnly,0);
IConfigurationPtr iConf( __uuidof(Configuration));
FieldsPtr Flds = iConf->GetFields();
FieldPtr Fld = Flds->GetItem("https://schemas.microsoft.com/cdo/configuration/sendusing");
Fld->Value = "cdoSendUsingExchange";
printf("Configuration object created\n");
IMessagePtr iMsg(__uuidof(Message));
iMsg->put_Configuration(iConf);
iMsg->put_From("User1@" + (_bstr_t)bstrDomainDNSName);
iMsg->put_To("User2@" + (_bstr_t)bstrDomainDNSName);
iMsg->put_Subject(L"Test Subject");
iMsg->put_TextBody(L"Test Body");
iMsg->AddAttachment( lpAttachment,"","");
iMsg->Send();
printf("Mail Sent\n");
}
catch(...)
{
printf("Something failed!\n");
}
conn = NULL;
}
/*******************************************
// GetDomainName
//
// Params: [out] BSTR * bstrDomainName
// Output: HRESULT
// Purpose: Retrieve the Domain DNS name.
********************************************/
HRESULT GetDomainName(BSTR * bstrDomainName)
{
HRESULT hr = S_OK;
IADsADSystemInfo *pADsys;
hr = CoCreateInstance( CLSID_ADSystemInfo,
NULL,
CLSCTX_INPROC_SERVER,
IID_IADsADSystemInfo,
(void**)&pADsys);
hr = pADsys->get_DomainDNSName(bstrDomainName);
if (pADsys)
pADsys->Release();
return hr;
}