Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) | Reguli | Inregistrare | Login

POZE MRRRR'S FORUM (VIEW ONLY)

Nu sunteti logat.
Nou pe simpatie:
sweetandana la Simpatie.ro
Femeie
25 ani
Teleorman
cauta Barbat
25 - 48 ani
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides / [MAIL] Send Email from excel using Gmail Account via SMTP Moderat de TRaP, TonyTzu
Autor
Mesaj Pagini: 1
TonyTzu
Moderator

Inregistrat: acum 12 ani
Postari: 252
I used the code below to successfully send an email using my gmail account via SMTP

Main source:
Fixing various errors: I used multiple sources found with googling the error message

Note:
You might be required to enable access to "Less Secure Apps" from the security section of your gmail account settings.

Code:

Sub CDO_Mail_Small_Text()
' https://www.rondebruin.nl/win/s1/cdo.htm

' Why using CDO code instead of Outlook automation or SendMail in VBA.
'1: It doesn 't matter what Mail program you are using (It only use the SMTP server).
'2: It doesn 't matter what Office version you are using (97-2016)
'3: You can send a range/sheet in the body of the mail (some mail programs can’t do this)
'4: You can send any file you like (Word, PDF, PowerPoint, TXT files, etc.)
'5: No Security warnings, really great if you are sending a lot of mail in a loop.

'This code will not work in Windows 98 and ME and will also not working on your Mac.
'You must be connected to the internet when you run an example.

'It is possible that you get a Send error when you use one of the examples. AFAIK : This will happen if you haven't setup an account in Outlook Express or Windows Mail. In that case the system doesn't know the name of your SMTP server. If this happens you can use the commented lines in each example.
'Don 't forget to fill in the SMTP server name in each code sample where it says "Fill in your SMTP server here"
'When you also get the Authentication Required Error you can add this three lines.
'.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
'.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "username"
'.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"

'Don't remove the TextBody line in the code. If you do you can't open the attachment (bug in CDO).
'If you don't want to have text in the body use this then .TextBody = ""

'Note: It is possible that your firewall blocks the code (Check your firewall settings)

    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/smtpauthenticate") = 1
            .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "YOUR EMAIL ACCOUNT"
            .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "YOUR PASSWORD"
            .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
            .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") = 465  'If you get this error : The transport failed to connect to the server then try to change the SMTP port to 25 or 587
            .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 = "EMAIL YOU WANT TO SEND TO"
        .CC = ""
        .BCC = ""
        .From = """Tony"" <type your email account>"
        .Subject = "EMAIL SUBJECT / TITLE"
        .TextBody = strbody
        .Send
    End With

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



pus acum 6 ani
   
Pagini: 1  

Mergi la