4

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

Al007
  • 383
  • 1
  • 5
  • 15
  • If anyone found this page wondering how to send an email from Excel using CDO, I threw together a Google Doc [How to Send Email from Excel using Gmail](https://docs.google.com/document/d/1u5VLzCApU3k4-9Vp9LEfqyFZ6u9tAY0avNPYN_1FsN4/edit?usp=sharing) with [code on GitHub gist](https://gist.github.com/bergerjac/7355d4e528fa6c64a02dc494f3d241a1) – Jake Berger Jul 28 '18 at 11:25

5 Answers5

10

With this code, I get to send mail using CDO to Office365.

    Private Message As CDO.Message
Private Attachment, Expression, Matches, FilenameMatch, i

Sub enviar_mail()

Set Message = New CDO.Message
Message.Subject = "Test Mail"
Message.From = "YourEmail@yourdomain.com"
Message.To = ""
Message.CC = ""
Message.TextBody = "my text body here"

Dim Configuration
Set Configuration = CreateObject("CDO.Configuration")
Configuration.Load -1 ' CDO Source Defaults
Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "YourEmail@yourdomain.com"
Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "YourPass"
Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True

Configuration.Fields.Update

Set Message.Configuration = Configuration
Message.Send

End Sub

0

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 = "name@myemail.com"
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") = "name@myemail.com"
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
pghcpa
  • 833
  • 11
  • 25
  • When I set the port it doesn't work. When I leave the smtpserverport field off, I assume it defaults to port 25. I found our new ISP blocks 25. Looking around, it appears CDO or VBA doesn't correctly pass the smtpserverport. If anyone knows the secret, there are many posts like this one that can use the help. – pghcpa Jun 01 '16 at 23:23
  • A .NET library does **not** help with VB6 or Classic ASP code. – eidylon Jun 29 '17 at 20:45
  • 1
    You posted this as an answer. This is not an answer, it's another question. – HackSlash Nov 12 '18 at 17:07
0

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()
David G
  • 541
  • 4
  • 11
  • This throws error `The server rejected the sender address. The server response was: 530 Error: authentication Required` – Andrus Apr 20 '22 at 11:12
  • Hi David. According to this answer from Paolo Branzaglia on a similar question ( [Error sending email using CDO on port 587 (TLS)](https://stackoverflow.com/a/57989621/577765) ), there is a bug in CDO that prevents it from sending STARTTLS when using port 587, but it will send it when using port 25. That answer also has a link to a TechNet forum discussing this in more detail. – Solomon Rutzky Oct 12 '22 at 17:29
0

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")= "name@myemail.com"
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
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Monica G
  • 1
  • 1
  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P May 21 '22 at 10:22
-1

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 = "liveid@hotmail.com"

' Add recipient email address
oSmtp.AddRecipientEx "support@emailarchitect.net", 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 = "liveid@hotmail.com"
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!