简体   繁体   中英

How to send an outlook email from generic email address using PowerShell script?

I am trying to automate sending email task using PowerShell script.

I want to send an email using generic (common) email address, not using current logged in user.

I have tried SentOnBehalfOfName property, but no luck, it is still sent an email from current logged in user.

For an exmaple,

My email address is : abc@outlook.net

Generic (common) email address is : common@outlook.net

Here, I want to send an email from common@outlook.net from any computer.

Below is my code that I have tried so far.

$Outlook = New-Object -Com Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.SentOnBehalfOfName = "common@outlook.net"
$Mail.To = "abc@****.net"
$Mail.Subject = "Test Email"
$Mail.Body = "Testing"
$Mail.Send()
$Outlook.Quit() 

Please let me know, how can I accomplish it? Thank you in Advance.

I will use native System.Net.SMTPClient:

$to = 'mailadress1'
$from = 'mailadress2'
$onBehalfOf = 'mailadress3'
$subject = "powershell send on behalf"
$body = "body"
$smtpserver = "an SMTP server in your domain"
$mailer = new-object Net.Mail.SMTPclient($smtpserver) 
$msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body)
$msg.Sender = $onBehalfOf
$mailer.send($msg)

用户帐户需要为该通用帐户启用Send on Behalf权限,我已经使用在我的 Outlook 中设置的 2 个通用帐户进行了测试,并具有上述权限: Microsoft - 代表另一个用户发送电子邮件

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM