VBA send email using Gmail: transport failed to connect - vba

Running the Excel VBA code below I get the following error:
Run-time error -2147220973
The transport failed to connect to the server
Public Function send_email()
Set cdomsg = CreateObject("CDO.message")
With cdomsg.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'NTLM method
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 465
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "mymail#gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypass"
.Update
End With
' build email parts
With cdomsg
.To = "mymail#gmail.com"
.From = "mymail#gmail.com"
.Subject = "the email subject"
.TextBody = "the full message body goes here. you may want to create a variable to hold the text"
.Send
End With
Set cdomsg = Nothing
End Function

Trouble source
A typo caused this mess - smptserverport should be smtpserverport!
Things I tried
On the way to solve this issue, I tried many things. But now I'm not so sure if all were actually needed.
still, someone might gain something from the list:
Enable "Less Secure App"
Open "Display Captcha" before testing connection
Make sure you have a strong password
App Passwords are no longer recommended
2 Step Verification is not a must - but highly recommended!
Few tips:
Port 465 should be in use when sending the password in clear text
Port 587 should be used when sending a secure password.
if you need to have secure password within VBA/VBS, you can use this little trick with PowerShell (source):
Dim myPassword, cmd, shell, executor, securePassword
myPassword = "ABCABCABC....."
cmd = "powershell.exe ConvertTo-SecureString "& myPassword
Set shell = CreateObject("WScript.Shell")
Set executor = shell.Exec(cmd)
executor.StdIn.Close
securePassword = executor.StdOut.ReadAll

Now I am using the bellow code and it works fine.
Don't forget to change your gmail config to allow sending email from other apps.
Sub sendEmail(gmail, password, targetEmail, subject, bodyContent)
Dim Mail As New Message
Dim Config As Configuration: Set Config = Mail.Configuration
Config(cdoSendUsingMethod) = cdoSendUsingPort
Config(cdoSMTPServer) = "smtp.gmail.com"
Config(cdoSMTPServerPort) = 465
Config(cdoSMTPAuthenticate) = cdoBasic
Config(cdoSMTPUseSSL) = True
Config(cdoSendUserName) = gmail
Config(cdoSendPassword) = password
Config.Fields.Update
Mail.To = targetEmail
Mail.from = Config(cdoSendUserName)
Mail.subject = subject
Mail.HTMLBody = bodyContent
Mail.Send
End Sub

Related

Error 530 5.7.0: server rejected sender address

I am trying to send an automated message to my gmail address with CDO.
I checked every setting in my email account (e.g., allow access through less secure apps) but still get the error message 530 5.7.0: The server rejected the sender address.
My code for the configs so far is:
With fields
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myemail#gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
.Update
End With
NewMail.Send
I also noticed that the schemas.microsoft.com site does not seem to work. Is there an update? Or is it still up and the mistakes is within my code?

When sending mail via SMTP, I get "Transport Failed to Connect to server"

I have the code below to send mail from a VBA macro using CDO. I get an error in the code:
Transport failed To connect to server Error
I am sending a mail from the Gmail SMTP service. Looks like the configuration is set correctly, but somehow it doesn't work.
Sub Email()
Dim CDO_Mail As Object
Dim CDO_Config As Object
Dim SMTP_Config As Variant
Dim strSubject As String
Dim strFrom As String
Dim strTo As String
Dim strCc As String
Dim strBcc As String
Dim strBody As String
strSubject = "Results from Excel Spreadsheet"
strFrom = "xxx#gmail.com"
strTo = "xxx#gmail.com"
strBody = "The total results are: 167"
Set CDO_Mail = CreateObject("CDO.Message")
Set CDO_Config = CreateObject("CDO.Configuration")
CDO_Config.Load -1
Set SMTP_Config = CDO_Config.Fields
With SMTP_Config
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxxx#gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxx"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Update
End With
With CDO_Mail
Set .Configuration = CDO_Config
End With
CDO_Mail.Subject = strSubject
CDO_Mail.From = strFrom
CDO_Mail.To = strTo
CDO_Mail.TextBody = strBody
CDO_Mail.Send
End Sub
That code works totally fine for me (send from Gmail to Gmail) - so you need to check the following:
try it with port 587 as well as port 465 (further reading)
configure your sending account in Gmail for Access for less secure apps - there is a Turn On option per the following support page
Allowing less secure apps to access your account
Google may block sign-in attempts from some apps or devices that do not use modern security standards. Since these apps and devices are easier to break into, blocking them helps keep your account safe.
Some examples of apps that do not support the latest security standards include:
The Mail app on your iPhone or iPad with iOS 6 or below
The Mail app on your Windows phone preceding the 8.1 release
Some Desktop mail clients like Microsoft Outlook and Mozilla Thunderbird
...
Option 2: Change your settings to allow less secure apps to access your account. We don't recommend this option because it might make it easier for someone to break into your account. If you want to allow access anyway, follow these steps:
Go to the "Less secure apps" section in My Account.
Next to "Access for less secure apps," select Turn on. (Note to Google Apps users: This setting is hidden if your administrator has locked less secure app account access.)
CDO is pretty old now so assume that is an example of an app that doesn't support latest security standards.

Use CDO Send Emails in Excel VBA Not Working

I am trying to send email within Excel VBA. Based on my research on Internet, I enabled Microsoft CDO for Windows 2000 Library and composed my VBA codes as below:
Sub SendCDOMail()
Dim objCDOMsg As Object
Set objCDOMsg = CreateObject("CDO.Message")
'CDO Configuration
With objCDOMsg.Configuration.Fields
'
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Server port (typically 25, 587)
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
'SMTP server IP or Name
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
'Type of authentication, NONE, Basic (Base64 encoded), NTLM
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
'SMTP Account User ID
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxx#bbbb.com"
'SMTP Account Password
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxxxx"
'Use SSL for the connection (False or True)
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
.Update
End With
'CDO Message
objCDOMsg.Subject = "TEST Subject"
objCDOMsg.From = "xx#xx.com"
objCDOMsg.To = "xx#xx.com"
objCDOMsg.TextBody = "TEST Body"
objCDOMsg.Send
End Sub
However, I received the error saying:
The server rejected the sender address. The server response was: 530.5.7.57 SMTP;
Client was not authenticated to send anonymous mail during MAIL FROM
I also tried the same configurations using PowerShell and it worked (smtpserver, username, password, server port...).
I don't know where it might go wrong.
Here it is: https://technet.microsoft.com/en-us/library/mt210446(v=exchg.150).aspx - note that this is in the Office365 section for "How to set up a multifunction device or application to send email using Office 365,", so there may be other things in there for you too.
This indicates that you are connecting to the SMTP client submission endpoint (smtp.office365.com), which can't be used for direct send. For direct send, use the MX endpoint for your Office 365 tenant, which ends with "mail.protection.outlook.com." You can find your MX endpoint by following the steps in Full configuration instructions for direct send.

Unable to Send Mail (VB.net)

I am trying to send a mail using Dart Control. It was working well with a particular server, but since I switched to a secured server, I receive the following error message:
Protocol Exception--
Request: AUTH LOGIN
Response: 530 Must issue a STARTTLS command first
Below is the code :
Dim Smtp1 As Smtp = New Smtp
Dim SMTPResult As SmtpResult
Dim Message As Dart.Mail.MailMessage = New Dart.Mail.MailMessage()
Smtp1.Session.RemoteEndPoint.Port = intPortNo
strErrLoc = "SMTP1.DnsServerTimeout"
Smtp1.DnsServerTimeout = 15 'default time out 30 seconds
strErrLoc = "Set User Name"
Smtp1.Session.Username = strUserID
Smtp1.Session.Password = strPWD
strErrLoc = "Subject and Mail TEXT"
Message.Subject = strSubject
If strHTMLEmail.Trim = "" Then
Message.Text = strMailText
Else
Message.Html = strHTMLEmail
End If
Smtp1.Session.Authentication = Authentication.Auto
Smtp1.Session.RemoteEndPoint.HostNameOrAddress = strServerName.Trim
Smtp1.Session.ServicePrincipleName = "SMTP/" & strServerName.Trim
SMTPResult = Smtp1.Send(Message)
try with enabling SSL mode,like
Smtp1.EnableSSL = True
this works in case of Gmail ,Please Check with yours.
The suggestion is to use Explicit security.
The following Code implementing Explicit Security resolved the issue.
Smtp1.Session.Security.Encrypt = Encrypt.Explicit

Send email via gmail from VBA (MS Access) with CDO: The Transport Failed to connect to the Server

I found following code but it is not working.
Set cdomsg = CreateObject("CDO.message")
With cdomsg.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = 465
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = MailSender
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = MailPassword
.Item("http://schemas.microsoft.com/cdo/configuration/urlproxyserver") = "xxx.xxx.x.xxx:8080"
.Update
End With
' build email parts
With cdomsg
.To = MailTo
.From = MailSender
.Subject = MailSubject
.TextBody = MailText
.Send
End With
Set cdomsg = Nothing
I get the error
The Transport Failed to connect to the Server
I'm behind a proxy I tried with and without it. The error is the same. What I'm uncertain about is if IIS is required for this to work becasue when googling this error it's often about issues with ASP and IIS. I think the code is fine. It' s identical to code working for others.
Other potential issues?
EDIT:
Is there a tool which allows me to get a for meaningful error message?
EDIT:
Never got this to work. Company changed to gmail as email provider and I did not get it to work either with my private gmail (proxy issues?) or my company email now linked to gmail ("Google Business"). However company has licensed Affixa chrome plugin. With that plugin it is possible do send email or better said affixa will open a dialog in gmail and you will have to press send manually.