Send email through GMAIL instead of OUTLOOK

Josiah

Board Regular
Joined
Apr 10, 2008
Messages
178
I have a VBA code linked to a funtion (if that's the right terminology) that will auto-email a generated PDF using Microsoft Outlook. I'd like to change the method from Outlook to web-based Gmail. Can the following function easily be changed to allow for this or is it a bit more plicomcated?

Code:
Function RDB_Mail_PDF_Outlook(FileNamePDF As String, StrTo As String, _                              StrCC As String, StrSubject As String, StrBody As String, Send As Boolean)
    
    If Range("Z1") = True Then Exit Function
    
    Dim OutApp As Object
    Dim OutMail As Object


    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)


    On Error Resume Next
    With OutMail
        .To = StrTo
        .CC = StrCC
        .BCC = ""
        .Subject = StrSubject
        .Body = Range("C23").Value
        .Attachments.Add FileNamePDF
        If Send = True Then
            .Send
        Else
            .Display
        End If
    End With
    On Error GoTo 0


    Set OutMail = Nothing
    Set OutApp = Nothing
End Function
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I have a VBA code linked to a funtion (if that's the right terminology) that will auto-email a generated PDF using Microsoft Outlook. I'd like to change the method from Outlook to web-based Gmail. Can the following function easily be changed to allow for this or is it a bit more plicomcated?

Code:
Function RDB_Mail_PDF_Outlook(FileNamePDF As String, StrTo As String, _                              StrCC As String, StrSubject As String, StrBody As String, Send As Boolean)
    
    If Range("Z1") = True Then Exit Function
    
    Dim OutApp As Object
    Dim OutMail As Object


    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)


    On Error Resume Next
    With OutMail
        .To = StrTo
        .CC = StrCC
        .BCC = ""
        .Subject = StrSubject
        .Body = Range("C23").Value
        .Attachments.Add FileNamePDF
        If Send = True Then
            .Send
        Else
            .Display
        End If
    End With
    On Error GoTo 0


    Set OutMail = Nothing
    Set OutApp = Nothing
End Function


Josiah,
Here is some code I have used to send a pdf via gmail...maybe you can adapt it.
Perpa
Code:
Sub CDO_Mail_Small_Text()
    Dim iMsg As Object
    Dim iConf As Object
    Dim strbody As String
    Dim Flds As Variant
    Dim PDFfileName As String
    
    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/smtpusessl") = True
       
        '.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1

        .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "yourUsername@gmail.com"
        .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
        .Update
    End With

    PDFfileName = "completepathandfilename.pdf"   'I generally put this in a cell, ie. PDFfilename = cells(1,1)

    strbody = "Automated Test email from" & vbNewLine & vbNewLine & "Your Name"    'this could be in a cell also, ie. strbody = cells(2,1)
    
    With iMsg
        Set .Configuration = iConf
        .To = ""    'Put your list of email addresses here       'this could be a cell reference where all the addresses are separated by a semi-colon
        .CC = ""   'Put your list of emails to copy to here      'this could be a cell reference
        .BCC = ""
        .From = "yourUsername@gmail.com"
        .Subject = "Your Subject"                                           'this could be a cell reference
        .TextBody = strbody
        .AddAttachment PDFfileName
        .Send
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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