吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1535|回复: 14
收起左侧

[其他原创] powershell修改outlook模板,并且批量发送

[复制链接]
skyruanboy 发表于 2023-3-3 11:18
本帖最后由 skyruanboy 于 2023-3-3 14:04 编辑

最近换了一家外企,软件安全审核严格。因为powershell是系统自带的,直接用powershell开发能避免很多问题。powershell有完整的基础语法,要完成各项任务是没有问题的,就是网上的文档太少。
这次完成的是一个邮件批量发送。其中要保存邮件中的格式,并且要能替换里面的名字及密码等关键词,最后批量发送给每个人。
这里邮箱模板用的是outlook的msg,然后批量用的excel表格。
虽然用powershell,但是还是放不下UI,就简单设计了一下。文档太少,自己研究有局限性,如果有可以改进的地方,欢迎指正。
image.png

[PowerShell] 纯文本查看 复制代码
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() 
}

免费评分

参与人数 1热心值 +1 收起 理由
soyadokio + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

hdyard 发表于 2023-3-3 11:53
本帖最后由 hdyard 于 2023-3-3 11:55 编辑

编辑帖子的时候,里面有一个插入code,把你的code放进去就是正常的了,你试试
111.png

[PowerShell] 纯文本查看 复制代码
#
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()
}

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
skyruanboy + 1 + 1 我很赞同!

查看全部评分

 楼主| skyruanboy 发表于 2023-3-3 11:30
本帖最后由 skyruanboy 于 2023-3-3 11:45 编辑

预览都是正常的。但是第一次发,代码直接是乱的。第二次用MD编辑,中间有注释,也会变了。感觉发图不好复制。我试了好几次,放弃编辑了,复制后,改下格式,或者哪个大佬教我下怎么编辑,让代码顺序不会变。然后我把注释的#换成//了,要不然版面直接变了。
whisky2011 发表于 2023-3-3 11:32
 楼主| skyruanboy 发表于 2023-3-3 11:34

outlook必须打开状态下
 楼主| skyruanboy 发表于 2023-3-3 13:02
hdyard 发表于 2023-3-3 11:53
编辑帖子的时候,里面有一个插入code,把你的code放进去就是正常的了,你试试

感谢,我重新编辑了
312363738 发表于 2023-3-3 13:40
感谢分享
hdyard 发表于 2023-3-3 14:03
skyruanboy 发表于 2023-3-3 13:02
感谢,我重新编辑了

选择代码的时候不是也有提示么,你用的powershell直接选择powershell就可以,我看你选择的C#,选择powershell更好,哈哈哈哈
 楼主| skyruanboy 发表于 2023-3-3 14:06
hdyard 发表于 2023-3-3 14:03
选择代码的时候不是也有提示么,你用的powershell直接选择powershell就可以,我看你选择的C#, ...

改了,我先没注意到有,就选了c#。
alongzhenggang 发表于 2023-3-3 17:44
自动推送专用吗
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 00:25

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表