Nguyễn Đình Trọng
Nguyễn Đình Trọng
Thảo luận 2 thảo luận
Vỗ tay 0 vỗ tay
Lượt xem 277 lượt xem

cách gửi maill tự động file excell bằng lập trình VBA ?

Chào thầy! Cho em hỏi cách gửi maill tự động file excell bằng lập trình VBA ?
Thảo luận 2 câu trả lời
Lượt xem 277 lượt xem
Vỗ tay vỗ tay
Nguyễn Đình Trọng 13:12 - Dec 16, 2020

Chào bạn có hai cách sử dụng 1 là sử dụng outlook hai là sử dụng trực tiếp excel kết nối với gmail nhưng nhớ phải mở cài đặt gửi ứng dụng từ bên ngoài bạn có thể xem code tại đây:

bạn có thể xem code tại đây:

Option Explicit

' Cú pháp gui:Send_Email_With_Gmail(Emailgui, email nhan, subject, body, file dinh kem)

Sub Send_Email_With_Gmail(sMailFrom, sMailTo, sSubject, sBody, Optional sAttachment = Null)

    Dim newMail As Object

    Dim mailConfiguration As Object

    Dim msConfigURL As String

    Dim fields As Variant

    On Error GoTo errHandle

    ' https://myaccount.google.com/lesssecu...

    Set newMail = CreateObject("CDO.Message")

    Set mailConfiguration = CreateObject("CDO.Configuration")

    mailConfiguration.Load -1

    Set fields = mailConfiguration.fields

    With newMail

        .bodypart.Charset = "utf-8" '  h? tr? UTF-8 - ti?ng Vi?t

        .Subject = sSubject

        .From = sMailFrom

        .To = sMailTo

        .CC = ""

        .BCC = ""

        .TextBody = sBody

        If (sAttachmet <> Null) Then

            .AddAttachment sAttachment

        End If

    End With

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

    With fields

        .Item(msConfigURL & "/smtpusessl") = True

        .Item(msConfigURL & "/smtpauthenticate") = 1

        .Item(msConfigURL & "/smtpserver") = "smtp.gmail.com"

        .Item(msConfigURL & "/smtpserverport") = 465

        .Item(msConfigURL & "/sendusing") = 2

        .Item(msConfigURL & "/sendusername") = Sheet1.Range("A1").Value ' <- Thay d?a ch? email c?a b?n vào dây

        .Item(msConfigURL & "/sendpassword") = Sheet1.Range("A2").Value ' <- Thay m?t kh?u email c?a b?n vào dây

        .Update

    End With

    newMail.Configuration = mailConfiguration

    newMail.Send

    Debug.Print "E-Mail has been sent to " & sMailTo, vbInformation

exit_line:

    '// Xoá object, ti?t ki?m b? nh?

    Set newMail = Nothing

    Set mailConfiguration = Nothing

    Exit Sub

errHandle:

    MsgBox "Error: " & Err.Description, vbInformation

End Sub

Sub guiemail()

   Call Send_Email_With_Gmail("gmail gui", "mail nhan", "sSubject", "bodi")

End Sub


Vỗ tay vỗ tay
Nguyễn Đình Trọng 13:12 - Dec 16, 2020

Dạ cảm ơn thầy

Vỗ tay vỗ tay
Câu hỏi liên quan
© 2020 - Bản quyền của Công Ty Cổ Phần Công Nghệ Giáo Dục Gitiho Việt Nam
Giấy chứng nhận Đăng ký doanh nghiệp số: 0109077145, cấp bởi Sở Kế hoạch và Đầu tư TP. Hà Nội
Giấy phép mạng xã hội số: 588, cấp bởi Bộ Thông tin và Truyền thông