Send email with Powershell from different mailbox

空扰寡人 提交于 2020-06-28 05:01:23

问题


I have created a small PS script to create an email for my pipeline to send out whenever there is a deployment. the problem is i dont want the email to be sent from my personal email but from the company outlook email. i searched and saw different SMTP server names and using mail.from but i cant get it to work. can someone help me out?

 param(
[Parameter(Mandatory=$true,Position=0)]
[string]$Address1,
[Parameter(Mandatory=$true,Position=1)]
[string]$Address2,
[switch]$Recurse,
[switch]$Force  
)

 $ol = New-Object -comObject Outlook.Application  
 $mail = $ol.CreateItem(0)  
 $Mail.Recipients.Add($Address1) 
$Mail.Recipients.Add($Address2) 
$Mail.Subject = "DSC Deployment in Progress"  
$Mail.Body = "There is a DSC install beginning. . ."  
$Mail.Send() 

回答1:


You need to assign a value to the SendUsingAccount property. The account can be found in the (outlook).Session.Accounts collection.

$sendSmtpAddress = "some.name@somedomain.com"
$account = $ol.session.acounts | ? { $_.smtpAddress -eq $sendSmtpAddress }

then, assign to the SendUsingAccount property before sending

$mail.SendUsingAccount = $account
$mail.Send()

Full example

$sendSmtpAddress = "some.name@somedomain.com"
$ol = new-object -comobject "outlook.application"
$account = $ol.session.accounts | ? { $_.smtpAddress -eq $sendSmtpAddress }
$mail = $ol.CreateItem(0)
$mail.recipients.add("target.user@somedomain.com") | out-null
$mail.subject = "test email"
$mail.body = "test email"
$mail.SendUsingAccount = $account
$mail.Send()

For what it's worth, I gave up trying to send email via Outlook a long time ago, it's much easier to use plain SMTP. Depending on the security policy on your local SMTP server (Exchange?), you may be able to 'send as' any user on your local domain. Ask your IT people for the name/IP of an internal SMTP server that you can use to send email, and then it's as easy as:

send-mailmessage -smtpServer (servername or IP) -from sender.name@domain.com -to @(recipient1@domain.com, recipient2@domain.com) -subject "Email Subject" -body "Email Body"

If using send-mailmessage, it's possible to set a Display Name for the sender by using the form "Display Name <sender.name@domain.com>" e.g.

-from "Deployment Alerts <sender.name@domain.com>"

Recipients will see the Display Name in their email client, rather than the SMTP address.

A couple of points that I consider to be good practice:

  1. Depending on the config of the SMTP server, there may be little, or no verification of the 'sender' address. It is worth using a genuine account that you have access to, so that you have sight of any bounce / non-delivery reports.
  2. Consider including something in the mail body (perhaps a footer) that mentions where the alert came from and what process generated it. This can help your successor or colleague track down the script in the future.


来源:https://stackoverflow.com/questions/41149684/send-email-with-powershell-from-different-mailbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!