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

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

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

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

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()

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

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!

Related

Cannot send mail using smtp.gmail.com from ONLY one specific Google account

I am not able to send mail via smtp.gmail.com for ONLY one specific Google account. When I use another Google account my VBS script sends mail successfully so I know the script is working correctly.
Both accounts have the Less Secure Apps turned ON.
I have also tried the Display Unlock Captcha multiple times.
POP and IMAP has been enabled for both accounts.
It looks like there is an issue with that particular account and I have already posted a request for support in the Gmail help forums.
https://productforums.google.com/forum/#!topic/gmail/8XFKONFbDA8
My VBS Script:
Dim emailObj
Set emailObj = CreateObject("CDO.Message")
emailObj.From = "******#gmail.com"
emailObj.To = "******#gmail.com"
emailObj.Subject = "Test Email"
emailObj.TextBody = "Testing Email Functionality"
Set emailConfig = emailObj.Configuration
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
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/smtpconnectiontimeout") = 60
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
'First Account
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "******#gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "************"
'Second Account
'emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "******#gmail.com"
'emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "************"
emailConfig.Fields.Update
emailObj.Send
If err.number = 0 Then
Msgbox "Done"
Else
Msgbox err
End If
Account 1 Test (Not Working)
Account 2 Test (Working)
Error Message: The message could not be sent to the SMTP server. The transport error code was 0x80040217. The server response was not available
Any help would be appreciated.
I had a same error. There is an option in Google account - https://myaccount.google.com/lesssecureapps. I turned it On and it helped me.

VBScript that sends an email without an SMTP server?

I'm trying to create a VBS script that sends an alert email when a folder has reached a specific file size, but I can't seem to get it to send an email. I get this error - "The transport failed to connect to the server". Is there any way to send an email without a SMTP server or?
I changed my pswrd/email an stuff for obv reasons.
Const dirPath = "C:\Users\tim.mcgee\Desktop\Offsite Drive"
Const alertedPath = "prevRun.txt"
alertOn = 3 * 2 ^ 29 '1.5GB
resetOn = alertOn * .95 'Approx 77MB
Const emailTo = "**"
Const emailFrom = "**"
Const emailSbjct = "Offsite Drive Full"
Const emailMsg = "The offsite drive has reached maximum capacity."
Const SMTPServer = "Smtp.gmail.com"
Const SMTPPort = 25
emailUsr = emailFrom
Const emailPsswd = "**"
Const emailSSL = False
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(alertedPath) Then
alerted = CBool(Trim(fso.OpenTextFile(alertedPath).ReadLine))
Else
alerted = False
End If
dirSize = fso.GetFolder(dirPath).Size
If alerted Then 'Email previously sent
alerted = dirSize > resetOn
ElseIf dirSize >= alertOn Then
SendEmail
alerted = True
End If
fso.OpenTextFile(alertedPath, 2, True).WriteLine CInt(alerted)
WScript.Quit 0
Sub SendEmail
Const cfg = "http://schemas.microsoft.com/cdo/configuration/"
With CreateObject("CDO.Message")
.From = emailFrom
.To = emailTo
.Subject = emailSbjct
.TextBody = emailMsg
With .Configuration.Fields
.Item(cfg & "sendusing") = 2
.Item(cfg & "smtpserver") = SMTPServer
.Item(cfg & "smtpserverport") = SMTPPort
.Item(cfg & "smtpconnectiontimeout") = 60
.Item(cfg & "smtpauthenticate") = 1
.Item(cfg & "smtpusessl") = emailSSL
.Item(cfg & "sendusername") = emailUsr
.Item(cfg & "sendpassword") = emailPsswd
.Update
End With
.Send
End With
End Sub
The error message means that your script can't connect to smtp.gmail.com on port 25. Nowadays most ISPs don't allow outbound mail on port 25 as a spam prevention measure. You need to send either through one of their mail relays, or the remote server must accept mail on a different port, usually 587 (submission) or 465 (SMTPS, deprecated).
Since you already have credentials you should probably just change the value of SMTPPort to 587 or 465. Gmail should accept authenticated mail on either of those ports.
As for your question about sending mail without an SMTP server, when using CDO you basically have 3 options for sending messages. You select the one you want to use via the sendusing configuration field:
cdoSendUsingPickup (numeric value 1): allows you to send mail without having to specify an SMTP server, but you must have an SMTP server installed on the host where the script is running. With this method the mail is submitted to the local SMTP server via a pickup folder. Does not require authentication, but the SMTP server must be configured to route mail correctly.
Normally, when you have a setup with local SMTP servers, these are configured to send all picked up mail to a central mail gateway/hub, which handles further delivery.
cdoSendUsingPort (numeric value 2, default): allows you to send mail to any SMTP server via the SMTP protocol. Also allows you to provide explicit credentials. With this method you must specify an SMTP server to send the mail to.
cdoSendUsingExchange (numeric value 3): allows you to send mail through the Exchange server of your domain. Requires a domain and an Exchange server, obviously.

email via SMTP - Outlook and Powershell OK, but VBscript/CDO does not work

I'm trying to send email via a remote SMTP: port 25, no authentication. I have this script, that gives me the 80040213 error ("The transport failed to connect to the server"). However, when I try with the same parameters from Outlook or Powershell, it works. I googled my ... off but I cannot find a solution.
Any suggestions are welcome. Thanks.
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "CDO test"
objMessage.From = "person#firm.com"
objMessage.To = "person#firm.com"
objMessage.TextBody = "This is a test email."
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "<SMTPserver>"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
objMessage.Send
OK. Finally I found out. McAfee Enterprise blocks communication via port 25 for some applications as a protection against "mass emailing worms".

how to spam an email using VBS

So i would like to spam an email to check the spam filters. However, it keeps giving me error messages. Such as
could not connect to STMP server
Please help me to improve this.
Dim User
Dim Pass
Dim Name
Dim Input
Dim Input2
Dim Input3
X=MsgBox("Welcome. To log in Please Click OK and enter your G-mail & pass.",0,"EmailSpamBot V1.0")
User = InputBox("Enter your G-mail:")
Pass = InputBox("Enter Password:"& vbCrLf & ""& vbCrLf & "Please note passwords are NOT stored in this script and are case sensitive.")
Name = InputBox("Enter Name:")
Input = InputBox("Enter e-mail of victim:")
Input2 = InputBox("Enter title:")
Input3 = InputBox("Enter message:")
EmailSubject = (""& Input2)
EmailBody = (""& Input3)
'Const EmailFrom = ""
'Const EmailFromName = ""
Const SMTPServer = "smtp.gmail.com"
'Const SMTPLogon = ""
'Const SMTPPassword = ""
Const SMTPSSL = True
Const SMTPPort = 465
Const cdoSendUsingPickup = 1 'Send message using local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using SMTP over TCP/IP networking.
Const cdoAnonymous = 0 ' No authentication
Const cdoBasic = 1 ' BASIC clear text authentication
Const cdoNTLM = 2 ' NTLM, Microsoft proprietary authentication
' First, create the message
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = EmailSubject
objMessage.From = "<" & User & Name & ">"
objMessage.To = "<" & Input & ">"
objMessage.TextBody = EmailBody
' Second, configure the server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = User
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = Pass
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPPort
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = SMTPSSL
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMessage.Configuration.Fields.Update
do
ObjMessage.Send
loop
Any help would be greatly appreciatied
The error message "could not connect to SMTP server" means that your script couldn't establish a connection to the given port on the remote host. That usually happens when a firewall is blocking access. You can verify that with the telnet command:
telnet smtp.gmail.com 465
If access is blocked you should be getting output like this after the timeout expiered:
Connecting To smtp.gmail.com...Could not open connection to the host, on port 465: Connect failed
A tool like tracetcp might provide further insight as to where the packets are blocked.
However, sending authenticated mail is normally for outbound message submission. If you want to test your own spam filter, you should be sending inbound mail (to port 25).
I figured out the problem, when you enter your email, five minutes or so later you get a security email about a less secure app attempting to access, that is the VBS script, you click on the enable less secure app access, and then it works.

Couldn't send message to SMTP server. Transport Error 0x80040217

I get this error when sending a mail through asp using gmail, I already used ports 465, 587 and 25 with same results
Dim mail
dim email2 as string
dim urlms as string
Dim mail
dim email2 as string
dim urlms as string
mail = CreateObject("CDO.Message")
urlms = "http://schemas.microsoft.com/cdo/configuration/"
mail.Configuration.Fields.Item(urlms & "sendusing") = 2 'enviar usando port
mail.Configuration.Fields.Item(urlms & "smtpserver") = "smtp.gmail.com"
mail.Configuration.Fields.Item(urlms & "smtpserverport") = 465
mail.Configuration.Fields.Item(urlms & "smtpusessl") = True
mail.Configuration.Fields.Item(urlms & "smtpconnectiontimeout") = 60
mail.Configuration.Fields.Item(urlms + "smtpauthenticate") = 1
mail.Configuration.Fields.Item(urlms + "sendusername") = "" 'login
mail.Configuration.Fields.Item(urlms + "sendpassword") = "" 'password
mail.Configuration.Fields.Update
mail.Send
If you are using 2-Step authentication in Google Account then you need to change settings either opt to "Enable Less Secure Apps" or generate app passwords that will be 16 chars and you need to use this in your code instead of your actual gmail password.
https://www.google.com/settings/security/lesssecureapps
It worked like a charm for my own mail server, but It fails with Gmail I don't know why....
Anyway, I tried also without the plus to concatinate and didnt work either, finally I used this:
Dim ObjSendMail
Set ObjSendMail = CreateObject("CDO.Message")
'This section provides the configuration information for the remote SMTP server.
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Send the message using the network (SMTP over the network).
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="mail.yoursite.com"
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465 ' or 587
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
' Google apps mail servers require outgoing authentication. Use a valid email address and password registered with Google Apps.
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'basic (clear-text) authentication
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="somemail#yourserver.com" 'your Google apps mailbox address
ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="yourpassword" 'Google apps password for that mailbox
ObjSendMail.Configuration.Fields.Update
ObjSendMail.To = "someone#someone.net"
ObjSendMail.Subject = "this is the subject"
ObjSendMail.From = "someone#someone.net"
' we are sending a text email.. simply switch the comments around to send an html email instead
'ObjSendMail.HTMLBody = "this is the body"
ObjSendMail.TextBody = "this is the body"
ObjSendMail.Send
Set ObjSendMail = Nothing
http://somee.com/DOKA/DoHelpTopics.aspx?docode=false&thnid=102
And worked like a charm for my server but it didn't work for gmail.
//
// MessageId: CDO_E_LOGON_FAILURE
//
// MessageText:
//
// The transport was unable to log on to the server.
//
#define CDO_E_LOGON_FAILURE 0x80040217L
Also why are you using plus to concatinate the three config items.
Your code Dims everything twice. And it dims things as strings.
You are creating objects without using Set.
I doubt your code runs to generate an error.
Note: The message says LOGON ERROR. Make sure name and password is correct.
Set emailObj = CreateObject("CDO.Message")
emailObj.From = "dc#gail.com"
emailObj.To = "dc#gail.com"
emailObj.Subject = "Test CDO"
emailObj.TextBody = "Test CDO"
Set emailConfig = emailObj.Configuration
msgbox emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver")
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
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/smtpusessl") = true
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "YourUserName"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Password1"
emailConfig.Fields.Update
emailObj.Send
If err.number = 0 then Msgbox "Done"