Powershell 邮件发送
目录
前言
最近领导想在winServer2012上搞个自动发送邮件的计划任务,下面有几种发送邮件的方式。
如果Powershell V2.0 以上建议使用第一种方式(比较无脑),2.0以下的话也可以使用第二种方法。
Send-MailMessage
Syntax:
Send-MailMessage -To $to -From $from -cc $cc -Subject $sub -Body $body -Credential $mycreds -SmtpServer $smtp -DeliveryNotificationOption Never -BodyAsHtml -UseSsl -port 587 -Attachments $attach
Example:
#Create the secure passward
Function Set-SecurePwd($storage)
{
$mysecret = 'YOURPASSWORD'
$mysecret |
ConvertTo-SecureString -AsPlainText -Force | #将加密的标准字符串转换为安全字符串。它还可以将纯文本转换为安全字符串。
ConvertFrom-SecureString | #将安全字符串转换为加密的标准字符串。
Out-File -FilePath $storage #将加密的标准字符输出到指定文件
$pw = Get-Content $storage | ConvertTo-SecureString #获取经过加密的标准字符并转换为安全字符串
return $pw
}
Function Send-Email($attach)
{
$pwd = Set-SecurePwd $storage
$cred = New-Object System.Management.Automation.PSCredential "userName",$pwd #创建身份认证对象
$to = "xxx@xxx.com"
$from = "xxx@163.com"
$cc = "xxx@xxx.com"
$sub = "TEST"
$body = "Just test"
$smtp = "smtp.163.com"
Send-MailMessage -To $to -From $from -cc $cc -Subject $sub -Body $body -Credential $cred -SmtpServer $smtp -UseSsl -port 25 -Attachments $attach
if($?)
{
Write-Host "Sent Successfully!" -ForegroundColor Green
}
else
{
Write-Host "Error" -ForegroundColor Red
}
}
#Main
$storage = "E:\pwd\password.txt"
$attach = "E:\attach\test.txt"
Send-Email $attach #指定需要发送的附件
注意:$from
的Address必须能够与$cred
身份认证对象一致,这个例子使用了163邮件的SMTP Server 。
.NET.Mail
还可以使用.NET支持的实例来实现邮件发送,上面的Cmdlet也是调用了这一实例。
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 = "SMTPSERVERADDRESS"
$smtpUser = "smtpUserName"
$smtpPassword = "smtpUserPwd"
$sslNeed =$true
#SMTP server needs SSL should set this attribute
$MailAddress ="xxx@163.com"
$fromName = "mailAccountName"
$replyTo = "xxx@163.com"
#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!'
}
}
Send-Mail "xxx@xxx.com" "TEST" "Just test" "E:\attach\hd.txt"
使用OutLook发送邮件
这是调用了本地的MAPI Client程序,不能自动发送,只是填充了邮件信息,需要手动点击发送。例子来自——Powershell中文博客
$subject = 'Sending via MAPI client'
$body = 'My Message'
$to = 'tobias@powertheshell.com'
$mail = "mailto:$to&subject=$subject&body=$body"
Start-Process -FilePath $mail
:)
Powershell 邮件发送的更多相关文章
- Chilkat----开源站点之VS2010 CKMailMan一个很好的邮件发送开源开发包
Chilkat 是一个很好的开源站点,有各种开源库. 开发语言主要有Classic ASP •C • C++ • C# • Delphi ActiveX • Delphi DLL • Visual F ...
- .NET开发邮件发送功能的全面教程(含邮件组件源码)
今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1) 邮件基础理论知识 2) ...
- J2EE 邮件发送那些事儿
距离自己写的关于java邮件发送的第一篇博客已经有很长一段时间了,现在回过头看看.虽然代码质量方面有待提高,整体结构也不怎样,但是基本思路和过程还是比较纯的.现在有空写写J2EE中邮件发送的开发,实际 ...
- 结合ABP源码实现邮件发送功能
1. 前言 2. 实现过程 1. 代码图(重) 2.具体实现 2.1 定义AppSettingNames及AppSettingProvider 2.2 EmailSenderConfiguration ...
- SSH项目里面 忘记密码的邮件发送功能
package com.xxx.util; import java.util.Date; import java.util.Properties; import javax.mail.Address; ...
- [UWP]UWP中获取联系人/邮件发送/SMS消息发送操作
这篇博客将介绍如何在UWP程序中获取联系人/邮件发送/SMS发送的基础操作. 1. 获取联系人 UWP中联系人获取需要引入Windows.ApplicationModel.Contacts名称空间. ...
- java spring 邮件发送
开发中经常会遇到发送邮件进行用户验证,或者其它推送信息的情况,本文基于spring,完成邮件的发送,主要支持普通文本邮件的发送,html文本邮件的发送,带附件的邮件发送,没有实现群发.多个附件发送等需 ...
- Java邮件发送与接收原理
一. 邮件开发涉及到的一些基本概念 1.1.邮件服务器和电子邮箱 要在Internet上提供电子邮件功能,必须有专门的电子邮件服务器.例如现在Internet很多提供邮件服务的厂商:sina.sohu ...
- c#实现邮件发送链接激活
2016-08-24 10:09:52 public void MailSend(string email) { MailMessage MyMail = new MailMessage(); MyM ...
随机推荐
- vue项目1-pizza点餐系统6-路由精讲之复用router-view
1.在主组件展示二级路由的组件内容,在App.vue中添加 <br> <div class="container"> <!-- row 行排列 --& ...
- editplus的使用技巧
数据库sql语句中的 in 后面需要 ('xx','bb')这样的结果,多的话修改起来就比较麻烦,这时候使用editplus 的替换功能就可以实现 ,顶部菜单的 搜索 - > 替换 或者 ctr ...
- poj 1564 Sum It Up(dfs)
Sum It Up Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7191 Accepted: 3745 Descrip ...
- python打包命令
打包成exe方法 (1)切换到该文件夹 (2)pyinstaller -F py文件 (py文件要英文才行) -F 生成单个可执行文件 -w 去掉控制台窗口 -p 自定义需要加载的类路径 -i 可执行 ...
- 一个有python扩展库的下载网站
https://www.lfd.uci.edu/~gohlke/pythonlibs/
- flask框架上下文
上下文 上下文:相当于一个容器,保存了 Flask 程序运行过程中的一些信息. Flask中有两种上下文,请求上下文和应用上下文 请求上下文(request context) 在 flask 中,可以 ...
- 调整数组顺序使奇数位于偶数前面(python)
题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 方法1:冒泡 O( ...
- Java面试之集合框架篇(3)
21.ArrayList和Vector的区别 这两个类都实现了List接口(List接口继承了Collection接口),他们都是有序集合,即存储在这两个集合中的元素的位置都是有顺序的,相当于一种动态 ...
- JS循环结构
什么是循环结构? 反复一遍又一遍做着相同(相似)的事情 循环结构的两大要素? 循环条件:什么时候开始,什么时候结束 循环操作:循环体,循环过程中 做了什么 一.while语句 while语句 属于前测 ...
- adb打开系统设置的命令
adb命令打开手机设置页面 设置主页面adb shell am start com.android.settings/com.android.settings.Settings 安全adb shell ...