簡體   English   中英

Powershell,將帶有Outlook的電子郵件作為不同的電子郵件發送

[英]Powershell, send email with outlook as different email

我們具有一些Powershell自動化功能,可以通過一個電子郵件帳戶發送帶有Outlook的電子郵件,但是我們正在尋找一種方法,可以將發件人電子郵件地址設置為我們可以訪問的其他Outlook帳戶。

我已經嘗試使用Google搜索,然后在這里四處尋找,似乎找不到解決方法。

這是我們正在使用的代碼。

$Outlook = New-Object -comObject  Outlook.Application 
$Mail = $Outlook.CreateItem(0) 
start-sleep 5
$Mail.subject = ""
$mail.
$Mail.To = ""
$Mail.Cc = ""
$Mail.Body = "Test" 
$Mail.Display()
$Mail.Send()

只需使用以下Outlook功能即可發送電子郵件。 您實際上在那邊做同樣的事情。 您遇到任何錯誤嗎? 無論如何,請使用以下內容:遵循功能中的所有注釋以供參考。

Function Global:Send-Email { 
[cmdletbinding()]
Param (
[Parameter(Mandatory=$False,Position=0)]
[String]$Address = "user2@domain.com",
[Parameter(Mandatory=$False,Position=1)]
[String]$Subject = "Mail Subject",
[Parameter(Mandatory=$False,Position=2)]
[String]$Body = "MailBody"
      )
Begin {
Clear-Host
# Add-Type -assembly "Microsoft.Office.Interop.Outlook"
    }
Process {
# Create an instance Microsoft Outlook
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "$Address"
$Mail.Subject = $Subject
$Mail.Body =$Body
# $Mail.HTMLBody = "HTML BODY"
# $File = "D:\CP\timetable.pdf"
# $Mail.Attachments.Add($File)
$Mail.Send()
       } # End of Process section
End {
# Section to prevent error message in Outlook
$Outlook.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook)
$Outlook = $null
   } # End of End section!
} # End of function

# Example of using this function
Send-Email #-Address User2@domain.com

注意:如果要以某人的名義發送電子郵件,則必須啟用來自連接器的匿名郵件,否則該用戶應具有以某人的名義發送郵件的權限。 在這種情況下,您可以再添加一個對象作為

$ mail.From =“”

一個從GMAI1發送郵件作為參考的示例示例

$From = "YourEmail@gmail.com"
$To = "AnotherEmail@YourDomain.com"
$Cc = "YourBoss@YourDomain.com"
$Attachment = "C:\temp\Some random file.txt"
$Subject = "Email Subject"
$Body = "Insert body text here"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential (Get-Credential) -Attachments $Attachment

希望能幫助到你...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM