[英]Send outlook email from powershell script
當我將以下腳本一個一個地寫入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()
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 ;
}
}
命令行和腳本文件之間的語法不變。 變化的是命令的執行速度。 如果要輸入它們,則每個命令之間會有大量延遲。 但是,如果它們從腳本運行,則可以更快地呈現給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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.