Skip to main content

AccessTr.neT


Vba İle Mail Gönderimi

Vba İle Mail Gönderimi

#2
Bu makaleyi inceleyin.

Bu çerçevede aşağıdaki kodları kullanın.


Dim from, recipient, cc, bcc, password, subject, body, attachment As String, enable_html As Boolean
  
    from = "[email protected]" 'kendi email adresiniz
    recipient = "[email protected]" 'alıcı email adresi
    'cc = "" 'CC email alıcısı (istiyorsanız opsiyoneldir açıklamadan koda dönüştürün)
    'bcc = "" 'BCC email alıcısı (istiyorsanız opsiyoneldir açıklamadan koda dönüştürün)
    password = "app password" 'Gmail App passwordünüzü girin (2 faktorlü doğrulama etkinleştirme makalesindeki authentication kodu)
    subject = "xxx" 'Email konunuz
    body = "yyy" 'Mesajınız
    'attachment = "" 'eklerinizin yolu (istiyorsanız opsiyoneldir açıklamadan koda dönüştürün)
    'enable_html = False 'Mailinizin Html formatlı olmasını istiyorsanız True yapın (istiyorsanız opsiyoneldir açıklamadan koda dönüştürün)
   
    'buradan itibaren bir değişiklik yapmanız gerekmemektedir
    On Error GoTo Err
    Dim mailObj As Object
    Dim configObj As Object
    Dim fields As Variant
    Dim msConfigURL As String

    msConfigURL = "http://schemas.microsoft.com/cdo/configuration"

    Set mailObj = CreateObject("CDO.Message")
    Set configObj = CreateObject("CDO.Configuration")
    configObj.Load -1
    Set fields = configObj.fields
    With mailObj
        .subject = subject
        .from = from
        .to = recipient
        .cc = cc
        .bcc = bcc
        .Addattachment attachment
    End With

    If enable_html = True Then
        With mailObj
            .htmlbody = body
        End With
    Else
        With mailObj
            .textbody = body
        End With
    End If

    With fields
        'Enable SSL Authentication
        .Item(msConfigURL & "/smtpusessl") = True
        'Enable MTP authentication
        .Item(msConfigURL & "/smtpauthenticate") = 1
        'Set the SMTP server and port Details
        .Item(msConfigURL & "/smtpserver") = "smtp.gmail.com"
        .Item(msConfigURL & "/smtpserverport") = 465
        .Item(msConfigURL & "/sendusing") = 2
        .Item(msConfigURL & "/sendusername") = from
        .Item(msConfigURL & "/sendpassword") = password
        .Update
    End With

    mailObj.Configuration = configObj
    mailObj.Send

    Set mailObj = Nothing
    Set configObj = Nothing

    Exit Sub

    Exit_Err:
    Set mailObj = Nothing
    Set configObj = Nothing
    End

    Err:
    Select Case Err.Number
        Case -2147220973
            MsgBox "Error: İnternet bağlantınızı kontrol edin." & vbNewLine & Err.Number & ": " & Err.Description
        Case -2147220975
            MsgBox "Error: Adreslerinizi kontrol edin." & vbNewLine & Err.Number & ": " & Err.Description
        Case Else
            MsgBox "Bir hata oluştu." & vbNewLine & Err.Number & ": " & Err.Description
    End Select

    Resume Exit_Err

    End Sub
Cevapla

Bir hesap oluşturun veya yorum yapmak için giriş yapın

Yorum yapmak için üye olmanız gerekiyor

ya da

Bu Konudaki Yorumlar
Vba İle Mail Gönderimi - Yazar: sipuasfaf - 30/06/2022, 16:08
Re: Vba İle Mail Gönderimi - Yazar: atoykan - 30/06/2022, 18:26