标题: [原创教程] PowerShell自动给xxx@163.com发邮件 [打印本页]
作者: gflrlm 时间: 2018-4-7 19:49 标题: PowerShell自动给xxx@163.com发邮件
功能比较简单,直接贴代码, 自动给xxx@163.com发邮件,前提是要设置好密码。 发邮件的时候,可以选择添加附件发送, 代码如下:
脚本里面的发件人也是xxx@163.com,自己给自己发。 可以修改成另外一个邮箱。
$file= "C:\000___backup.zip"
$att = new-object Net.Mail.Attachment($file)
$SMTPMessage.Attachments.Add($att) - $self_filename=$MyInvocation.MyCommand.Name
- $host.ui.RawUI.WindowTitle="$self_filename"
- #Start-Sleep -Seconds 5
- $From = "xxx@163.com"
- $To = "xxx@163.com"
- $Subject = "000___backup.zip"
- $Body = "000___backup.zip"
- $smtpServer = "smtp.163.com"
- $smtpPort = 25
- $username = "xxx@163.com"
- $password = "YOUR_PASSWORD" # xxx邮件的密码
- write-host "---------------------------------------------------------"
- write-host "---------------------------------------------------------"
-
- $SMTPMessage = New-Object System.Net.Mail.MailMessage($From, $To, $Subject, $Body)
- $SMTPClient = New-Object Net.Mail.SmtpClient($smtpServer, $SmtpPort)
- $file= "C:\000___backup.zip"
- $att = new-object Net.Mail.Attachment($file)
- $SMTPMessage.Attachments.Add($att)
- $SMTPClient.EnableSsl = $false
- $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($username, $password);
- write-host "From = $From "
- write-host "To = $To "
- write-host "Subject = $Subject "
- write-host "Body = $Body "
- write-host "附件 = $file "
- write-host "---------------------------------------------------------"
- write-host "---Start send mail ..."
- write-host "---------------------------------------------------------"
-
- $SMTPClient.Send($SMTPMessage)
- # cmd /c "pause"
- # exit
复制代码
作者: yhcfsr 时间: 2018-4-8 19:36
MARK,以后肯定用得上。
作者: amwfjhh 时间: 2019-3-18 10:05
非常不错。
作者: sofood 时间: 2019-10-25 17:07
感谢分享成果
作者: lancer 时间: 2023-8-16 10:41
运行之后没有任何反应啊
作者: jyswjjgdwtdtj 时间: 2023-8-16 14:19
回复 5# lancer
或许stmp服务器要随邮件地址改变而改变
作者: lancer 时间: 2023-8-16 15:07
回复 6# jyswjjgdwtdtj
改了,用的QQ邮箱,所以用的smtp.qq.com
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |