使用 powershell 使用 gmail 发送电子邮件 [英] sending email with gmail using powershell

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

问题描述

我正在尝试使用 powershell 脚本通过 gmail smtp 服务器发送电子邮件.但我无法发送电子邮件..我正在使用以下脚本-

i am trying to send emails though gmail smtp server using powershell script. but i am not able to send emails .. i am using below script-

$EmailFrom = "bttpm@gmail.com"

$EmailTo = "kamlesh.bhatt@domain.com" 

$Subject = "Notification from XYZ" 

$Body = "this is a notification from XYZ Notifications.." 

$SMTPServer = "smtp.gmail.com" 

$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 

$SMTPClient.EnableSsl = $true 

$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("bttpm", "Password"); 

$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

推荐答案

这对我有用:

$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "username@gmail.com"
$Password = ""

$to = "user1@domain.com"
$cc = "user2@domain.com"
$subject = "Email Subject"
$body = "Insert body text here"
$attachment = "C:	est.txt"

$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $body
$message.to.add($to)
$message.cc.add($cc)
$message.from = $username
$message.attachments.add($attachment)

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)
write-host "Mail Sent"

但是,您可能会收到以下错误消息:

However, you may get this error message:

"The SMTP server requires a secure connection or the client was not authenticated. 
The server response was: 5.5.1 Authentication Required. "

这是因为 Gmail 的默认安全设置阻止了连接,正如来自 Google 的自动消息所建议的那样.因此,只需按照消息中的说明启用访问安全性较低的应用程序"即可.风险自负.:)

This is because the default security settings of Gmail block the connection, as suggested by the auto message from Google. So just follow the instructions in the message and enable "Access for less secure apps". At your own risk. :)

更多信息在这里:http://petermorrissey.blogspot.ro/2013/01/sending-smtp-emails-with-powershell.html

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

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