从powershell脚本发送outlook电子邮件

时间:2013-07-11 23:31:17

标签: powershell automation

当我将以下脚本逐一写入powershell命令行时,它会成功发送电子邮件,但是当我运行脚本时,它会返回一堆错误。我猜测某些语法需要改变才能将其作为脚本运行?有什么想法吗?

Start-Process Outlook

$o = New-Object -com Outlook.Application


$mail = $o.CreateItem(0)

#2 = high importance email header
$mail.importance = 2

$mail.subject = “Auto Build Test“

$mail.body = “This is a test“

#for multiple email, use semi-colon ; to separate
$mail.To = “myemail@company.com"
$mail.Send()

# $o.Quit()

2 个答案:

答案 0 :(得分:1)

Param(
[parameter(Mandatory=$true)]
[alias("e")]
[string]$RecipientEmailAddress
)

if($RecipientEmailAddress -notmatch  "\b[A-Za-z0-9._%+-]+@BLAHH.com")
{
    Write-Output "the email address for the receipient of log reports is not a valid       email address, hence will not send the report via email. They can still be accessed at "   |Out-String ;
}else
{
    $returnVal= New-Object PSObject ;
    $returnVal |Add-Member -Name is_Success -MemberType NoteProperty -Value $null;
    $returnVal |Add-Member -Name Explanation -MemberType NoteProperty -Value $null;
    try{
            $Attachments =Get-ChildItem -Path "C:\FOLDERWHEREYOURAATACHMENTS ARESTORED";
            if($Attachments.count -eq 0)
            {
                 $returnVal.Explanation="Error sending log report email to the user: $RecipientEmailAddress. Please check if the C:\FOLDERWHEREYOURAATACHMENTS is accessible and there are indeed log files present";
            #Write-Output "Error sending log report email to the user: $RecipientEmailAddress" |Out-String ;
            #Write-Output "Please check if the C:\FOLDERWHEREYOURAATACHMENTS is accessible and there are indeed log files present "|Out-String;
            $returnVal.is_Success= $false;
            return $returnVal;
        }
        $TestedAttachmentsList = new-Object System.Collections.ArrayList;
        for($i=0;$i -lt $Attachments.count;$i++)
        {
           $TestedAttachmentsList.add($Attachments[$i].FullName);
        }
        Send-MailMessage -From "<FROM@BLAHH.COM>" -To "<$RecipientEmailAddress>" -SmtpServer "mail.BLAHH.com" -Attachments $TestedAttachmentsList -Subject "BLAHH SUBJECT" -Body "BLAHH BLAHH";
        $returnVal.is_Success=$true;  
        $returnVal.Explanation="An email has been sent to the $RecipientEmailAddress containing the log of the setup and configuration." 
        return $returnVal ; 
}Catch [System.Exception]
   {
        #Write-Output "Error sending log report email to the user: $RecipientEmailAddress" |Out-String ;
        #Write-Output "Please check communication between your host machine and mail.BLAHH.com on port 25 is possible"|Out-String;
        $returnVal.is_Success= $false;
        $returnVal.Explanation="Error sending log report email to the user: $RecipientEmailAddress Please check communication between your host machine and mail.BLAHH.com on port 25 is possible";
        return $returnVal ; 
}
}

答案 1 :(得分:0)

语法在命令行和脚本文件之间不会更改。命令执行的速度有多大变化。如果你输入它们,那么每个命令之间有很多延迟。但如果它们从脚本运行,则会更快地呈现给Outlook。

解决此问题的一种简单方法是在失败的命令之前添加Sleep 1(或类似)。如果没有看到你的错误输出,我猜你想在CreateItem之后,也许在Send之前睡觉。但是,如果仔细查看错误消息,您将看到它们确定脚本的哪一行失败。在失败的第一行之前放置Sleep。重试脚本。如果新线路出现故障,那么也要在它之前加上延迟。如果第一行仍然失败,您可以尝试Sleep 2。您还可以缩短睡眠时间。 1/2秒:Sleep -milliseconds 500

如果添加Sleeps可以解决问题 - 换句话说问题就是同步问题,你可以使用的Outlook对象模型中可能会有一些不像使用Sleeps那样的hackish。


我无法在Outlook 2010安装上重新启用此功能。但是我确实查找了另一种从PS发送电子邮件的方法(如下)。也许这种方法可行。

$i=$o.Session.folders.item(2).folders.item("Outbox").items.add(0)
$i.to="me@whereiwork.com"
$i.Subject="a wittle testy"
$i.Body="some body"
$i.send()
相关问题