Sending mail from excel data to GMAILS using VBA Script

satyarankireddy

New Member
Joined
Dec 15, 2018
Messages
1
Hello Guru’s

I would require your help on below requirement. I am new in VBA script this is my first attempt.
Currently am sending mails one by one with manual change in below code. Now I am planning to change code in automation way.
I have excel with data as below.

SNONameMail IDAmountPDF attachement path
1vardhanvardhan@gmail.com1000D:\vardhan.pdf
2vihasvihas@gmail.com1000D:\vihas.pdf
3satyasatya@gmail.com1000D:\Satya.pdf
4varunvarun@gmail.com1000D:\Varun.pdf

<tbody>
</tbody>

Based on above cell values need to send mail each one with pdf attachment in separately with one click.
Currently am using below code changing values manually one by one. It is consuming time.

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") = 465
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") ="xxxx@gmail.com"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxx"

myMail.Configuration.Fields.Update

With myMail
.Subject = "Test Email from Dr.xxx"
.From = "xxx@gmail.com"
.To = "xxxx@gmail.com"
.CC = "xxx@gmail.com"
.BCC = ""
.TextBody = "Good morning!"
.AddAttachment "D:\xxx.txt"
End With
On Error Resume Next
myMail.Send
'MsgBox ("Mail has been sent")
Set myMail = Nothing

End Sub

Can you please help or provide me code which is similar for my requirement. Highly appreciated.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Assuming SNO, Name, etc. are on row 1, try the following modification:
Hello Guru’s

I would require your help on below requirement. I am new in VBA script this is my first attempt.
Currently am sending mails one by one with manual change in below code. Now I am planning to change code in automation way.
I have excel with data as below.

SNONameMail IDAmountPDF attachement path
1vardhanvardhan@gmail.com1000D:\vardhan.pdf
2vihasvihas@gmail.com1000D:\vihas.pdf
3satyasatya@gmail.com1000D:\Satya.pdf
4varunvarun@gmail.com1000D:\Varun.pdf

Based on above cell values need to send mail each one with pdf attachment in separately with one click.
Currently am using below code changing values manually one by one. It is consuming time.

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") = 465
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") ="xxxx@gmail.com"
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "xxxxx"

myMail.Configuration.Fields.Update

Dim i As Integer

for i = 1 to 4 step 1

With myMail
.Subject = "Test Email from Dr.xxx"
.From = "xxx@gmail.com"
.To = ActiveSheet.Cells(i+1,3)
.CC = "xxx@gmail.com"
.BCC = ""
.TextBody = "Good morning!"
.AddAttachment ActiveSheet.Cells(i+1,5)
End With
On Error Resume Next
myMail.Send
'MsgBox ("Mail has been sent")

Next i

Set myMail = Nothing

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,580
Members
449,039
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top