在587端口(使用TLS安全协议(protocol)的端口)用CDO发送邮件有什么技巧吗?
这是我的 C++ 代码:
CDO::IMessagePtr iMsg(__uuidof(CDO::Message));
CDO::IConfigurationPtr iConf = iMsg->GetConfiguration();
CDO::FieldsPtr iFields;
_bstr_t empty("");
iConf->Load(CDO::cdoIIS,empty); // this string constant from import
iFields = iConf->Fields;
iFields->Item["https://schemas.microsoft.com/cdo/configuration/smtpserver"]->Value = _variant_t(szServer);
iFields->Item["https://schemas.microsoft.com/cdo/configuration/smtpserverport"]->Value = _variant_t(587);
iFields->Item["https//schemas.microsoft.com/cdo/configuration/sendusing"]->Value = 2;
iFields->Item["https//schemas.microsoft.com/cdo/configuration/smtpauthenticate"]->Value = _variant_t(1); // Basic
iFields->Item["https//schemas.microsoft.com/cdo/configuration/sendusername"]->Value = _variant_t(szUser);
iFields->Item["https//schemas.microsoft.com/cdo/configuration/sendpassword"]->Value = _variant_t(szPassword);
if(iUseSSLTLS == 2)
iFields->Item["https//schemas.microsoft.com/cdo/configuration/sendtls"]->Value = _variant_t(true);
else
iFields->Item["https//schemas.microsoft.com/cdo/configuration/smtpusessl"]->Value = _variant_t(true);
iFields->Update();
etc... etc...
如果我将此代码与 smtp.gmail.com
一起使用:
- 服务器:smtp.gmail.com,
- 端口:587,
- sndtls = true,
- 帐户:我的 gmail 帐户,
- 密码:
我得到以下响应:
- 代码 = 8004020e,
- 代码含义 = Impossibile modificare o eliminare un oggetto che è stato aggiunto utilizzando COM+ Admin SDK,
- 来源 = (null),
- 描述 = Indirizzo del mittente respinto dal 服务器。 Risposta del 服务器:530 5.7.0 必须先发出 STARTTLS 命令。 y2sm3575389wme.12 - gsmtp,
(抱歉......部分消息是意大利语,但请看一下粗体/斜体)
显然,如果我使用相同的参数配置 Outlook 2010,它会完美运行。
还有一件事,如果我使用端口 465 和 SSL:
- 服务器:smtp.gmail.com,
- 端口:465,
- smtpusessl= true,
- 帐户:我的 gmail 帐户,
- 密码:
代码运行良好,但我需要配置 587 端口和 TLS。
我最终一起尝试了 smtpusessl
和 sendtls
,并将它们设置为 true:
iFields->Item["https//schemas.microsoft.com/cdo/configuration/sendtls"]->Value = _variant_t(true);
iFields->Item["https//schemas.microsoft.com/cdo/configuration/smtpusessl"]->Value = _variant_t(true);
我收到以下错误:
- 代码 = 80040213
- 代码含义 = IDispatch 错误#19
- 来源 = CDO.Message.1
- 说明 = 传输无法连接到服务器。
最佳答案
2 年多后,我找到了解决方案,嗯……不是解决方案,但现在我知道为什么它不起作用,为什么它永远不会起作用。 CDO 库中似乎有一个错误:它可以处理端口 25 上的 STARTTLS 命令,但不能处理端口 587。
正如我刚刚在上面所写的那样,这是一个错误,我认为 Microsoft 永远不会纠正它。 future 的建议是放弃 CDO 并使用“Power shell”或第三方组件。
关于c++ - 在端口 587 (TLS) 上使用 CDO 发送电子邮件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45009481/