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
Dạ cảm ơn thầy