目录

前言

最近领导想在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 邮件发送的更多相关文章

  1. Chilkat----开源站点之VS2010 CKMailMan一个很好的邮件发送开源开发包

    Chilkat 是一个很好的开源站点,有各种开源库. 开发语言主要有Classic ASP •C • C++ • C# • Delphi ActiveX • Delphi DLL • Visual F ...

  2. .NET开发邮件发送功能的全面教程(含邮件组件源码)

    今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1)         邮件基础理论知识 2)         ...

  3. J2EE 邮件发送那些事儿

    距离自己写的关于java邮件发送的第一篇博客已经有很长一段时间了,现在回过头看看.虽然代码质量方面有待提高,整体结构也不怎样,但是基本思路和过程还是比较纯的.现在有空写写J2EE中邮件发送的开发,实际 ...

  4. 结合ABP源码实现邮件发送功能

    1. 前言 2. 实现过程 1. 代码图(重) 2.具体实现 2.1 定义AppSettingNames及AppSettingProvider 2.2 EmailSenderConfiguration ...

  5. SSH项目里面 忘记密码的邮件发送功能

    package com.xxx.util; import java.util.Date; import java.util.Properties; import javax.mail.Address; ...

  6. [UWP]UWP中获取联系人/邮件发送/SMS消息发送操作

    这篇博客将介绍如何在UWP程序中获取联系人/邮件发送/SMS发送的基础操作. 1. 获取联系人 UWP中联系人获取需要引入Windows.ApplicationModel.Contacts名称空间. ...

  7. java spring 邮件发送

    开发中经常会遇到发送邮件进行用户验证,或者其它推送信息的情况,本文基于spring,完成邮件的发送,主要支持普通文本邮件的发送,html文本邮件的发送,带附件的邮件发送,没有实现群发.多个附件发送等需 ...

  8. Java邮件发送与接收原理

    一. 邮件开发涉及到的一些基本概念 1.1.邮件服务器和电子邮箱 要在Internet上提供电子邮件功能,必须有专门的电子邮件服务器.例如现在Internet很多提供邮件服务的厂商:sina.sohu ...

  9. c#实现邮件发送链接激活

    2016-08-24 10:09:52 public void MailSend(string email) { MailMessage MyMail = new MailMessage(); MyM ...

随机推荐

  1. unsolved question's solution

    因为很懒,没有时间,只会口胡等等原因,所以有些题目就不打code了 $luogu:$ P1973 [NOI2011]Noi嘉年华: 时间离散化,预处理一个区间$[l,r]$内的最多活动个数$in[l] ...

  2. npm发布包

    一.发布一个新包第一步:进入要发布的项目根目录,初始化为npm包: npm init 依次按提示填入包名.版本.描述.github地址.关键字.license等 这步完成之后会生成一个package. ...

  3. 面试经典&&竞赛——二叉树

    To record her trees for future generations, she wrote down two strings for each tree: a preorder tra ...

  4. element-ui 表格标题换行

     render-header: 列标题 Label 区域渲染使用的 Function <template> <el-table :data="dataList"& ...

  5. Codeforces 矩阵题 题单

    Matrix CF 166E Tetrahedron dp方程设为 f[i] 最后在 D点,g[i] 表示最后不在D点.最后 g[] 可以通过矩阵加速数列求得,数据可以强化,复杂度 \(O(logn) ...

  6. java ThreadGroup源码分析

    使用: import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction; public class Test { public s ...

  7. 微信小程序 背景音频播放遇到的深坑

    1.微信前台(聊天页)暂停后回到小程序,再点击播放,播放信息消失,无法续播 ios可以监听到 (onStop已经停止)事件, 安卓无法监听到,只能监听到普通的暂停事件. 2.

  8. [每日一讲] Python系列:字符串(上)

    字符串作为人类最常处理的内容,在计算中决定了其占有重要的地位.在 Python 中,字符串的操作和处理往往需要根据实际问题,结合其他操作才可以完成目标.在复杂世界仅仅是字符串 API 还无法完成工作. ...

  9. 对Nuxt的研究

    Nuxt就是基于Vue的一个应用框架,采用服务端渲染,让你的SPA应用(Vue)也可以拥有SEO Nuxt的生命周期有些在服务端(Node),客户端,甚至两边都在: 1.其他之前都不存在Window对 ...

  10. React 之React.createContext

    使用Context,可以跨越组件进行数据传递 import React from 'react'; import ReactDOM from 'react-dom'; const ThemeConte ...