powershell修改outlook模板,并且批量发送
本帖最后由 skyruanboy 于 2023-3-3 14:04 编辑最近换了一家外企,软件安全审核严格。因为powershell是系统自带的,直接用powershell开发能避免很多问题。powershell有完整的基础语法,要完成各项任务是没有问题的,就是网上的文档太少。
这次完成的是一个邮件批量发送。其中要保存邮件中的格式,并且要能替换里面的名字及密码等关键词,最后批量发送给每个人。
这里邮箱模板用的是outlook的msg,然后批量用的excel表格。
虽然用powershell,但是还是放不下UI,就简单设计了一下。文档太少,自己研究有局限性,如果有可以改进的地方,欢迎指正。
function gui-label {
param (
$x = 10, $y = 10, $text = "请输入:"
)
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size($x, $y)
$Label.Size = New-Object System.Drawing.Size(180, 20)
$Label.Text = $text
return $Label
}
function gui-textbox {
param (
$x = 10, $y = 10
)
$TextBox = New-Object System.Windows.Forms.TextBox
$TextBox.Location = New-Object System.Drawing.Size($x, $y)
$TextBox.Size = New-Object System.Drawing.Size(150, 50)
return $TextBox
}
function OutlookSend_accountInformation {
param (
$PathToMsg = "",
$userName = "none",
$Mail1 = "none",
$Mail2 = "none",
$CC = "",
$modify1 = "none",
$modify2 = "none"
)
$outlook = New-Object -comObject Outlook.Application
$session = $outlook.Session
$session.Logon()
$mail = $outlook.Session.OpenSharedItem($PathToMsg)
$mail.HTMLBody = $mail.HTMLBody -replace "modify1", $modify1
$mail.HTMLBody = $mail.HTMLBody -replace "modify2", $modify2
$mail.Forward()
$mail.Recipients.Add($Mail1)
$mail.Recipients.Add($Mail2)
$mail.CC = $CC
$mail.send()
}
function g-win {
Add-Type -AssemblyName system.windows.forms
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text = 'skyruan-outlook'
$main_form.Width = 400
$main_form.Height = 300
$main_form.AutoSize = $true
$Richtextbox = New-Object System.Windows.Forms.richtextbox
$Richtextbox.Location = New-Object System.Drawing.Point(10, 200)
$Richtextbox.Size = New-Object System.Drawing.Size(380, 20)
$Richtextbox.Height = 280
$main_form.Controls.Add($Richtextbox)
$main_form.Controls.Add((gui-label 10 12 "收件箱1"))
$TextBoxTo = (gui-textbox 220 10)
$main_form.Controls.Add($TextBoxTo)
$main_form.Controls.Add((gui-label 10 42 "收件箱2"))
$TextBoxCC1 = (gui-textbox 220 40)
$main_form.Controls.Add($TextBoxCC1)
$main_form.Controls.Add((gui-label 10 72 "抄送"))
$TextBoxCC2 = (gui-textbox 220 70)
$main_form.Controls.Add($TextBoxCC2)
$main_form.Controls.Add((gui-label 10 102 "修改1"))
$TextBoxName = (gui-textbox 220 100)
$main_form.Controls.Add($TextBoxName)
$main_form.Controls.Add((gui-label 10 132 "修改2"))
$TextBoxDate = (gui-textbox 220 130)
$main_form.Controls.Add($TextBoxDate)
$ButtonExcel = New-Object System.Windows.Forms.Button
$ButtonExcel.Location = New-Object System.Drawing.Size(60, 160)
$ButtonExcel.Size = New-Object System.Drawing.Size(120, 30)
$ButtonExcel.Text = "导入"
$main_form.Controls.Add($ButtonExcel)
$ButtonExcel.Add_Click(
{
$ExcelObj = New-Object -comobject Excel.Application
$ExcelObj.visible = $true
$ExcelWorkBook = $ExcelObj.Workbooks.Open("C:\1\1.xlsx")
$ExcelWorkSheet = $ExcelWorkBook.Sheets.Item("1")
#判断有多少行数据
for ($i = 1; $i -lt 100; $i++) {
if ($ExcelWorkSheet.Range("A" + $i).Text -eq "") {
$number = $i
break
}
else {}
}
for ($j = 1; $j -lt $number; $j++) {
$Mail1 = $ExcelWorkSheet.Range("A" + $j).Text
$Mail2 = $ExcelWorkSheet.Range("B" + $j).Text
$CC = $ExcelWorkSheet.Range("C" + $j).Text
$modify1 = $ExcelWorkSheet.Range("D" + $j).Text
$modify2 = $ExcelWorkSheet.Range("E" + $j).Text
OutlookSend_accountInformation "C:\1\1.msg" $Mail1 $Mail2 $CC $modify1 $modify2
$Richtextbox.AppendText("发送完毕" + "`n")
}
}
)
$ButtonSend = New-Object System.Windows.Forms.Button
$ButtonSend.Location = New-Object System.Drawing.Size(220, 160)
$ButtonSend.Size = New-Object System.Drawing.Size(120, 30)
$ButtonSend.Text = "发送"
$main_form.AcceptButton = $ButtonSend
$main_form.Controls.Add($ButtonSend)
$ButtonSend.Add_Click(
{
#逻辑和批量发送一样,这里就不填这么多了,这是为了方便只有一条数据的时候,不需要通过excel操作
}
)
$main_form.ShowDialog()
}
本帖最后由 hdyard 于 2023-3-3 11:55 编辑
编辑帖子的时候,里面有一个插入code,把你的code放进去就是正常的了,你试试
#
function gui-label {
param (
$x = 10,$y = 10, $text = "请输入:" ) $Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size($x, $y) $Label.Size = New-Object System.Drawing.Size(180, 20)
$Label.Text =$text
return $Label } function gui-textbox { param ( $x = 10, $y = 10 ) $TextBox = New-Object System.Windows.Forms.TextBox
$TextBox.Location = New-Object System.Drawing.Size($x, $y) $TextBox.Size = New-Object System.Drawing.Size(150, 50)
return $TextBox } function OutlookSend_accountInformation { param ( $PathToMsg = "",
$userName = "none", $Mail1 = "none",
$Mail2 = "none", $CC = "",
$modify1 = "none", $modify2 = "none"
)
$outlook = New-Object -comObject Outlook.Application $session = $outlook.Session $session.Logon()
$mail =$outlook.Session.OpenSharedItem($PathToMsg) $mail.HTMLBody = $mail.HTMLBody -replace "modify1",$modify1
$mail.HTMLBody =$mail.HTMLBody -replace "modify2", $modify2 $mail.Forward()
$mail.Recipients.Add($Mail1)
$mail.Recipients.Add($Mail2)
$mail.CC =$CC
$mail.send() } function g-win { Add-Type -AssemblyName system.windows.forms $main_form = New-Object System.Windows.Forms.Form
$main_form.Text = 'skyruan-outlook' $main_form.Width = 400
$main_form.Height = 300 $main_form.AutoSize = $true $Richtextbox = New-Object System.Windows.Forms.richtextbox
$Richtextbox.Location = New-Object System.Drawing.Point(10, 200) $Richtextbox.Size = New-Object System.Drawing.Size(380, 20)
$Richtextbox.Height = 280 $main_form.Controls.Add($Richtextbox) $main_form.Controls.Add((gui-label 10 12 "收件箱1"))
$TextBoxTo = (gui-textbox 220 10) $main_form.Controls.Add($TextBoxTo) $main_form.Controls.Add((gui-label 10 42 "收件箱2"))
$TextBoxCC1 = (gui-textbox 220 40) $main_form.Controls.Add($TextBoxCC1) $main_form.Controls.Add((gui-label 10 72 "抄送"))
$TextBoxCC2 = (gui-textbox 220 70) $main_form.Controls.Add($TextBoxCC2) $main_form.Controls.Add((gui-label 10 102 "修改1"))
$TextBoxName = (gui-textbox 220 100) $main_form.Controls.Add($TextBoxName) $main_form.Controls.Add((gui-label 10 132 "修改2"))
$TextBoxDate = (gui-textbox 220 130) $main_form.Controls.Add($TextBoxDate) $ButtonExcel = New-Object System.Windows.Forms.Button
$ButtonExcel.Location = New-Object System.Drawing.Size(60, 160) $ButtonExcel.Size = New-Object System.Drawing.Size(120, 30)
$ButtonExcel.Text = "导入" $main_form.Controls.Add($ButtonExcel) //会直接批量发送 $ButtonExcel.Add_Click(
{
$ExcelObj = New-Object -comobject Excel.Application $ExcelObj.visible = $true $ExcelWorkBook = $ExcelObj.Workbooks.Open("C:\1\1.xlsx") $ExcelWorkSheet = $ExcelWorkBook.Sheets.Item("1") for ($i = 1; $i -lt 100;$i++) {
if ($ExcelWorkSheet.Range("A" +$i).Text -eq "") {
$number =$i
break
}
else {}
}
for ($j = 1;$j -lt $number;$j++) {
$Mail1 =$ExcelWorkSheet.Range("A" + $j).Text $Mail2 = $ExcelWorkSheet.Range("B" +$j).Text
$CC =$ExcelWorkSheet.Range("C" + $j).Text $modify1 = $ExcelWorkSheet.Range("D" +$j).Text
$modify2 =$ExcelWorkSheet.Range("E" + $j).Text OutlookSend_accountInformation "C:\1\1.msg"$Mail1 $Mail2$CC $modify1$modify2
$Richtextbox.AppendText("发送完毕" + "`n") } } ) $ButtonSend = New-Object System.Windows.Forms.Button
$ButtonSend.Location = New-Object System.Drawing.Size(220, 160) $ButtonSend.Size = New-Object System.Drawing.Size(120, 30)
$ButtonSend.Text = "发送" $main_form.AcceptButton = $ButtonSend $main_form.Controls.Add($ButtonSend) $ButtonSend.Add_Click(
{
//逻辑和批量发送一样,这里就不填这么多了,这是为了方便只有一条数据的时候,不需要通过excel操作
}
)
$main_form.ShowDialog()
}
本帖最后由 skyruanboy 于 2023-3-3 11:45 编辑
预览都是正常的。但是第一次发,代码直接是乱的。第二次用MD编辑,中间有注释,也会变了。感觉发图不好复制。我试了好几次,放弃编辑了,复制后,改下格式,或者哪个大佬教我下怎么编辑,让代码顺序不会变。然后我把注释的#换成//了,要不然版面直接变了。 密码怎么填写? whisky2011 发表于 2023-3-3 11:32
密码怎么填写?
outlook必须打开状态下 hdyard 发表于 2023-3-3 11:53
编辑帖子的时候,里面有一个插入code,把你的code放进去就是正常的了,你试试
感谢,我重新编辑了 感谢分享
skyruanboy 发表于 2023-3-3 13:02
感谢,我重新编辑了
选择代码的时候不是也有提示么,你用的powershell直接选择powershell就可以,我看你选择的C#{:1_918:},选择powershell更好,哈哈哈哈 hdyard 发表于 2023-3-3 14:03
选择代码的时候不是也有提示么,你用的powershell直接选择powershell就可以,我看你选择的C#, ...
改了,我先没注意到有,就选了c#。 自动推送专用吗
页:
[1]
2