使用cdo在c ++中发送邮件? [英] Using cdo to send mail in c++?
本文介绍了使用cdo在c ++中发送邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用cdo通过gmail发送电子邮件,但是它不起作用!它无法发送任何电子邮件.你能帮助我吗?谢谢
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();
}
推荐答案
您应该每次都使用真正的COM对象,而不要使用像
这样的宏. hr = msg-> put_TextBody(L"xxxxxxx");
创建一个真实的文本CComVariant(.
您是否检查了所有错误,例如
hr = msg-> put_Configuration(config);
这是关键任务"和最佳实践",还是我应该使用CAPS;-)
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 ;-)
HRESULT 0x8004020d出现在 ^ ]
来自 http://msdn.microsoft.com/en-us/library/ms870485(v = exchg.65).aspx [
The HRESULT 0x8004020d appears in http://support.microsoft.com/kb/963581[^]
From http://msdn.microsoft.com/en-us/library/ms870485(v=exchg.65).aspx[^]
#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();
}
我认为您错过了cdoSendEmailAddress
,并将发件人的邮件放在cdoSendUserName
中.当然,如果不调试计算机中的代码,我将无法确认或反驳这一假设.;)
希望这会有所帮助,
巴勃罗.
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.
这篇关于使用cdo在c ++中发送邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文