我正在尝试从PowerShell脚本发送电子邮件,但似乎无法使其通过SMTP服务器正确进行身份验证。

$fromEmail = 'local@example.com';
$toEmail = 'remote@example.net';
$mailUser = 'myUsername';
$mailPassword = 'myPassword';
$smtpServer = 'smtp.example.com';
$smtpPort = 587;
$content = 'Email message content';
$subject = 'Email message subject';

$credentials = New-Object System.Net.NetworkCredential $mailUser, $mailPassword;

$server = New-Object System.Net.Mail.SmtpClient $smtpServer, $smtpPort;
$server.UseDefaultCredentials = $false;
$server.EnableSSL = $true;
$server.Credentials = $credentials
$server.Send($fromEmail, $toEmail, $subject, $content);

当我尝试在Powershell提示符中运行以上命令时,会导致指定错误的中继访问被拒绝。
Exception calling "Send" with "4" argument(s): "Client does not have permission to submit mail to this server. The
server response was: 4.7.1 <remote@example.net>: Relay access denied"
At line:1 char:1
+ $server.Send($fromEmail, $toEmail, $subject, $content);
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SmtpException

如果我尝试使用Thunderbird或PHP的Swiftmailer以及相同的服务器和凭据设置发送电子邮件,则可以正常工作。从查看后缀日志来看,似乎SmtpClient甚至没有尝试进行身份验证,而是只是尝试匿名发送邮件。

这是雷鸟制作的一个好的日志条目,如下所示:
Jun 14 21:17:42 services postfix/submission/smtpd[16653]: connect from unknown[xxxx]
Jun 14 21:17:43 services postfix/submission/smtpd[16653]: 59074F96E: client=unknown[xxxx], sasl_method=PLAIN, sasl_username=local@example.com

而SmtpClient所做的输入如下所示:
Jun 14 22:11:16 services postfix/submission/smtpd[16824]: connect from unknown[xxxx]
Jun 14 22:11:16 services postfix/submission/smtpd[16824]: NOQUEUE: reject: RCPT from unknown[xxxx]: 454 4.7.1 <remote@example.net>: Relay access denied; from=<local@example.com> to=<remote@example.net> proto=ESMTP helo=<WTFv2>

作为测试,我还尝试使用我的gmail凭据通过gmail地址发送邮件,但由于未进行身份验证而失败。
Exception calling "Send" with "4" argument(s): "The SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"
At line:1 char:1
+ $server.Send($fromEmail, $toEmail, $subject, $content);
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SmtpException

我在这里想念什么? .net SmtpClient为什么不发送身份验证以允许邮件通过?

最佳答案

您可以尝试这种方式吗,这是我用于Gmail的方式,我使用System.Net.NetworkCredential作为凭据(来自我的C#代码):

$smtpServer = "smtp.gmail.com"
$smtpServerport = "587"
$emailSmtpUser = "toto@myDomain.com"
$emailSmtpPass = "toto.123"

$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = "toto@myDomain.com"
foreach ($addr in $to)
{
  $emailMessage.To.Add($addr)
}
foreach ($attachment in $attachments)
{
  $emailMessage.Attachments.Add($attachment)
}
$emailMessage.Subject = "The subject"
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = $body

$smtpClient = New-Object System.Net.Mail.SmtpClient($smtpServer , $smtpServerport)
$smtpClient.EnableSsl = $true
$smtpClient.Credentials = New-Object System.Net.NetworkCredential($emailSmtpUser , $emailSmtpPass);

$SMTPClient.Send( $emailMessage )

关于powershell - 为什么SmtpClient无法在我的Powershell脚本中进行身份验证?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44557744/

10-15 03:05
查看更多