功能比较简单,直接贴代码, 自动给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
复制代码
|