powershell - Powershell 使用 Gmail 在文件夹中发送 .csv 文件
问题描述
我意识到以前有人问过这个问题,但我无法使用相关问题找到解决方案。
我正在尝试使用来自另一个 gmail 帐户(senderexample@gmail.com)的凭据,使用 Powershell 将两个 .csv 文件发送到一个 gmail 帐户(externalexample@gmail.com)。我已为这两个帐户打开了不太安全的应用程序功能,但在尝试运行我的脚本时出现以下错误:
> Exception calling "Send" with "1" argument(s): "The SMTP server
> requires a secure connection or the client was not authenticated. The
> server response was: 5.7.0 Authentication Required. Learn more at" At
> C:\users\luke\downloads\EmailTest.ps1:37 char:1
> + $smtp.Send($msg)
> + CategoryInfo: NotSpecified: (:) [], MethodInvocationException
> + FullyQualifiedErrorId : SmtpException
我的脚本:
#Connection Details
$username="senderexample@gmail.com"
$password="senderpassword"
$smtpServer = “smtp.gmail.com”
$msg = new-object Net.Mail.MailMessage
#Change port number for SSL to 587
$smtp = New-Object Net.Mail.SmtpClient($smtpServer, 587)
#Uncomment Next line for SSL
$smtp.EnableSsl = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($username, $password)
#From Address
$msg.From = "senderexample@gmail.com"
#To Address, Copy the below line for multiple recipients
$msg.To.Add(“externalexample@gmail.com”)
#Message Body
$msg.Body=”Please See Attached Files”
#Message Subject
$msg.Subject = “Email with Multiple Attachments”
#your file location
$files=Get-ChildItem “C:\Users\luke\Daily Stats\”
Foreach($file in $files)
{
Write-Host “Attaching File :- ” $file
$attachment = New-Object System.Net.Mail.Attachment –ArgumentList $file.FullName
$msg.Attachments.Add($attachment)
}
$smtp.Send($msg)
$attachment.Dispose();
$msg.Dispose();
非常感谢所有帮助!
解决方案
推荐阅读
- python - 提取游戏数据:不正确的值
- matplotlib - 图形格式 - X 轴表示法和注释 - Matplotlib
- c++ - 在标头和源中分离类会导致构建错误
- c# - 如何使用 WorldCat API 检索提供 ISBN 的图书的所有书目数据
- javascript - 在 Array.prototype.find 的实现中,为什么我们需要使用 Objetct(this) 来转换为对象?
- haskell - 我无法理解的haskell中的意外错误
- node.js - 如何在express js,mongodb中使用put请求更新数据
- session - Symfony 5:“也许你忘记在实体管理器中持久化它了?” --> Doctrine 折旧的 merge() 函数的替代方案
- sql - Hash Group By 和 Sort Group By 在 Oracle 内部如何工作?
- r - visNetwork R:指定变量节点背景颜色和边框颜色