Nguyễn Hoàng 
Nguyễn Hoàng 
Thảo luận 1 thảo luận
Vỗ tay 0 vỗ tay
Lượt xem 438 lượt xem

Em muốn đính kèm file attach theo đường dẫn vào mail outlook thì mình dùng code

Hi Thầy

Em muốn đính kèm file attach theo đường dẫn vào mail outlook thì mình dùng code nào vậy thầy


Thảo luận 1 câu trả lời
Lượt xem 438 lượt xem
Vỗ tay vỗ tay
Nguyễn Hoàng  09:11 - Nov 11, 2020

Chào bạn bạn phải biết được đường đẫn bạn nhé. hoặc bạn có thể dùng code sau:

lưu ý nhớ vào https://myaccount.google.com/lesssecu... tích gửi gmail kém an toàn nhé.

phần sAttachment bạn lấy đường dẫn vào nhé.

Sub guiemail()

   Call Send_Email_With_Gmail("mailgui", "mailnhan", "Gitiho", "alo12345")

End Sub

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

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