工具引用:Microsoft Outlook 16.0 Object Librar
示例代码:
Attribute VB_Name = "模块1"
Option Explicit
Sub sendEmail()
Dim objOutlook As Outlook.Application
Dim objMail As MailItem
Dim strFilePath As String
Set objOutlook = New Outlook.Application '创建objOutlook为Outlook应用程序对象
Set objMail = objOutlook.CreateItem(olMailItem) '创建objMail为一个邮件对象
With objMail
.To = "xxx@xxx.com" '收件人
.CC = "xxx@xxx.com" '抄送
.Subject = "this is a test" '标题
.Body = "Test for auto send email" '正文
' 获取当前工作目录路径
strFilePath = ThisWorkbook.Path & "\test_import.txt"
.Attachments.Add strFilePath,olByValue, 1, "4th Quarter 1996 Results Chart" '附件
.Save '保存为草稿
.SentOnBehalfOfName = "xxx@xx.com" '发送人
' .Send '发送
End With
Set objMail = Nothing
Set objOutlook = Nothing
End Sub