使用Powershell发送Gmail [英] send gmail using powershell

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

问题描述

我也在为自己的房屋开发一个简单的监控器,我需要它给我发送电子邮件。但是,无论我如何尝试,都不会连接到我的Gmail帐户来发送通知-甚至是简单的测试消息。我有一个脚本,该脚本适用于为工作而编写的实用程序,但是使用的是公司SMTP中继。我不会混合工作和个人资源。

I'm developing a simply monitoring too for my home and I need it to send me an email. However, no matter what I try, it is not connecting to my gmail account to send the notification - or even a simple test message. I have a script that is working for a utility I wrote for work, but that is using the corporate SMTP relay. I'm not going to mix work and personal resources.

我花了过去4-5周的时间来研究和尝试其他几种开发语言。所有人都有类似的问题。

I have spent the past 4-5 weeks digging through this and trying several other development languages. All have come up with similar problems.

我尝试了以下操作:


  • 打开安全性较低的应用程序

  • 尝试了25、465、587等各种端口

  • 打开和关闭SSL

  • 尝试了多种形式的用户名(电子邮件地址)

  • 启用了2因子身份验证并为此实用程序创建了应用密码

  • 尝试打开TLS-出现一个错误,表明我需要一个starttls命令。

  • Turned on less secure apps
  • Tried a variety of ports, 25, 465, 587
  • Turned on and off SSL
  • Tried various forms of my username (email address)
  • Turned on 2-factor authentication and created an app password for this utility
  • Attempted to turn on TLS - as one error indicated I needed a starttls command.

在Powershell中,我似乎遇到了两个常见错误。第一个取决于我要发送给Send参数的参数-无论如何,它都会失败。

With Powershell, I seem to get two common errors. The first one is dependent on the parameters I'm sending to the Send argument - regardless, it fails.

[错误屏幕截图-] [1]

[Screenshot of error - ][1]

我去过多个论坛和站点。所有建议的代码似乎都是相同结构的所有变体,而与我办公室的工作代码相同,只是使用我们自己的邮件系统。

I have been on multiple forums and sites. All of the suggested code appears to be all variants of the same structure, as is the working one for my office - just using our own mail system.

推荐答案

您应该可以使用 Send-MailMessage 。以下是使用Gmail的SMTP服务器安全发送电子邮件的示例(包括如何安全存储凭据以便以后使用或在自动化中使用):

You should be able to use Send-MailMessage for this. Here's an example for sending an email securely using Gmail's SMTP server (including how to securely store your credential for further use later or in automation):

# Securely enter your Gmail credential
$cred = Get-Credential

# Store it off in a file if you want to reuse it later from the same box
# Password is encrypted and only your user on the same box can decrypt it
$cred | Export-CliXml \Path\To\GmailCredential.xml

# Import the credential from the file later
$cred = Import-CliXml \Path\To\GmailCredential.xml

# Send the mail message (use -BodyAsHtml instead of -Body if you have an HTML body)
$sendMailArguments = @{
  SmtpServer = "smtp.gmail.com"
  Credential = $cred
  UseSsl = $true
  Port = 587
  To = "mailbox@domain.tld"
  From = "youremail@domain.tld"
  Subject = "Subject of the email"
  Body = "Body of the email"
}
Send-MailMessage @sendMailArguments

请注意,如果您设置了多重身份验证(MFA),则需要特定于应用程序的凭据来验证Gmail,这是必需步骤以及使用MFA为Gmail帐户设置电子邮件客户端。

Note that if you have Multi-Factor Authentication (MFA) set up you will need an app-specific credential to auth to Gmail, which would be a required step as well for setting up an email client for a Gmail account using MFA.

如果您无法达到 smtp .gmail.com ,则可能您的防火墙阻止了到该服务器和端口的流量。

If you can't reach smtp.gmail.com, then it's likely you have a firewall blocking traffic to that server and port.

如果您想更好地理解为什么我要使用参数的哈希图调用 Send-MailMessage 而不是分别指定每个参数,请参阅注释讨论此答案,并在此处阅读Splatting: https://docs.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-6

And if you want to better understand why I'm calling Send-MailMessage with a hashmap of arguments instead of specifying each parameter separately, see the comment discussion on this answer, and read up on Splatting here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-6

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

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