Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sending email using CDO and C++

1,060 views
Skip to first unread message

geogauci

unread,
Jul 10, 2009, 7:54:26 AM7/10/09
to
Hi. I've been trying to send an email using CDO (Collaboration Data
Objects) using C++. I found the code listed below on
http://msdn.microsoft.com/en-us/library/aa487412%28EXCHG.65%29.aspx,
but I haven't got it to work. I modified what I thought where errors
(marked as bold) but now I'm getting a runtime error. I'm using
VSPro2008 on WindowsServer2008. The error location is shown in the
code.

Btw, the belowmentioned dlls where successfully located, and CDO and
SMTP server have been installed.

#import "c:\program files\common files\system\ado\msado15.dll"
no_namespace raw_interfaces_only
#import "c:\windows\system32\cdosys.dll" no_namespace
raw_interfaces_only
#include "cdosysstr.h"
#include "cdosyserr.h"
#include <atlbase.h>
#include <atlimpl.cpp>

int main( )
{

CoInitialize(NULL);
HRESULT hr = S_OK;
{
// Create the Configuration object.
CComPtr<IConfiguration> pConf;
hr = pConf.CoCreateInstance(L"CDO.Configuration");

CComPtr<Fields> pFields;
hr = pConf->get_Fields(&pFields); //was pflds

CComPtr<Field> pfld;
hr = pFields->get_Item(CComVariant(cdoSMTPServer),&pfld);
hr = pfld->put_Value(CComVariant("mailserver"));

hr = pFields->get_Item(CComVariant(cdoSMTPServerPort),&pfld); // <--
Error
hr = pfld->put_Value(CComVariant((long)67));

hr = pFields->get_Item(CComVariant(cdoSMTPAccountName),&pfld);
hr = pfld->put_Value(CComVariant("George"));

hr = pFields->get_Item(CComVariant(cdoSendEmailAddress),&pfld);
hr = pfld->put_Value(CComVariant("\"MySelf\" <mys...@example.com>"));

hr = pFields->get_Item(CComVariant(cdoSMTPAuthenticate),&pfld);
hr = pfld->put_Value(CComVariant((long)cdoBasic));

hr = pFields->get_Item(CComVariant(cdoSendUserName),&pfld);
hr = pfld->put_Value(CComVariant("domain\\username"));

hr = pFields->get_Item(CComVariant(cdoSendPassword),&pfld);
hr = pfld->put_Value(CComVariant("password"));

hr = pFields->get_Item(CComVariant(cdoSMTPUseSSL),&pfld);
hr = pfld->put_Value(CComVariant(VARIANT_TRUE));

hr = pFields->get_Item(CComVariant(cdoSendUsingMethod),&pfld);
hr = pfld->put_Value(CComVariant((long)cdoSendUsingPort));

hr = pFields->Update();

CComPtr<IMessage> pMsg;
pMsg.CoCreateInstance(L"CDO.Message");
pMsg->putref_Configuration(pConf);

// ... Compose message, add attachments, etc.

pMsg->Send();

}
CoUninitialize();
}

Any help would be greatly appreciated.

Christian ASTOR

unread,
Jul 10, 2009, 11:41:04 AM7/10/09
to
On 10 juil, 13:54, geogauci <geoga...@gmail.com> wrote:
> Hi. I've been trying to send an email using CDO (Collaboration Data
> Objects) using C++.

A Win32 sample (no ATL needed) which works for me =>

// to generate .tlh
//#import "c:\program files\common files\system\ado\msado15.dll"
rename_namespace("ADODB") rename("EOF", "EndOfFile")
raw_interfaces_only
//#import <cdosys.dll> no_namespace auto_search auto_rename
named_guids raw_interfaces_only

#include <windows.h>
#include <tchar.h>

#include "cdosys.tlh"

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
CoInitialize(NULL);

HRESULT hr = S_OK;
IMessage* pMessage = NULL;
hr = CoCreateInstance(__uuidof(Message), NULL, CLSCTX_INPROC_SERVER,
__uuidof(IMessage), (void**)&pMessage);

_variant_t vTo = "your_...@gmail.com";
_variant_t vText =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">"
"<HTML>"
" <BODY>"
" <p><FONT COLOR=\"#663399\"><b>This is a Test</b></FONT></
p>"
" </BODY>"
"</HTML>";

hr = pMessage->put_To(_bstr_t(vTo));
hr = pMessage->put_From(_bstr_t("\"From\" <f...@gmail.com>"));
hr = pMessage->put_Subject(_bstr_t("Test email CDO"));
hr = pMessage->put_HTMLBody(_bstr_t( vText ));

hr = pMessage->put_AutoGenerateTextBody(TRUE);
hr = pMessage->put_MimeFormatted(TRUE);


IConfiguration* pConfiguration = NULL;
hr = CoCreateInstance(__uuidof(Configuration), NULL,
CLSCTX_INPROC_SERVER, __uuidof(IConfiguration), (void**)
&pConfiguration);

ADODB::Fields* pFields = NULL;
ADODB::Field* pField = NULL;

hr = pConfiguration->get_Fields(&pFields);

hr = pFields->get_Item(_variant_t(cdoSendUsingMethod), &pField);
hr = pField->put_Value(_variant_t((long)cdoSendUsingPort));
pField->Release();
pField=NULL;

hr = pFields->get_Item(_variant_t(cdoSMTPServer), &pField);
hr = pField->put_Value(_variant_t("SRVSMTP.smt.cm.net")); // SMTP
Server
pField->Release();
pField=NULL;

hr = pFields->get_Item(_variant_t(cdoSMTPServerPort), &pField);
hr = pField->put_Value(_variant_t((long)25)); // SMTP Port
pField->Release();
pField=NULL;

pFields->get_Item(_variant_t(cdoSMTPAuthenticate),&pField);
pField->put_Value(_variant_t(cdoAnonymous));
pField->Release();
pField = NULL;

hr = pFields->Update();
pFields->Release();
pFields=NULL;

hr = pMessage->putref_Configuration(pConfiguration);
pConfiguration->Release();
pConfiguration=NULL;

pMessage->Send();
pMessage->Release();

CoUninitialize();

}

Mkumar

unread,
Jul 29, 2009, 3:50:20 PM7/29/09
to
Hey Christian,

I tried using code you posted here. the code compile and link fine.
But when I run it, it doesn't send an email.

On Jul 10, 11:41 am, Christian ASTOR <casto...@club-internet.fr>
wrote:


> On 10 juil, 13:54, geogauci <geoga...@gmail.com> wrote:
>
> > Hi. I've been trying to send an email using CDO (Collaboration Data
> > Objects) using C++.
>
> A Win32 sample (no ATL needed) which works for me =>
>
> // to generate .tlh
> //#import "c:\program files\common files\system\ado\msado15.dll"
> rename_namespace("ADODB") rename("EOF", "EndOfFile")
> raw_interfaces_only
> //#import <cdosys.dll> no_namespace auto_search auto_rename
> named_guids raw_interfaces_only
>
> #include <windows.h>
> #include <tchar.h>
>
> #include "cdosys.tlh"
>
> int APIENTRY _tWinMain(HINSTANCE hInstance,
>                      HINSTANCE hPrevInstance,
>                      LPTSTR    lpCmdLine,
>                      int       nCmdShow)
> {
>         CoInitialize(NULL);
>
>         HRESULT hr = S_OK;
>         IMessage* pMessage = NULL;
>         hr = CoCreateInstance(__uuidof(Message), NULL, CLSCTX_INPROC_SERVER,
> __uuidof(IMessage), (void**)&pMessage);
>

>         _variant_t vTo = "your_em...@gmail.com";

Ulrich Eckhardt

unread,
Jul 30, 2009, 6:02:41 AM7/30/09
to
Mkumar wrote:
> I tried using code you posted here. the code compile and link fine.
> But when I run it, it doesn't send an email.

This translates to "It doesn't work, fix this for me!".

Are you sure you want to order Christian, who already took the effort to
provide example code, to also fix some unspecified error that comes up for
you? Take a timeout and read Eric S. Raymond's essay on asking questions
the smart way, that should explain why this approach of yours does not and
will not yield any results.

Uli

Christian ASTOR

unread,
Jul 30, 2009, 7:58:36 AM7/30/09
to
On 29 juil, 21:50, Mkumar <mit.pa...@gmail.com> wrote:

> I tried using code you posted here. the code compile and link fine.
> But when I run it, it doesn't send an email.

As Ulrich said, you should be more precise. (it works for me with 2
different SMTP servers..)
This code is minimal, without error checking, mainly for the "hr"
variable.
You can check it manually by debugging and display its value with the
VS debugger : probably that it returns something else than S_OK at one
particular step...

0 new messages