Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using cdo to send email through gmail but it doesn't work! It can not send any email. Can you help me? Thanks

#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( )
{ 
	HRESULT hr = S_OK;

	// Create the Configuration object.
	CComPtr<IConfiguration> config	= NULL;
	FieldsPtr				fields	= NULL;
	FieldPtr				field	= NULL;
	CComPtr<IMessage>		msg		= NULL;
	
	hr = CoInitialize(NULL);	

	hr = config.CoCreateInstance(L"CDO.Configuration");
	hr = config->get_Fields(&fields);
	
	hr = fields->get_Item(CComVariant(cdoSMTPServer), &field);
	hr = field->put_Value(CComVariant("smtp.gmail.com"));

	hr = fields->get_Item(CComVariant(cdoSMTPServerPort), &field);
	hr = field->put_Value(CComVariant((long)25));

	hr = fields->get_Item(CComVariant(cdoSMTPAuthenticate), &field);
	hr = field->put_Value(CComVariant((long)cdoBasic));

	hr = fields->get_Item(CComVariant(cdoSendUserName), &field);
	hr = field->put_Value(CComVariant("sample@gmail.com"));

	hr = fields->get_Item(CComVariant(cdoSendPassword), &field);
	hr = field->put_Value(CComVariant("sample"));

	hr = fields->get_Item(CComVariant(cdoSMTPUseSSL), &field);
	hr = field->put_Value(CComVariant(VARIANT_TRUE));

	hr = fields->get_Item(CComVariant(cdoSendUsingMethod), &field);
	hr = field->put_Value(CComVariant((long)cdoSendUsingPort));

	hr = fields->Update();		
		
	hr = msg.CoCreateInstance(L"CDO.Message");
	hr = msg->put_Configuration(config);

	// Compose message, add attachments, etc.
	hr = msg->put_Subject(L"xxx");		
	hr = msg->put_To(L"touser@gmail.com");		
	hr = msg->put_TextBody(L"xxxxxxx");		

	hr = msg->Send();

	CoUninitialize();
}
Posted
Comments
Pablo Aliskevicius 29-Mar-12 7:09am    
Did you test hr? Something like "if (FAILED(_hr)) _com_issue_error(_hr); "
This should help you identify the line where some action fails.
thuyphuongid 29-Mar-12 7:15am    
There is an error at line msg->send()with:
0x8004020d Cannot modify or delete an object that was not added using the COM+ Admin SDK. Help me an instruction.

you should use everytime real COM-Objects, and not such macros like in
hr = msg->put_TextBody(L"xxxxxxx");

create a real textual CComVariant(.

Did you check every error, like

hr = msg->put_Configuration(config);

it is "mission critical" and "best practice" or should I use CAPS ;-)
 
Share this answer
 
Comments
thuyphuongid 29-Mar-12 8:11am    
I have checked every hr above. But, there is only an error at line 'send()'. It's still unresolved to me.
KarstenK 29-Mar-12 8:17am    
have you created these objects?
The HRESULT 0x8004020d appears in http://support.microsoft.com/kb/963581[^]

From http://msdn.microsoft.com/en-us/library/ms870485(v=exchg.65).aspx[^]

C++
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace
#import "c:\program files\common files\microsoft shared\cdo\cdoex.dll" no_namespace

main( ){

  CoInitialize(NULL);
  {
    IMessagePtr IMsg(__uuidof(Message));
    IConfigurationPtr iConfig = Msg->Configuration;

    FieldsPtr Flds;
    FieldPtr Fld;
    Flds = iConfig->Fields;

    // The full strings for field names are used to clarify the process.
    // The cdoex.h header file contains BSTR constants that
    // can be used to avoid typos and so forth.

    Flds->Item[_variant_t("http://schemas.microsoft.com/cdo/configuration/smtpserver")]->Value = _variant_t("fakesmtp.example.com") ;
    Flds->Item[_variant_t("http://schemas.microsoft.com/cdo/configuration/smtpserverport")]->Value = _variant_t((long)25) ;
    Flds->Item[_variant_t("http://schemas.microsoft.com/cdo/configuration/sendusing")]->Value = _variant_t((int)cdoSendUsingPort) ;
	// this value is 2
    Flds->Item[_variant_t("http://schemas.microsoft.com/cdo/configuration/smtpaccountname")]->Value = _variant_t("My Name") ;
    Flds->Item[_variant_t("http://schemas.microsoft.com/cdo/configuration/sendemailaddress")]->Value = _variant_t("\"MySelf\" <myself@example.com>") ;
    Flds->Item[_variant_t("http://schemas.microsoft.com/cdo/configuration/senduserreplyemailaddress")]->Value = _variant_t("\"Another\" <another@example.com>") ;
    Flds->Item[_variant_t("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")]->Value = _variant_t((long)cdoBasic) ;
    Flds->Item[_variant_t("http://schemas.microsoft.com/cdo/configuration/sendusername")]->Value = _variant_t("domain\\username") ;
    Flds->Item[_variant_t("http://schemas.microsoft.com/cdo/configuration/sendpassword")]->Value = _variant_t("password") ;
    Flds->Update();

    /*
    ** These string constants are available in the cdosys.h header file,
    ** but are not put in the cdosys.tlh file when #import runs.

       const BSTR cdoSMTPServer = = L"http://schemas.microsoft.com/cdo/configuration/smtpserver";

       and so on additionally for each of these:

       cdoSMTPServer
       cdoSMTPAccountName
       cdoSMTPAuthenticate
       cdoSendUsingMethod    (You can use the CdoSendUsing enumeration for this.)
       cdoSMTPServerPort
       cdoSendEmailAddress
       cdoSendUserName
       cdoSendPassword
       cdoSendUserReplyEmailAddress
    **
    */

    iMsg->Configuration = iConfig;

    // ... Compose message; add attachments and so forth.

    iMsg->Send();

  }
  CoUninitialize();
}


I think you missed cdoSendEmailAddress, and put the sender's mail in cdoSendUserName. Of course, I cannot confirm or disprove this assumption without debugging the code in your machine... ;)
Hope this helps,

Pablo.
 
Share this answer
 
v2
Comments
thuyphuongid 29-Mar-12 11:05am    
I have changed something in my program, but it's not working to me!
Tks for your help.

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