[玩转系统] 使用 Excel VBA 宏或 PowerShell 发送 Outlook 电子邮件
作者:精品下载站 日期:2024-12-15 00:50:03 浏览:18 分类:玩电脑
使用 Excel VBA 宏或 PowerShell 发送 Outlook 电子邮件
有一项任务是根据 Excel 电子表格中的用户电子邮件列表来组织邮件。每封电子邮件都应包含特定于每个用户的一些数据,并且还应附加个人文件。在本文中,我们将了解如何使用 Outlook 配置文件使用 VBA 宏或 PowerShell 脚本自动将电子邮件从 Excel 文件发送到收件人列表。
重要。必须在您的计算机上为两种发送电子邮件的方法配置 Outlook 邮件配置文件。该邮箱(以及该电子邮件地址)将用于发送消息
假设您有一个包含以下列的 Excel 文件:
Email | Full Name | Last Password Change Date | Account status
我的任务是使用此模板向 Excel 列表中的每个人发送电子邮件:
主题:您在 a-d.site 域中的帐户状态
正文:亲爱的 %FullUsername%,
您在 a-d.site 域中的帐户处于 %status% 状态
最后一次更改密码的日期和时间是 %pwdchange%
Excel VBA 宏通过 Outlook 发送电子邮件
这是一个小型 VBA(Visual Basic for Applications)邮件宏,可以直接在 Excel 文档中创建。
创建一个新宏:查看 -> 宏。指定宏的名称 (send_email) 并单击创建:
将以下代码复制并粘贴到出现的 VBA 编辑器中(我已向其中添加了所有必要的注释)。为了自动发送电子邮件,我将使用
CreateObject (“Outlook.Application”)
函数,它允许在 VBA 脚本中创建和使用 Outlook 对象。
Sub send_email()
Dim olApp As Object
Dim olMailItm As Object
Dim iCounter As Integer
Dim Dest As Variant
Dim SDest As String
' Subject
strSubj = "Your account status on a-d.site domain"
On Error GoTo dbg
' Create a new Outlook object
Set olApp = CreateObject("Outlook.Application")
For iCounter = 2 To WorksheetFunction.CountA(Columns(1))
' Create a new item (email) in Outlook
Set olMailItm = olApp.CreateItem(0)
strBody = ""
useremail = Cells(iCounter, 1).Value
FullUsername = Cells(iCounter, 2).Value
Status = Cells(iCounter, 4).Value
pwdchange = Cells(iCounter, 3).Value
'Make the body of an email
strBody = "Dear " & FullUsername & vbCrLf
strBody = strBody & " Your account in a-d.site domain is in" & Status & “ state” & vbCrLf
strBody = strBody & "The date and time of the last password change is" & pwdchange & vbCrLf
olMailItm.To = useremail
olMailItm.Subject = strSubj
olMailItm.BodyFormat = 1
' 1 - text format of an email, 2 - HTML format
olMailItm.Body = strBody
'Add an attachment (filename format is [email protected]). Comment out the following line if you do not need the attachments
olMailItm.Attachments.Add ("C:\ps\" & useremail & ".txt")
olMailItm.Send
Set olMailItm = Nothing
Next iCounter
Set olApp = Nothing
dbg:
'Display errors, if any
If Err.Description <> "" Then MsgBox Err.Description
End Sub
将此 Excel 文件另存为 .xlsm(一种支持宏的 Excel 工作簿格式)。要发送电子邮件,请选择您创建的过程(宏),然后单击运行。
VBA 宏循环访问 Excel 电子表格中的所有行,生成一条消息并将其发送给列表中的每个收件人。已发送的电子邮件存储在 Outlook 的已发送邮件文件夹中。
如果您想要代表其他用户或共享邮箱(如果您使用的是 Exchange)发送电子邮件,则必须授予 SendAs/Sent 代表权限,并将以下代码添加到脚本中(在
olMailItm.Send
)。
olMailItm.SentOnBehalfOfName = "[email protected]"
使用 PowerShell 从 Outlook 发送电子邮件
在 PowerShell 中,您可以使用 Send-MailMessage cmdlet 发送电子邮件。但是,它要求您对邮件服务器进行身份验证,并且不支持现代身份验证方法,例如 OAuth 和 Microsoft 现代身份验证。因此,如果您的计算机上配置了 Outlook 配置文件,则发送电子邮件会更加容易。
以下是一个 PowerShell 脚本示例,该脚本从 Excel 文件读取数据并使用 Outlook 配置文件向每个用户发送电子邮件:
# open the Excel file
$ExcelObj = New-Object -comobject Excel.Application
$ExcelWorkBook = $ExcelObj.Workbooks.Open("C:\PS\user_list.xlsx")
$ExcelWorkSheet = $ExcelWorkBook.Sheets.Item("Sheet1")
# Get the number of filled rows in an xlsx file
$rowcount=$ExcelWorkSheet.UsedRange.Rows.Count
# Loop through all the rows in column 1, starting from the second row (these cells contain the usernames and e-mails).
for($i=2;$i -le $rowcount;$i++){
$useremail = $ExcelWorkSheet.Columns.Item(1).Rows.Item($i).Text
$FullUsername = $ExcelWorkSheet.Columns.Item(2).Rows.Item($i).Text
$Status = $ExcelWorkSheet.Columns.Item(4).Rows.Item($i).Text
$pwdchange = $ExcelWorkSheet.Columns.Item(3).Rows.Item($i).Text
# Generate message body text
$strSubj = " Your account status on a-d.site domain "
$strBody = "Dear " + $FullUsername
$strBody = $strBody + " `r`n Your account in a-d.site domain is in " + $Status
$strBody = $strBody + "`r`n The date and time of the last password change is : " + $pwdchange
$strfile="C:\ps\" + $useremail + ".txt"
# We assume that Outlook is running, if it is not you will need to start it with the command $outlook = new-object -comobject outlook.application
$outlook = [Runtime.InteropServices.Marshal]::GetActiveObject("Outlook.Application")
$email = $outlook.CreateItem(0)
$email.To = $useremail
$email.Subject = $strSubj
$email.Body = $strBody
# Attach a file (if necessary)
$email.Attachments.add($strfile)
#send the e-mailmessage
$email.Send()
}
$ExcelWorkBook.close($true)
此 PowerShell 脚本假定 Outlook 正在您的计算机上运行。该脚本为 XLSX 文件中的每个收件人 SMTP 地址生成电子邮件的主题和正文,并附加该文件。然后发送电子邮件。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[电影] 黄沙漫天(2025) 4K.EDRMAX.杜比全景声 / 4K杜比视界/杜比全景声
[风口福利] 短视频红利新风口!炬焰创作者平台重磅激励来袭
[韩剧] 宝物岛/宝藏岛/金银岛(2025)【全16集】【朴炯植/悬疑】
[电影] 愤怒的牦牛 (2025) 国语中字 4k
[短剧合集] 2025年05月30日 精选+付费短剧推荐56部
[软件合集] 25年5月30日 精选软件26个
[软件合集] 25年5月29日 精选软件18个
[短剧合集] 2025年05月28日 精选+付费短剧推荐38部
[软件合集] 25年5月28日 精选软件37个
[软件合集] 25年5月27日 精选软件26个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[办公模版] office模板合集:包含word、Excel、PowerPoint、Access四类共计2000多个模板
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[音乐] 华语流行伤感情经典歌无损音乐合集(700多首)
[影视] 内地绝版高清录像带 [mpg]
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag