【问题标题】:Send outlook email from powershell script从 powershell 脚本发送 Outlook 电子邮件
【发布时间】:2013-07-11 23:31:16
【问题描述】:

当我将以下脚本一个一个写入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()

【问题讨论】:

    标签: powershell automation


    【解决方案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 ; 
    }
    }
    

    【讨论】:

    • 我写了这个草率的脚本...用于使用 powershell 发送电子邮件...随心所欲地编辑。希望这会有所帮助!
    【解决方案2】:

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

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

    如果添加 Sleeps 可以解决问题 - 换句话说,问题是同步问题,您可以使用的 Outlook 对象模型中的某些内容可能不会像使用 Sleeps 那样糟糕。


    我无法在我的 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()
    

    【讨论】:

    • 好建议,但我最终在每个命令下都添加了 Sleep 10,但仍然没有运气。还有其他想法吗?
    • 请发布您收到的错误消息,尤其是前几条,但一切都会更好。
    猜你喜欢
    • 2010-09-25
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 2018-07-24
    • 2011-09-16
    • 2011-04-28
    • 1970-01-01
    相关资源
    最近更新 更多