3

Is there any trick to sending mail with CDO on port 587 (the port that uses TLS security protocol)?

This is my C++ code:

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...

If I use this code with smtp.gmail.com:

  • server: smtp.gmail.com,
  • port: 587,
  • sndtls = true,
  • account: my gmail account,
  • password:

I obtain the following response:

  • Code = 8004020e,
  • Code meaning = Impossibile modificare o eliminare un oggetto che è stato aggiunto utilizzando COM+ Admin SDK,
  • Source = (null),
  • Description = Indirizzo del mittente respinto dal server. Risposta del server: 530 5.7.0 Must issue a STARTTLS command first. y2sm3575389wme.12 - gsmtp,

(sorry ... part of the message is in Italian language, but take a look at the bold/italic one)

Obviously, if I configure Outlook 2010 using the same parameters, it works perfectly.

One more thing, if I use port 465 and SSL:

  • server: smtp.gmail.com,
  • port: 465,
  • smtpusessl= true,
  • account: my gmail account,
  • password:

the code works fine, but I need to configure 587 port and TLS.


I eventually tried smtpusessl and sendtls together, setting them 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);

And I obtain the following error:

  • Code = 80040213
  • Code meaning = IDispatch error #19
  • Source = CDO.Message.1
  • Description = The transport failed to connect to the server.
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Pretty sure smtpusessl needs to be on for TLS as well – Alex K. Jul 10 '17 at 10:23
  • Do you meen; iFields->Item[... /sendtls"]->Value = _variant_t(true); iFields->Item[... /smtpusessl"]->Value = _variant_t(true); togheter? – Paolo Branzaglia Jul 10 '17 at 10:46
  • no, I tried smtpusessl and sendtls together, setting them true and I obtain the following error: Code = 80040213 Code meaning = IDispatch error #19 Source = CDO.Message.1 Description = The transport failed to connect to the server. – Paolo Branzaglia Jul 10 '17 at 14:58
  • Does this answer your question? [Using CDO/SMTP/TLS in VB6 to send email smtp.office365.com mail server](https://stackoverflow.com/questions/37530037/using-cdo-smtp-tls-in-vb6-to-send-email-smtp-office365-com-mail-server) – David G Jun 05 '21 at 11:19

1 Answers1

9

After over 2 years, I found a solution, well ... not a solution, but now I know why it didn't work, e why it will never work. It seems there's a bug in CDO library: it can handle STARTTLS command on port 25, but it can't on port 587.

You can read more here: https://social.technet.microsoft.com/Forums/en-US/37d00342-e5e9-4c8d-975d-44362332d426/bug-in-cdomessage-smtpserverport-587-fails?forum=ITCG

As I've just written above, it's a bug and I think Microsoft will never correct it. The recommendation for the future is to abandon CDO and use "Power shell" or third-party components.