无法在Powershell中使用send-MailMessage连接到服务器

问题描述 投票:0回答:1

我一直在尝试创建一封电子邮件,该电子邮件将在运行PoweShell文件时从gmail帐户发送。但是,使用send-MailMessage方法时,我无法连接到服务器。我有以下代码:

$from = "[email protected]"
$to = "[email protected]"
$subject = "The subject of your email"
$body = "This is just a test mail to verify the working of CMD"
$attachment = "C:\Users\37.csv"
$smtp = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("[email protected]", "xxx");

send-MailMessage -SmtpServer $smtp -Credential $SMTPClient -To $to -From $from -Subject $subject -Body $body -Attachment $attachment -Priority high 

任何帮助将不胜感激。

powershell email credentials
1个回答
0
投票

您正在使用错误的凭据参数调用Send-MailMessage Cmdlet。从Commandlet帮助中,它接受pscredential

SYNTAX
Send-MailMessage [-To] <string[]> [-Subject] <string> [[-Body] <string>] [[-SmtpServer] <string>] -From <string> [-Attachments <string[]>] [-Bcc <string[]>] [-BodyAsHtml] [-Encoding <Encoding>] [-Cc <string[]>] [-DeliveryNotificationOpt
ion {None | OnSuccess | OnFailure | Delay | Never}] [-Priority {Normal | Low | High}] [-ReplyTo <string[]>] [-Credential <pscredential>] [-UseSsl] [-Port <int>] [<CommonParameters>]

您正在传递$SMTPClient,即Net.Mail.SmtpClient

只需创建一个伪对象并将其传递给-Credential。还要完全删除$SMTPClient

© www.soinside.com 2019 - 2024. All rights reserved.