How to Send Mail from Excel VBA Script?

We found many people searching for VBA script to send SMTP email without using outlook or any other application. We have written a Macro and also shared the code below to send email from your itself.

Although most of the purposes of sending emails through Excel are solved with Mail Merge but it could be useful for sending plain emails in bulk with less efforts or could be used as a module in Excel VBA application for sending email. Download the Excel Mail sending Macro from here or use the written code in this page.

Download Excel Mail Aensinf Macro

You may also use below written code. All you need to do is open VBA Editor using ALT + F11 (Press Fn key also if your keyboard has.) shortcut key, Click on insert and insert a module. Copy and paste the below code and change following values:

  1. Replace smtp.example.com with your server SMTP server address (E.g. smtp.gmail.com for Gmail Accounts, smtp.mail.yahoo.com for Yahoo Accounts)
  2. Replace 465 with your SMTP Server Port (E.g. 465 / 587 / 25). These ports could vary according to Encryption Type. Try using anyone of three.
  3. User Email ID should be your email id (E.g. example@gmail.com)
  4. Replace sender_password with your password
  5. Replace receiver@example.com with email ID to whom you want to send email and sender@example.com should be same as we used earlier.

Note: Please make sure your account does not have Two Factor Authentication Enabled. Also if you are using Gmail account, "Allow less secure apps" in Sign in and secutiry settings should be ON.

Sub CDO_Mail_Small_Text()
Dim iMsg As Object
Dim iConf As Object
Dim strbody As String
Dim Flds As Variant

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

iConf.Load -1 ' CDO Source Defaults
Set Flds = iConf.Fields

With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "sender@example.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "sender_password"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Update
End With

strbody = "Hey There," & vbNewLine & vbNewLine & _ "This is a test message from VBA Excel Mail sending Program. Thank you for using me." & vbNewLine & _
"Thank you" & vbNewLine & _
"Regards"

With iMsg
Set.Configuration = iConf
.To = "receiver@example.com"
.From = "sender@example.com"
.Subject = "Mail Subject"
.TextBody = strbody
.Send
End With

Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing
End Sub

The above code could be used for sending bulk email by just listing a list of email accounts in excel and looping through it.