使用Powershell从其他邮箱发送电子邮件 [英] Send email with Powershell from different mailbox

查看:81
本文介绍了使用Powershell从其他邮箱发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小的PS脚本,以创建电子邮件,供我的管道在部署时发送出去.问题是我不希望从我的个人电子邮件中发送电子邮件,而是从公司展望电子邮件中发送电子邮件.我搜索并看到了不同的SMTP服务器名称并使用mail.from,但我无法使它正常工作.有人可以帮我吗?

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() 

推荐答案

您需要为 SendUsingAccount 属性分配一个值.可以在(外观).Session.Accounts 集合中找到该帐户.

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 }

然后,在发送之前将其分配给SendUsingAccount属性

then, assign to the SendUsingAccount property before sending

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

完整示例

$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()

对于它的价值,我很久以前就放弃了尝试通过Outlook发送电子邮件的方法,使用纯SMTP更加容易.根据本地SMTP服务器(Exchange?)上的安全策略,您也许可以发送为"本地域中的任何用户.向您的IT人员询问可用于发送电子邮件的内部SMTP服务器的名称/IP,然后就可以了:

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"

如果使用 send-mailmessage ,则可以使用"Display Name< sender.name@domain.com>"形式为发件人设置显示名称.例如

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>"

收件人将在其电子邮件客户端中看到显示名称,而不是SMTP地址.

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

我认为是很好的做法:

  1. 取决于SMTP服务器的配置,可能很少或没有对发件人"地址的验证.值得使用您可以访问的真实帐户,以便查看所有退回/未送达报告.
  2. 请考虑在邮件正文中添加一些内容(可能是页脚),以提及警报的来源以及生成该警报的过程.这可以帮助您的继任者或同事将来查找脚本.

这篇关于使用Powershell从其他邮箱发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆