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 ...
随机推荐
- django2.0url的变动
WARNINGS:?: (urls.W001) Your URL pattern '^$' uses include with a route ending with a '$'. Remove th ...
- dsLinq.Count() 引发了“System.NullReferenceException”类型的异常
DataTable dt = PurchaseArriveInfoBus.GetPurchaseArriveInfo(companyCD, txtArriveNo, txtTitle, txtProv ...
- python中的面向对象和面向过程
一.面向对象和面向过程 一.什么是面向过程 核心是过程二字:面向过程是一种流水线的工作流程,是先做什么在做什么 二.什么是面向对象 核心 是对象二字:是一种编程思想,上帝思维,一切皆对象,编程中负责调 ...
- Nginx 编译安装工程优化
1.减小 nginx 编译后的文件大小 在编译 nginx 时,默认以 debug 模式进行,在 debug 模式下会插入很多跟踪和 assert 之类的信息. 在 nginx 源码文件解压后,找到源 ...
- 116-基于5VLX110T FPGA FMC接口功能验证6U CPCI平台 光纤PCIe卡
基于5VLX110T FPGA FMC接口功能验证6U CPCI平台 一.板卡概述 本板卡是Xilinx公司芯片V5系列芯片设计信号处理板卡.由一片Xilinx公司的XC5VLX110T-1FF113 ...
- PAT Basic 1014 福尔摩斯的约会 (20 分) Advanced 1061 Dating (20 分)
大侦探福尔摩斯接到一张奇怪的字条:我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm.大侦探很快就明白了,字条上奇 ...
- moc_XXXX.o:(.data.rel.ro._ZTI12CalculatorUI[_ZTI12CalculatorUI]+0x10): undefined reference to `typeinfo for QWidget' collect2: error: ld returned 1 exit status make: *** [Makefile:144: myCalculator]
main.cpp:(.text.startup+0x22): undefined reference to `QApplication::QApplication(int&, char**, ...
- php内置函数分析之array_change_key_case()
PHP_FUNCTION(array_change_key_case) { zval *array, *entry; zend_string *string_key; zend_string *new ...
- PHP简单的爬虫–原型
1.PHP简单的爬虫–原型 爬虫的原理: 给定原始的url: 分析链接,根据设置的正则表达式获取链接中的内容: 有的会更新原始的url再进行分析链接,获取特定内容,周而复始. 将获取的内容保存在数据库 ...
- Array 和 ArrayList 、 List 以及 LinkedList 的区别
下面列出了Array(数组)和ArrayList(集合)的不同点: Array可以包含基本类型和对象类型,ArrayList只能包含对象类型. Array大小是固定的,ArrayList的大小是动态变 ...