使用PowerShell通过Smtp发送邮件
2010-06-09 18:01
651 查看
借助.NET框架,使PowerShell具有强大的自动化功能,PowrShell可通过Smtp发送邮件,以下是代码:
$comments = @'
author:fuhj(powershell@live.cn ,http://txj.shell.tor.hu)
example:
send-mail -toAddress user@domain.com -subject "Powershell Testing Mail " -body "This is a test mail form powershell" -file "C:\powershellmailfile.txt"
-toName -body and -file are all optional.
use double quotes for the name parameters ie; -body "Proper Content"
'@
function send-mail{
param(
[string]$toAddress = $(throw "toAddress must be set")
,[string]$Subject = $(throw "subject must be set")
,[string]$body = ""
,[string]$file = "")
#mail server configuration
$smtpServer = "smtp.live.com"
$smtpUser = "powershell@live.cn"
$smtpPassword = "P@ssWord"
$sslNeed =$true #SMTP server needs SSL should set this attribute
$MailAddress ="powershell@live.cn"
$fromName = "fuhj"
$replyTo = "powershell@live.cn"
#create the mail message
$mail = New-Object System.Net.Mail.MailMessage
#set the addresses
$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress,$fromName)
$mail.To.Add($toAddress)
#set the content
$mail.Subject = $Subject
$mail.Priority = "High"
$mail.Body = $Body
$filename= $file
$attachment = new-Object System.Net.Mail.Attachment($filename)
$mail.Attachments.Add($attachment)
#send the message
$smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer
$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword
$smtp.EnableSsl = $sslNeed;
try{
$smtp.Send($mail)
echo 'Ok,Send succed!'
}
catch
{
echo 'Error!Filed!'
}
}
执行的效果如下图所示:
收到的邮件如下图所示:
作者: 付海军
出处:http://fuhj02.blog.51cto.com
版权:本文版权归作者和51cto共有
转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
个人网站: http://txj.shell.tor.hu
$comments = @'
author:fuhj(powershell@live.cn ,http://txj.shell.tor.hu)
example:
send-mail -toAddress user@domain.com -subject "Powershell Testing Mail " -body "This is a test mail form powershell" -file "C:\powershellmailfile.txt"
-toName -body and -file are all optional.
use double quotes for the name parameters ie; -body "Proper Content"
'@
function send-mail{
param(
[string]$toAddress = $(throw "toAddress must be set")
,[string]$Subject = $(throw "subject must be set")
,[string]$body = ""
,[string]$file = "")
#mail server configuration
$smtpServer = "smtp.live.com"
$smtpUser = "powershell@live.cn"
$smtpPassword = "P@ssWord"
$sslNeed =$true #SMTP server needs SSL should set this attribute
$MailAddress ="powershell@live.cn"
$fromName = "fuhj"
$replyTo = "powershell@live.cn"
#create the mail message
$mail = New-Object System.Net.Mail.MailMessage
#set the addresses
$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress,$fromName)
$mail.To.Add($toAddress)
#set the content
$mail.Subject = $Subject
$mail.Priority = "High"
$mail.Body = $Body
$filename= $file
$attachment = new-Object System.Net.Mail.Attachment($filename)
$mail.Attachments.Add($attachment)
#send the message
$smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer
$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword
$smtp.EnableSsl = $sslNeed;
try{
$smtp.Send($mail)
echo 'Ok,Send succed!'
}
catch
{
echo 'Error!Filed!'
}
}
执行的效果如下图所示:
收到的邮件如下图所示:
作者: 付海军
出处:http://fuhj02.blog.51cto.com
版权:本文版权归作者和51cto共有
转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任
个人网站: http://txj.shell.tor.hu
相关文章推荐
- 使用PowerShell通过Smtp发送邮件
- 使用PowerShell通过Smtp发送邮件
- CentOS6.5通过mailx使用外部SMTPSSL的方式发送邮件
- 在php中使用SMTP通过密抄批量发送邮件
- python - 如何通过GMail发送邮件:smtplib的使用
- python使用smtplib模块通过gmail实现邮件发送的方法
- 通过java socket编程使用smtp简单邮件协议发送邮件
- 使用System.Web.Mail 组件通过SMTP认证发送邮件
- 使用 Mailx 通过 SMTP 在 Centos 上发送邮件,解决 WordPress 发送邮件问题,解决centos7不能发送邮件的问题
- powershell 通过SMTP发送邮件
- 使用MailMessage和SmtpClient类实现asp.net的邮件发送(有详细注释并已测试通过)
- python使用smtplib模块通过gmail实现邮件发送的方法
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
- 使用PHP通过SMTP发送邮件新手指南zz
- 使用java通过smtp完成发送邮件源码
- 使用PHP通过SMTP发送邮件新手指南
- python - 如何通过GMail发送邮件:smtplib的使用
- 在php中使用SMTP通过密抄批量发送邮件续:修正
- 使用java通过smtp完成发送邮件源码
- 使用Pear和php socket来实现通过smtp验证发送邮件