把creating mail代码写到调用处,往outbox写入 mailxxx.csv文件,入面记录了邮件的主要内容

写入

	#template
$TMP = IMPORT-CSV "$($dh)\mail\TEMPLATES\MAIL.CSV"
#customization
$TMP.CC ="" #
$TMP.Subject ="From Server $($( get-wmiobject Win32_ComputerSystem ).Name) By APOMS_TSQL_Deployment_Package $(get-date)." # test1
$TMP.Body ="$msg`nPlease Check The attachement for details. `nThanks`nBest Regards." # test
##clear
if(Test-Path "$logfile.zip" ){
Remove-Item -Path "$logfile.zip" -Force|&$log
}
#create zip
"zipping log.."
export-Zip -sourcefiles "$logfile" -zipfilename "$logfile.zip"|&$log
#add attachements
$TMP.Attachments ="$logfile.zip,$($dh)\Mail\msg.txt" #
#wirte-outbox,output mailxxx.csv
ConvertTo-Mail -path "$($dh)\mail\outbox\MAIL$t.CSV" -mail $tmp

发送,调用send-avmail.ps1发送邮件

."$($dh)\Mail\Send-AvEmail.ps1" 

sample

cls;
$dh = $pwd.path
#$PSScriptRoot =Split-Path -Parent $MyInvocation.MyCommand.Definition
$pv = $psversiontable.psversion.major
$strDate = Get-Date -Format "yyyyMMdd"
$logfile = "$($dh)\log\log$($strDate).txt"
$log = {$input|Tee-Object -FilePath $logfile -Append}
$filename = ''
$rem = {$i = '';1..75|%{$i += '-'};$i }
#getting order
"start deploying `nat $(get-date)"|&$log
"reading configuration file..."|&$log
$content = ( Get-Content "$($dh)\module\config_order"| Out-String )
$hash = ( Invoke-Expression $content )
#$hash
#sql parameters
$content = ( Get-Content "$($dh)\module\config_sys"| Out-String )
$hashsys = ( Invoke-Expression $content )
&$rem|&$log
try{
#Display
#sorting hashtable
$hash.getEnumerator()|sort name|&$log
&$rem|&$log
"echo The Depoly will start immediately" |&$log
sleep 1|&$log
&$rem|&$log
#hit by order foreach($e in $hash.getEnumerator()|sort name){
if($dh -ne $pwd.path){
Set-Location $dh
} $key = $e.value
"........................$key............." |&$log
$cmd = "$($dh)\module\RunSqlcmdByKey.ps1 "
$dh|&$log
Invoke-Expression -Command $cmd |&$log
} }
catch {
"$(Get-Date) error:$_.Exception"|&$log
}finally{
"finish deploying at $(get-date)"|&$log
$title = "Send mail"
$message = "Do you want to Send the logfile to isoftstone?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Send the logfile to isoftstone."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Continue."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
switch ($result)
{
0 {
#import mail moudle
"creating attachement"
Import-Module "$($dh)\mail\MailModule.psm1" -force
$t = Get-Date -Format 'yyyyMMdd_hh_mm_ss'
."$($dh)\Mail\msg.ps1" "creating mail"
$msg ="FYI."
#template
$TMP = IMPORT-CSV "$($dh)\mail\TEMPLATES\MAIL.CSV"
#customization
$TMP.CC ="" #
$TMP.Subject ="From Server $($( get-wmiobject Win32_ComputerSystem ).Name) By APOMS_TSQL_Deployment_Package $(get-date)." # test1
$TMP.Body ="$msg`nPlease Check The attachement for details. `nThanks`nBest Regards." # test
##clear
if(Test-Path "$logfile.zip" ){
Remove-Item -Path "$logfile.zip" -Force|&$log
}
#create zip
"zipping log.."
export-Zip -sourcefiles "$logfile" -zipfilename "$logfile.zip"|&$log
#add attachements
$TMP.Attachments ="$logfile.zip,$($dh)\Mail\msg.txt" #
#wirte-outbox,output mailxxx.csv
ConvertTo-Mail -path "$($dh)\mail\outbox\MAIL$t.CSV" -mail $tmp
#start sending
"praparing to send"
sleep 10
"sending email..."
."$($dh)\Mail\Send-AvEmail.ps1" |&$log
}
1 {}
} }

send-avmail.ps1

#cls;
#paths
$dh = $pwd.path
$pv = $psversiontable.psversion.major
$strDate = Get-Date -Format "yyyyMMdd"
#log
$log_file = "$($dh)\mail\log\log$($strDate).txt"
$log = {$input|Tee-Object -FilePath $log_file -Append}
$filename = ''
$rem = {$i = '';1..75|%{$i += '-'};$i} ################################START###################################### #remember
$curentFile = ''
$pop = $pwd
$path = "$($dh)\Mail"
CD $path
#body
try {
if(Test-Path -Path $path ){
Import-Module "$path\MailModule.psm1" -force
$t = Get-Date -Format 'yyyyMMdd_hh_mm_ss'
$smtp = Get-Smtp -Path $path
Get-Date|&$log
#MAX 10
foreach($f in $(ls -Path "$path\outbox\*.csv"|sort-object -Property LastWriteTime -Descending|Select-Object -First 10)){
&$rem|&$log
$curentFile = "$($f.fullname)"
$curentFile|&$log
#ONE CSV FILE PRESENT ON MAIL MESSAGE.
$email = ConvertFrom-MailCsv -Path $curentFile
"$($email.subject) is sending.."
#SEND IT
$smtp.send($email)
"$($email.subject) is sent"
#MOVE IT TO FOLDER SENT WHEN SUCCESS
Move-Item -Path $curentFile -Destination "$path\sent\$($f.name)"
#Tabke a break
sleep 1
}
}
}
catch {
#log error message
$_|&$log
"error occur when sending mail:$curentFile"|&$log
#MOVE IT TO FOLDER error WHEN fail
Move-Item -Path $curentFile -Destination "$path\error\$($f.name)"
}
finally {
#mark with date
Get-Date|&$log
#pop
cd $pop
}

mailmodule.psm1

function import-Zip
{
param([string]$zipfilename, [string] $destination) if(test-path($zipfilename))
{
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
$destinationFolder = $shellApplication.NameSpace($destination)
$destinationFolder.CopyHere($zipPackage.Items())
}
} function export-Zip
{
param([string]$sourcefiles, [string]$zipfilename) dir $sourcefiles | foreach-object {
if(!$zipfilename) {
$zipfile = $_.FullName + ".zip";
}
else {
$zipfile = $zipfilename;
} if(!(test-path($zipfile))) {
set-content $zipfile ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18));
( dir $zipfile).IsReadOnly = $false;
} $shellApplication = new-object -com shell.application;
$zipPackage = $shellApplication.NameSpace($zipfile);
$zipPackage.CopyHere(($_.FullName));
}
} function ConvertFrom-MailCsv{
[CmdletBinding()]
param(
[System.String]$Path
) $Path = "$Path"
if(Test-Path -Path $Path){
$tmp = Import-Csv -Path $Path
$mail = New-Object System.Net.Mail.MailMessage #set the from addresses
$MailAddress = $tmp.From
$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress) #set the destination addresses
$MailtoAddress =$tmp.To
$mail.To.Add($MailtoAddress) #set the CC addresses
foreach($cca in $tmp.CC -split ','){
if($cca -ne "" -and $cca -ne $null){
$copy =New-Object System.Net.Mail.MailAddress("$cca");
$mail.CC.Add($copy);
}
}
#set the content
$mail.Subject = $tmp.Subject $mail.Priority = $tmp.Priority
$mail.Body = $tmp.Body #set the attachments
foreach($filename in $tmp.attachments -split ','){
if(Test-Path -Path $filename){
$attachment = new-Object System.Net.Mail.Attachment($filename)
$mail.Attachments.Add($attachment)
}
}
#add to array
return $mail; }
}
function ConvertFrom-Mail{
[CmdletBinding()]
param(
[System.String]$Path
)
begin {
$pop = $pwd
cd $Path
}
process {
[System.Net.Mail.MailMessage[]]$mails = @() foreach($f in $(ls -Path $Path -Name "*.csv")){
$tmp = Import-Csv -Path $f
$mail = New-Object System.Net.Mail.MailMessage #set the from addresses
$MailAddress = $tmp.From
$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress) #set the destination addresses
$MailtoAddress =$tmp.To
$mail.To.Add($MailtoAddress) #set the CC addresses
foreach($cca in $tmp.CC -split ','){
if($cca -ne "" -and $cca -ne $null){
$copy =New-Object System.Net.Mail.MailAddress("$cca");
$mail.CC.Add($copy);
}
}
#set the content
$mail.Subject = $tmp.Subject $mail.Priority = $tmp.Priority
$mail.Body = $tmp.Body #set the attachments
foreach($filename in $tmp.attachments -split ','){
if(Test-Path -Path $filename){
$attachment = new-Object System.Net.Mail.Attachment($filename)
$mail.Attachments.Add($attachment)
}
}
#add to array
$mails += $mail;
} return $mails;
}
end {
cd $pop
} }
function Get-Smtp{
param(
[System.String]$Path
)
begin {
$pop = $pwd
cd $Path
}
process {
$param = Import-Csv ".\smtp.csv"
$smtpServer = $param.Server
$smtpUser = $param.User
$smtpPassword = $($param.Password|ConvertTo-SecureString)
$smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer
#$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,"Isoft1410"#$smtpPassword
$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser, $smtpPassword
return $smtp;
}
end {
cd $pop
} } function ConvertTo-Mail{
[CmdletBinding()]
param(
$mail,
[System.String]$Path
)
$mail|Export-Csv -Path $Path
} Export-ModuleMember -Function ConvertFrom-Mail, ConvertTo-Mail,Get-Smtp, ConvertFrom-MailCsv,import-Zip,export-Zip

download:

http://download.csdn.net/detail/wangzhpwang/8268477

http://download.csdn.net/detail/wangzhpwang/8268511

Powershell Mail module, 发送outbox 里的全部邮件(一个.csv文件代表一封邮件)的更多相关文章

  1. 从csv文件里取数据作为请求参数,和把返回数据放到一个csv文件

    本来想把登陆后的token放到数组里,下一个参数用,但是貌似不支持数组,暂时先这样用了,并不麻烦,还很方便. 1.添加线程组等必要的东东后,添加csv配置器 2.进行设置 说明:csv文件设置不能读取 ...

  2. 读写文件:每次读入大文件里的一行、读写.CSV文件

    读文件: 传统的读法.所有读出,按行处理: fp=open("./ps.txt", "r"); alllines=fp.readlines(); fp.clos ...

  3. .Net Mail SMTP 发送网络邮件

    刚刚迈入"开发"的行列 一直有一个想法 我什么时候能给我庞大的用户信息数据库给每一位用户邮箱发送推荐信息呢? 刚迈入"编程两个月的时间" 我采用 SMTP 发送 ...

  4. Spring邮件服务:Maven + Spring SMTP Mail (可发送附件)

    1  spring-smtp-mail.xml <?xml version="1.0" encoding="UTF-8"?> <beans x ...

  5. IMAP(Internet Mail Access Protocol,Internet邮件访问协议)以前称作交互邮件访问协议(Interactive Mail Access Protocol)。

    IMAP(Internet Mail Access Protocol,Internet邮件访问协议)以前称作交互邮件访问协议(Interactive Mail Access Protocol).IMA ...

  6. 关于 PHPMailer 邮件发送类的使用心得(含多文件上传)

    This is important for send mail PHPMailer 核心文件 class.phpmailer.php class.phpmaileroauth.php class.ph ...

  7. SpringBoot项目实现文件上传和邮件发送

    前言 本篇文章主要介绍的是SpringBoot项目实现文件上传和邮件发送的功能. SpringBoot 文件上传 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 开发准备 环境要 ...

  8. Azure PowerShell (7) 使用CSV文件批量设置Virtual Machine Endpoint

    <Windows Azure Platform 系列文章目录> 请注意: - Azure不支持增加Endpoint Range - 最多可以增加Endpoint数量为150 http:// ...

  9. 如何在一个div标签里显示出另一个网页? <iframe src=" http://www.baidu.com " width="800px" height="200px" scrolling="no" frameborder="0"> </iframe>

    如何在一个div标签里显示出另一个网页? 用在div里用iframe,就像下面的代码 <iframe src=" http://www.baidu.com " width=& ...

随机推荐

  1. 符号文件(.pdb)——Windows 应用程序调试必备

    最近在做项目需求过程中,时不时会遇到崩溃,总是异常中断,于是学习了windbg进行调试的一些基础,windbg在接下来文章进行更新,先介绍在windbg调试中一个重要文件(符号文件) 一.符号文件定义 ...

  2. 关于WEB三层架构的思考

    1.MVC设计思想 MVC程序设计思想是眼下比較流行的WEB开发的模式,当中,M(model)是模型.即JavaBean,用来封装和保存数据:V(view)是视图,即JSP.用来显示内容:C(cont ...

  3. Python全栈开发

    Python全栈开发 一文让你彻底明白Python装饰器原理,从此面试工作再也不怕了. 一.装饰器 装饰器可以使函数执行前和执行后分别执行其他的附加功能,这种在代码运行期间动态增加功能的方式,称之为“ ...

  4. window.name 跨域

    跨域的由来 JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.但是我们常常会遇到无法避免跨域的情况,如普通文章站点(article.xxx.com)需要评论,而评论站点却在chea ...

  5. QLockFile,QRunInfo

    http://doc.qt.io/qt-5/qlockfile.html http://www.dushibaiyu.com/2014/10/qruninfo-api-smple.html

  6. Flex上传文件

    前几天写了一篇jsp页面利用ajaxFileUpload上传文件.如今把flex上传页面也分享出来: 前台页面 <?xml version="1.0" encoding=&q ...

  7. WordPress的用户系统总结

    原文发表自我的个人主页,欢迎大家訪问~转载请保留本段,或注明原文链接:http://www.hainter.com/wordpress-user-module keyword:WordPress,用户 ...

  8. 尺度空间(Scale space)理论

    尺度空间方法的基本思想是:在视觉信息处理模型中引入一个被视为尺度的參数,通过连续变化尺度參数获得不同尺度下的视觉处理信息,然后综合这些信息以深入地挖掘图像的本质特征.尺度空间方法将传统的单尺度视觉信息 ...

  9. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  10. [Ext JS 4]性能优化

    一般的优化技巧 1. 检查你定义的时间监听器 正确的设置事件监听器对性能会有很大的影响. 举例来说, 在定义一个store的时候,设置一个load 的事件去触发从后台读取数据,如果设置single 的 ...