CDO mail

Fire_Chief

Well-known Member
Joined
Jun 21, 2003
Messages
693
Office Version
  1. 365
Platform
  1. Windows
I need the code to send CDO mail to a gmail account. I have tried many times to get

Ron de Bruins code to work but just can't get it.

VBA Code:
Sub SEND_PDF_SHEET_WITH_CDO()

Dim filepath As String

filepath = Environ$("temp") & "\" & ActiveWorkbook.Name & ".pdf" 'TODO:change filepath for the temp pdf file

    Range("A5:P39").Select                          ' In excel worksheet

    Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        filepath, _
        Quality:=xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
'Setting up CDOSYS configuration to send out the email
Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
    Set Flds = iConf.Fields
    With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30
    .Item("http://schemas.Microsoft.Com/cdo/configuration/smtpusetls") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "someone@somewhere.com"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = my password
    .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/smtpserverport") = 587
    .Update
    End With

    With iMsg
         Set .Configuration = iConf
        .From = "someone@somewhere.com"
       .To = Range("JA3").Value
         .Subject = Range("A346").Value
        .HTMLBody = Range("A350").Value
        .AddAttachment (filepath)
        .Send                                                       ' THIS IS WHERE I GET AN ERROR   = Run_time error '-2147220973 (80040213)':
    End With

    Set iMsg = Nothing
    Set iConf = Nothing
     Kill filepath
    Set iMsg = Nothing
    Set iConf = Nothing
     Kill filepath

End Sub


This is driving me crazy.
 
Last edited by a moderator:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
.
See if this makes a difference ...

VBA Code:
'If you have a GMail account then you can try this example to use the GMail smtp server
'The example will send a small text message
'You must change four code lines before you can test the code


'.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Full GMail mail address"
'.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "GMail password"


'Use your own mail address to test the code in this line
'.To = "Mail address receiver"


'Change YourName to the From name you want to use
'.From = """YourName"" (Reply@something.nl)"


'If you get this error : The transport failed to connect to the server
'then try to change the SMTP port from 25 to 465


'Possible that you must also enable the "Less Secure" option for GMail
'https://www.google.com/settings/security/lesssecureapps


Sub CDO_Mail_Small_Text_3()
    Dim iMsg As Object
    Dim iConf As Object
    Dim strbody As String
    Dim Flds As Variant
   
    Dim r As Range          ''**************************************************
    Set r = Worksheets("Sheet1").Range("F1:F59").SpecialCells(xlCellTypeVisible)  '**************************************
   
    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/smtpusessl") = True
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Full GMail mail address"
        .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "GMail password"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"


        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        .Update
    End With


    'strbody = "Hi there" & vbNewLine & vbNewLine & _
    '    "This is line 1" & vbNewLine & _
    '    "This is line 2" & vbNewLine & _
    '    "This is line 3" & vbNewLine & _
    '    "This is line 4"


    With iMsg
        Set .Configuration = iConf
        .To = "uremail@yahoo.com"
        .CC = ""
        .BCC = ""
        ' Note: The reply address is not working if you use this Gmail example
        ' It will use your Gmail address automatic. But you can add this line
        ' to change the reply address  .ReplyTo = "Reply@something.nl"
        .From = """YourName"" (Reply@something.nl)"
        .Subject = "Important message"
       
        .HTMLBody = strbody   'RangetoHTML(r) '****************************************************************************
       
        '.Addattachment "c:\temp\Scripty.zip"    ' --- edit path to file
        .Send
       
       
    End With
   
    Set iMsg = Nothing
    Set iConf = Nothing
    Set Flds = Nothing
End Sub
 
Upvote 0
Same error

Transport failed to connect to the sever
I also tried port 465
 
Upvote 0
Also :

1608082364251.png
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,825
Members
449,470
Latest member
Subhash Chand

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