'Using CDO/SMTP/TLS in VB6 to send email smtp.office365.com mail server

I am searching for days to find out how can I set Office365 SMTP server in my VB6 application. My code is working properly with port 465 and other mail servers. BUT it is not working with port 587 and smtp.office365.com

Is there any way I could have TLS via 587 in VB6?

Thanks



Solution 1:[1]

This code worked for me until a few days ago when we switched ISP's (or maybe something changed coincidentally on the server side). If I specify port 587, I get a transport error and have not found the solution for that with VBA. Please let me know if this works for you and if you find a way to use 587. (Port 25 doesn't work for me either, same error.)

Public Function SMTPSend(vSendTo, vsubject As Variant, vmessage As Variant)
'This works
Set emailObj = CreateObject("CDO.Message")

emailObj.From = "[email protected]"
emailObj.To = vSendTo
emailObj.Subject = vsubject
emailObj.TextBody = vmessage
'emailObj.AddAttachment "c:\windows\win.ini"

Set emailConfig = emailObj.configuration


emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
'Must exclude port if specifying SSL
'emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "[email protected]"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
emailConfig.Fields.Update

emailObj.Send

If Err.Number = 0 Then SMTPSend = True Else SMTPSend = False

End Function

Solution 2:[2]

There seem to be a number of posts on various forums suggesting that the CDO library doesn't work with port 587, but that's not true. I think the real issue is that SMTP services using port 587 are doing so because they require STARTTLS authentication and the http://schemas.microsoft.com/cdo/configuration/smtpusessl config option is specific to SSL, not TLS (I know, I know - not the most accurate description but it'll suffice for now).

Instead, there is another setting - http://schemas.microsoft.com/cdo/configuration/sendtls - that does support TLS, so using this with port 587 works fine:

config.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value = 2          '2 = cdoSendUsingPort
config.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value = "smtp.example.com"
config.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport").Value = 587
config.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").Value = 1   '1 = cdoBasic
config.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername").Value = "my_username"
config.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword").Value = "myy_very_secret_password"
config.Fields("http://schemas.microsoft.com/cdo/configuration/sendtls").Value = True
config.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout").Value = 60
config.Fields.Update()

Solution 3:[3]

This is what worked for me:

flds("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2          
flds("http://schemas.microsoft.com/cdo/configuration/smtpserver")= "smtp.office365.com"
flds("http://schemas.microsoft.com/cdo/configuration/smtpserverport")= 25
flds("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")= 1   
flds("http://schemas.microsoft.com/cdo/configuration/sendusername")= "[email protected]"
flds("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypasword"
flds("http://schemas.microsoft.com/cdo/configuration/smtpusessl")= True
flds("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout")= 60

Solution 4:[4]

I don't know if you could fix it, but I got it with this:

Private Sub Command1_Click()

Dim oSmtp As New EASendMailObjLib.Mail
oSmtp.LicenseCode = "TryIt"

' Set your Hotmail email address
oSmtp.FromAddr = "[email protected]"

' Add recipient email address
oSmtp.AddRecipientEx "[email protected]", 0

' Set email subject
oSmtp.Subject = "test email from hotmail account"

' Set email body
oSmtp.BodyText = "this is a test email sent from VB 6.0 project with hotmail"

' Hotmail SMTP server address
oSmtp.ServerAddr = "smtp.live.com"

' Hotmail user authentication should use your 
' Hotmail email address as the user name. 
oSmtp.UserName = "[email protected]"
oSmtp.Password = "yourpassword"

' Set port to 25, if you want to use 587 port, please change 25 to 587
oSmtp.ServerPort = 25

' detect SSL/TLS connection automatically
oSmtp.SSL_init

MsgBox "start to send email ..."

If oSmtp.SendMail() = 0 Then
    MsgBox "email was sent successfully!"
Else
    MsgBox "failed to send email with the following error:" & oSmtp.GetLastErrDescription()
End If

End Sub

Font: https://www.emailarchitect.net/easendmail/kb/vb.aspx?cat=4

Remember download the library:

http://easendmail-smtp-component-net-edition.soft112.com/

Just Replace Parameters!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pghcpa
Solution 2 David G
Solution 3 Tyler2P
Solution 4 Jhony Donosso Gutierrez