在成功执行PowerShell脚本后发送邮件

时间:2015-12-09 09:12:31

标签: windows email powershell

所以我创建了以下脚本:

$target = "c:\$(get-date -F 'yyyy-MM')"
if (!(Test-Path $target)) {md $target}
gci 'c:\test\' -Filter *.xml -recurse | ?{!$_.PSIsContainer -and $_.CreationTime -ge (get-date "01.$((get-date).Month)")} | copy-item -Destination $target -Force

在成功执行此脚本后,有人能指出如何向特定地址发送电子邮件的正确方向吗?

3 个答案:

答案 0 :(得分:1)

正如arco444在评论中指出的那样,send-mailmessage将允许您从运行Powershell v2或更高版本的任何计算机发送电子邮件,只要您有可访问的SMTP服务器。

答案 1 :(得分:0)

您可以在PowerShell中尝试以下解决方案

 $EmailFrom = "someone1@company.com"
 $EmailTo = "someone2@company.com" 
 $ccUsers="someone3@company.com"  
 $SMTPServer = "smtp.office365.com"
 $port=587     
 $smtp = New-Object Net.Mail.SmtpClient($SMTPServer,$port) 
 $smtp.EnableSsl = "true"
 $smtp.Credentials =New-Object System.Net.NetworkCredential($userName, $password); 
 $msg = New-Object Net.Mail.MailMessage 
 $msg.From =  $EmailFrom
 $msg.To.Add($EmailTo) 
 $msg.CC.Add($ccUsers)  
 $msg.IsBodyHTML = $true 
 $msg.Subject = "Test Subject";
 #if you want to send some html in your email
 $html = "<html><body><table width='100%'><tr bgcolor='#CCCCCC'><td height='25' align='center'> <font face='tahoma' color='#003399' size='4'><strong>Test Email from PowerShell</strong></font></td> </tr> </table></body></html>" $msg.Body = $html; $smtp.Send($msg);

答案 2 :(得分:-2)

如果你使用outlook,这里是一个例子

[system.reflection.assembly]::loadwithpartialname("Microsoft.office.interop.outlook") 
$OL=New-Object -ComObject OUTLOOK.APPLICATION
$ns =$OL.GETNAMESPACE("MAPI")
$mail =$ol.createitem(0)   
$mail.recipients.add("something@live.nl")
$mail.CC=("something@ziggo.nl")
$MAIL.Subject= "$shortname"
#gets a signature
$mail.htmlbody= get-content $env:appdata\Microsoft\Signatures\*.txt       
#for attachments..
$mail.Attachments.Add($file.FullName)

$lines=get-content $filename
$lines+=get-content $env:appdata\Microsoft\Signatures\*.txt      
clear-variable text
foreach ($line in $lines)
{
$text += $line + "`r`n"
}

$mail.body= $text 
$mail.display()
相关问题