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
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