[VBA Code]How to send emails from Excel without Outlook - KING OF EXCEL

KING OF EXCEL

KINGEXCEL.INFO ( KING OF EXCEL ) Welcome KINGEXCEL.INFO - Nothing Is Unable ... About Excel Tricks, Learning VBA Programming, Dedicated Software, Accounting, Living Skills ...

Tuesday, August 11, 2020

[VBA Code]How to send emails from Excel without Outlook

[VBA Code]How to send emails from Excel without Outlook
*****
Private Sub CommandButton1_Click()

    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/smtpusessl") = True
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "sender@thebigshot.co"
        .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.thebigshot.co"

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

    strbody = "<H3>Topic Here:</H3>" & _
            "Body here<BR>"
           
    'Paste selected range for formatted text
    Dim cRange As Range
    Set cRange = Worksheets("Sheet1").Range("A2:B8").SpecialCells(xlCellTypeVisible)
    cRange.Select
    cRange.Copy
    cRange.PasteSpecial  '???
    'Application.ScreenUpdating = False

    With iMsg
        Set .Configuration = iConf
        .To = "recepient@gthebigshot.co"
        .CC = ""
        .BCC = ""
        .From = """Sender Name"" <sender@thebigshot.co>"
        .Subject = "Subject here"
        .HTMLBody = strbody
        .send
       
    End With
    MsgBox ("Email has been sent successfully.")

End Sub

No comments:

Post a Comment