参考文章:https://www.cnblogs.com/lsgxeva/p/9309217.html

文章内容:

问:如何用powershell发邮件?
答:
Send-MailMessage   -Subject "主题"       `
-From  "你的hotmail账户@hotmail.com"   -To  "你的qq邮箱@qq.com"   `
-SmtpServer   "smtp.live.com"  -Port 587  -UseSsl    -Credential  "你的hotmail账户@hotmail.com"  `
-Attachments $附件 -BodyAsHTML -body $HTML邮件内容

注意:
1 用xxx@QQ.com + 你的qq密码 + 上述命令 发邮件是不行的。因为为了qq密码安全,腾讯要求独立的邮箱密码。  
2 从qq邮箱发件,默认也不行。被关了,需要在设置中开启smtp。
3 powershell 3.0 和 以上 才支持port参数。对于win7,你需要先安装ps最新版

我的代码:

# 简单版Send-MailMessage   -Subject "powershell send Email"       `
-From  "taozXX@hotmail.com"   -To  "3845XX@qq.com"   `
-SmtpServer   "smtp.live.com"  -Port 587  -UseSsl    -Credential  "taozXX@hotmail.com"  `
-BodyAsHTML -body "powershell send Email from taozXX@hotmail.com to 3845XX@qq.com"
#弹出密码框,填写hotmail密码#-SmtpServer   "smtp.office365.com"  -Port 587  -UseSsl    -Credential  "taozXXX@hotmail.com" `

# 函数版,并且加了安全机制# Write-Output 不要随便用,会有管道传递,Write-Host功能少,只是简单打印
function SendEmail {param ($SmtpServer = 'smtp.office365.com', # This is the address of the Office 365 SMTP server we will be using.$SmtpPort = '587', #This is the Port of the Office 365 SMTP server we will be using.$SmtpUser = 'taoXXX@hotmail.com', # Specify your Office 365 User ID#$SmtpPassword = '', # Specify your Office 365 password, but not secure, more information belower $SmtpSecureFile = 'secure.file', # Specify your password secure.file, more secure$MailFrom = 'taoXXX@hotmail.com', # Specify the Source Email ID[Parameter(Mandatory=$true)]$MailTo, # Specify the users to which the mails should be sent. $MailTo = 't-shtao@microsoft.com'[Parameter(Mandatory=$true)]$MailSubject, # This is the Mail Subject line. $MailSubject = "powershell send Email by t-shtao"$MailBody, # This is the Mail body. $MailBody = "Hello <span style='font-weight:bold;'>Microsoft</span>.",$MailAttachments # This is the Mail attachments' file path. $MailAttachments = "C:\PBID-qs\attachments\attachment01.txt"
    )# get Email secure password by [SecureString][String]$SmtpPassword = SecurefileToString -SecureFile $SmtpSecureFile# make credentials by Email secure password$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser, $($smtpPassword | ConvertTo-SecureString -AsPlainText -Force) # MailParameters setting$MailParameters = @{Subject=$MailSubjectFrom=$MailFromTo=$MailToSmtpServer=$SmtpServerPort=$SmtpPortUseSsl=$trueCredential=$CredentialsBodyAsHtml=$true}# add html bodyif ($MailBody) { $MailParameters.Add('Body',$MailBody) }# add attachmentsif ($MailAttachments) { $MailParameters.Add('Attachments',$MailAttachments) }# send EmailSend-MailMessage @MailParameters# send successfully. Write some informationWrite-Host "Send Successfully. SmtpServer:$SmtpServer, From:$MailFrom, To:$MailTo"
}function StringToSecurefile {# [string]password convert to secure.fileparam ($OrdinaryString, # the string need secure$SecureFile = 'secure.file' # storeage of secure string
    )# string convert to file$OrdinaryString | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File -FilePath $SecureFileWrite-Host "password has converted to $SecureFile"
}function SecurefileToString {# secure.file convert to [string]passwordparam ($SecureFile = 'secure.file' # storeage of secure string
    )# file convert to secure string$secureString = Get-Content -Path $SecureFile | ConvertTo-SecureString$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUnicode($secureString)$OrdinaryString = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptr) Write-Host "password has read"return $OrdinaryString
}# make password's secure.file by visible password string. run only first
# StringToSecurefile -OrdinaryString "password" -SecureFile "secure.file"# send Email, [Mandatoty]MailTo, [Mandatory]MailSubject, MailBody, MailAttachments
SendEmail -MailTo "tXXX@microsoft.com" -MailSubject "powershell send Email" `-MailBody "Hello <span style='font-weight:bold;'>Microsoft</span>." `-MailAttachments "C:\PBID-qs\attachments\attachment01.txt"

 

转载于:https://www.cnblogs.com/taoshiqian/p/10138014.html

powershell 发邮件相关推荐

  1. 公司内部exchange2010 下删除误发邮件

    1.Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 2.get-mailbox | search-mailbox -Search ...

  2. 利用exchangelib快速上手使用python发邮件

    前言 之前一直知道使用python smtp发邮件,但是最近发现一个神器exchangelib,用这个神器发邮件,简直简单的不能再简单了,官方文档在这里 实例 只需要将下面的xxx改成你的信息就可以了 ...

  3. 自动发邮件的程序 c语言,5分钟!教你用C语言发送邮件:附送源码+教学!

    5分钟!教你用C语言发送邮件:附送源码+教学!-1.jpg (10.71 KB, 下载次数: 0) 2018-9-3 02:21 上传 关注<一碳科技>,获取更多知识! 前言 相信年夜家都 ...

  4. 创建oracle发邮件job导致的故障

    前几天创建了个发邮件的存储过程,想把数据库每天的运行情况自动发到邮箱,没想到第二天就出了问题,在dbms/trace目录下产生了大量的xxx_j000_xxx.trc文件,一分钟产生2个.alter日 ...

  5. MDaemon 10.1.2 通过Webclient发邮件时,提示“发邮件时发生错误

    MDaemon 10.1.2 通过Webclient发邮件时,提示"发邮件时发生错误",经查发现服务里边有个Simple mail transfer protocol 服务被启用, ...

  6. 猫猫学iOS(四十五)之常用的小功能比如打电话、打开网址、发邮件、发短信打开其他应用。...

    猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243?viewmode=contents 简介 iOS中的很多小功能都 ...

  7. vba给服务器发送消息,使用VBA实现发邮件功能

    财务MM经常要给员工发送每月的工资信息,一个个发送实在是太忙了.本文将介绍使用VBA实现工资信息的自动发送.有了这个功能,财务MM只需要把基本数据准备好,然后按下按钮只要选择需要发送的对象.就可以快速 ...

  8. 【知识积累】JavaMail实现发邮件功能

    一.前言 今天闲来没事,想着通过程序来给别人发邮件.于是,上网搜了一下,相应的资料也很多,刚开始完成了邮件的简单发送,后来想如何能发送附件,继续寻找 答案,但是遇到了一个问题是当我使用txt类型作为附 ...

  9. python自动化发送邮件_python接口自动化(三十三)-python自动发邮件总结及实例说明番外篇——下(详解)...

    简介 发邮件前我们需要了解的是邮件是怎么一个形式去发送到对方手上的,通俗点来说就是你写好一封信,然后装进信封,写上地址,贴上邮票,然后就近找个邮局,把信仍进去,其他的就不关心了,只是关心时间,而电子邮 ...

最新文章

  1. Python设计模式-命令模式
  2. 初步学习pg_control文件之八
  3. Intel 4004,世界上首个CPU,进不惑之年
  4. maven 内存不足_如何修复Maven内存不足问题
  5. Pandas系列(十四)数据转换函数map、apply、applymap以及分组apply
  6. HDU - 2602 01背包
  7. 汽车理论课后习题matlab程序,汽车理论课后题matlab程序
  8. DaleCloud(原NFine)介绍二:系统菜单及菜单按钮权限控制
  9. gooflow的流程设计
  10. 学习OpenCV3:判断两条直线相交,并计算交点和夹角
  11. Qt之QTreeWidget增删节点
  12. 我自己常用的几个JS深浅拷贝的方法
  13. 台式电脑计算机里的共享网络,如何实现笔记本和台式机两台电脑共享上网
  14. hyperledger-fabric第一天 Fabric环境搭建
  15. 杰奇运行在php7,帝国CMS7.5使用PHP7.x环境登录后台报错的解决方法!
  16. 麒麟V10SP1的国产化Qt适配
  17. 哪位知道基带怎么修复?
  18. B2B2C分销商城系统,带进货模块,二级分销商城Java系统源码
  19. 使用网络唤醒功能实现远程开机 —— 定时执行专家
  20. 漫步线性代数八——向量空间和子空间

热门文章

  1. 哈弗f7带自动泊车吗_中国品牌的全球车型再升级——试驾2020款哈弗F7
  2. SAE学习-使用SAE的Storage服务存储图片
  3. android 加载json停顿,java – 在Android上解析~1 MB JSON非常慢
  4. diy 单片机 自动浇花_造个自动浇花装置(完)
  5. 各种机器学习任务的顶级结果(论文)汇总
  6. python3.9新特性_Python 3.9正式版,新特性提前一睹为快
  7. 如何将树莓派设置为WiFi热点
  8. 插入始终是1_插入式电磁流量计的安装说明
  9. JavaWeb——springMVC请求数据绑定分类解析(深度好文)
  10. Xna游戏编辑器开发(WinForm内嵌Xna)