首页 > 解决方案 > 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();

非常感谢所有帮助!

标签: powershellsmtpgmail

解决方案


推荐阅读