I'm trying to create a VBA script which uses the Google server but company domain. A typical example would be firstname#company.com.
I'm using the code below but it gives me an error if I use a domain name that's not gmail. I want to ask the user to enter their login information (username and password).
Sub SendGMail()
' Object creation
Set objMsg = CreateObject("CDO.Message")
Set msgConf = CreateObject("CDO.Configuration")
' Server Configuration
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = UserForm1.TextBox1.Value
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = UserForm1.TextBox2.Value
msgConf.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = 1
msgConf.Fields.Update
' Email
objMsg.To = UserForm1.TextBox1.Value
objMsg.From = UserForm1.TextBox1.Value
objMsg.Subject = "Test send with Gmail account"
objMsg.HTMLBody = "HTML/Plain text message."
objMsg.Sender = "Mr. Name"
Set objMsg.Configuration = msgConf
' Send
objMsg.Send
' Clear
Set objMsg = Nothing
Set msgConf = Nothing
End Sub
You need to add Microsoft CDO Reference before running the code
Tool-->Reference---> Microsoft CDO for Windows 2000 Library
Sub MailTest()
Dim Mail As New Message
Dim Config As Configuration
Set Config = Mail.Configuration
Config(cdoSendUsingMethod) = cdoSendUsingPort
Config(cdoSMTPServer) = "smtp.gmail.com"
Config(cdoSMTPServerPort) = 25
Config(cdoSMTPAuthenticate) = cdoBasic
Config(cdoSMTPUseSSL) = True
Config(cdoSendUserName) = InputBox("EnterUsername")
' Otherwise if you use text box
'config(cdoSendUserName) = UserForm1.TextBox1.Text
Config(cdoSendPassword) = InputBox("Password")
' Otherwise if you use text box
'config(cdoSendUserName) = UserForm1.TextBox2.Text
Config.Fields.Update
Mail.To = "yahoo44#gmail.com"
Mail.From = Config(cdoSendUserName)
Mail.Subject = "Email Subject"
Mail.HTMLBody = "<b> mail body </b>"
Mail.Sender = " MRSender"
Mail.Send
MsgBox "mail has been sent"
End Sub
Related
I need to periodically send e-mails and I used the following code for years:
Function SSLSendMail(sTO As String, sFROM As String, sSubject As String, sText As String, sServer As String, _
sUser As String, sPassword As String, Optional sAttach As String, Optional sCC As String, Optional sBCC As String) As Boolean
Dim ObjSendMail As Object
Set ObjSendMail = CreateObject("CDO.Message")
Dim iConf As Object
Set iConf = CreateObject("CDO.Configuration")
With iConf.Fields 'Flds
.Item(cdoSendUsingMethod) = cdoSendUsingPort ' 2
.Item(cdoSMTPAuthenticate) = cdoBasic ' 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True ' If use SSL set to True, if not, set to False
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Item(cdoSendUserName) = sUser
.Item(cdoSendPassword) = sPassword
.Item(cdoSMTPServer) = sServer
.Item(cdoSMTPConnectionTimeout) = 10
.Update
End With
Set ObjSendMail.Configuration = iConf
With ObjSendMail
.To = sTO
.Subject = sSubject
.from = sFROM
.TextBody = sText
If sAttach > "" Then
.AddAttachment sAttach
End If
If sCC > "" Then
.cc = sCC
End If
If sBCC > "" Then
.BCC = sBCC
End If
On Local Error GoTo invalidTo
.Send
On Local Error GoTo 0
End With
Set ObjSendMail = Nothing
Set iConf = Nothing
SSLSendMail = True
Exit Function
invalidTo:
Debug.Print "Error on " & sTO & ": " & Err.description
Err.Clear
SSLSendMail = False
End Function
In the last days, I get error 0x80040217 and the mail is not sent. After some investigation, I found that Google changed the authentication method from Basic to OAuth2 or, at least, that is what I have to change in my Thunderbird client in order to be able to send mails.
I have seen an answer to a similar question suggesting to use Outlook object, but that is not applicable for a series of reasons: I don't have any outlook user configured (and I don't want to!), the "sender" seems not dynamically specified, etc.
I guess I have to change .Item(cdoSMTPAuthenticate) = cdoBasic to something else, but I cannot find anything related to OAuth2.
After days and days of unsuccessfully testing changes in that code, I finally realized the problem was in the SMTP server only. I had to access my Google configuration page, ask for a second level security for the specific machine on which the Excel VBA is running and I got an encrypted password, based on my regular password, I guess. Using that password, the above code works fine, as before. Thanks Google for making our life more difficult!
I have a project that i want to ditribute.
I would like it to autosend me a mail on error.
I tried here with CDO library, but i am not sure it is the best to use.
I have :
Private Sub MyErrorHandler()
'Here i save the file to send it next
Dim path As String: path = Environ("AppData") + "\Local"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs path & "AI_ErroringFile.xls"
Application.DisplayAlerts = True
'Here i create my email
Dim Cdo_Message As New CDO.Message
Set Cdo_Message.Configuration = GetSMTPGmailServerConfig()
With Cdo_Message
.To = "myadress#yahoo.fr"
.From = Environ("username") + "#ErrorHandler.com"
.Subject = "Test"
.TextBody = "Hello"
.send
End With
Set Cdo_Message = Nothing
End Sub
and my gmail config function :
Function GetSMTPGmailServerConfig() As Object
Dim Cdo_Config As New CDO.Configuration
Dim Cdo_Fields As Object
Set Cdo_Fields = Cdo_Config.Fields
With Cdo_Fields
.item(cdoSendUsingMethod) = cdoSendUsingPort
.item(cdoSMTPServer) = "smtp.gmail.com"
.item(cdoSMTPServerPort) = 465
.item(cdoSendUserName) = "AIErrorHandler#gmail.com"
.item(cdoSendPassword) = "mypassword"
.item(cdoSMTPAuthenticate) = cdoBasic
.item(cdoSMTPUseSSL) = True
.Update
End With
But on the .send instruction i have this error :
sendusing configuration value is invalid
I have try the 3 possibles sendusing configurations without success.
I am looking for a way to fix this or to send an email with vba without using outlook
hello i tried to send email in excel
here is my code
Sub send_email_via_Gmail()
Dim myMail As CDO.Message
Set myMail = New CDO.Message
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "example#gmail.com"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
myMail.Configuration.Fields.Update
With myMail
.Subject = "Test Email from Dr. Takyar"
.From = "example#gmail.com"
.To = "example1#gmail.com"
.TextBody = "Good morning!"
End With
myMail.Send
MsgBox ("Mail has been sent")
Set myMail = Nothing
End Sub
but when i run this it is giving me runtime error
when i try to send mail..
for me coding seems correct .. i dont know what is the issue ..
please help
This is really only my second VBS script so be gentle... I did **** out anything that was personal or company related I am sure all those fields are correct anyways. The SMTP server is correct I double checked with the provider as that was the number 1 reason i found on other sites. This script will also pull information from a certain cell and paste it into the body... Any help would be greatly appreciated! Also its saying the error is on line 46 which is the "ObjSendMail.Send". Everything works except the emailing portion...
Dim ObjSendMail
Set ObjSendMail = CreateObject("CDO.Message")
Set objExcel = CreateObject("Excel.Application")
StopDate = DateAdd("d", -1 - Weekday(Date), Date)
StartDate = StopDate-13
Dim xlApp
Dim xlWkb
Dim monthEnd
Set xlApp = CreateObject("excel.application")
Set xlWkb = xlApp.Workbooks.Open("******")
xlWkb.RunAutoMacros 1
xlApp.Run ("UpdateAll")
monthEnd = xlApp.cells(2,7).value
xlApp.ActiveWorkbook.SaveAs strSaveFile & "Monthly Revenue Report " & Year(Now) & "." & Month(Now) & "." & Day(Now) & ".xls", 56
xlApp.Quit
Set xlWkb = Nothing
Set xlApp = Nothing
WScript.Sleep 10000
mailSubject = "Monhtly Revenue Report " & PrevMonthName
mailBody = "The Monthly Revenue Report is no ready. Month End: " & monthEnd
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.office365.com"
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 240
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "********"
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "********"
ObjSendMail.Configuration.Fields.Update
ObjSendMail.To = "*********"
ObjSendMail.Subject = mailSubject
ObjSendMail.From = "*******"
'ObjSendMail.HTMLBody = "this is the body"
ObjSendMail.TextBody = mailBody
ObjSendMail.Send
'Set ObjSendMail = Nothing
When in doubt, read the documentation. Office365 uses the submission port (587/tcp) for mail submission. Replace this:
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
with this:
ObjSendMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587
and the error should go away (provided that outbound connections to port 587/tcp are allowed on your network).
You can test accessibility of the port with a port scanner like nmap, scanline, or PortQry), or manually with telnet:
telnet smtp.office365.com 587
Outbound connections to port 25/tcp are most likely blocked by your provider, as a measure to prevent/reduce botnet spam.
The following code worked for smtp.office365.com. You DO indicate smtpusessl=true, but you do NOT specify the port, otherwise you get error 5.7.57.
Sub SMPTTest2()
Set emailObj = CreateObject("CDO.Message")
emailObj.From = "name#myaddress.com"
emailObj.To = "name#youraddress.com"
emailObj.Subject = "Test CDO"
emailObj.TextBody = "Test CDO"
'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"
'Exclude the following line
'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#myaddress.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
emailConfig.Fields.Update
emailObj.Send
If Err.Number = 0 Then MsgBox "Done"
End Sub
From Unknown email code from CDO.Message send method
CDO_E_FAILED_TO_CONNECT 0x80040213L The transport failed to connect to the server.
If using SSL the port is normally 465.
My client wants to distribute excel/vba to his clients and the vba will automatically send emails.
Maybe the sender should be some other account, not the outlook account of the person using the vba, because some private stuff might be in the email. Is it really possible?
Another thing is the infamous pop-up warning when automating such tasks. I heard Application.SendKeys doesn't always work when computer's locked.
How is CDO for this task?
On your initial question you can use MailItem.SentOnBehalfOfName with Outlook
On the Security warning the standard two solutions for Outlook are:
1) Use Clickyes
2) Install Outlook Redemption
You don't have to use Outlook to send emails. As you ask, CDO works without using Outlook.
Here's some code to get you started.
Public Sub SendEmail(Subject As String, Body As String, ToPerson as String)
Dim iCfg As Object
Dim iMsg As Object
Set iCfg = CreateObject("CDO.Configuration")
Set iMsg = CreateObject("CDO.Message")
With iCfg.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.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") = "email-account"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
.Item("http://schemas.microsoft.com/cdo/configuration/sendemailaddress") = "account#domain.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Update
End With
With iMsg
.Configuration = iCfg
.Subject = Subject
.TextBody = Body
.To = ToPerson
.Send
End With
Set iMsg = Nothing
Set iCfg = Nothing
End Sub