【问题标题】:Send-MailMessage - Unable to connect to the remote serverSend-MailMessage - 无法连接到远程服务器
【发布时间】:2019-02-15 17:00:06
【问题描述】:

我在 Windows Server 2016 上的服务器上工作。 我的目标是使用 PowerShell 发送邮件,我编写了下面的脚本。

Send-MailMessage -From "john.doe@mydomaine.com" -To "john.doe@mydomaine.com" -Subject "votre objet" -SmtpServer "smtp.office365.com" -Body "Blablabla" -Credential "john.doe@mydomaine.com" -Port "587" -UseSsl

当我在 PC 上执行此脚本时,它运行良好,邮件已发送。所以我的脚本没问题。

当我在服务器上执行它时,出现“无法连接到远程服务器”的错误。当我ping smtp.office365.com 时,就可以了。

有人知道问题出在哪里吗?

这里是完整的错误:

Send-MailMessage : 不可能 de se 连接器 au serverur 遥远 Au caractère Ligne:1 : 1 + 发送邮件消息-从“john.doe@mydomaine.com”-到“john.doe@myd ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation : (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException + FullyQualifiedErrorId:SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

【问题讨论】:

  • 试试telnet smtp.office365.com 587 - 它可能被阻止(如果你没有telnet客户端,你可能需要在你的服务器上安装它)
  • 我已经在服务器上安装了 telnet。我试过你的命令行并获得:"Connexion à smtp.office365.com...Impossible d’ouvrir une connexion à l’hôte, sur le port 587: Échec lors de la connexion"(带翻译:Unable to open a connection on the host
  • 这样可以确认防火墙正在阻止访问。

标签: powershell smtp


【解决方案1】:

我刚刚找到了解决方案。我现在使用的是 25 端口,它可以正常工作,邮件是从服务器正确发送的。

【讨论】:

    【解决方案2】:

    第 1 步: 包括 $SMTPPort:25 和 $SMTPPort:587

    第 2 步:您可能必须在您的 Google 帐户设置中允许访问“不太安全的应用程序” 转到https://myaccount.google.com/lesssecureapps 并将其设置为“开启”并重试

    【讨论】:

    • 如何添加第二个端口?你放两行吗?
    【解决方案3】:

    获取用户凭据

    $username = "xxx@gmail.com"
    $password = "" | ConvertTo-SecureString -AsPlainText -Force
    

    为电子邮件创建哈希

    $email = @{
      from = $username
      to = "xxx@gmail.com"
      subject = "Hello"
      smtpserver = "smtp.gmail.com"
      body = "hi"
      credential = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $password
      Port = "587" 
      UseSsl = $true
    }
    
    Send-MailMessage @email
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 2011-04-29
      • 2010-11-13
      • 2017-06-26
      相关资源
      最近更新 更多