locked
Send Email by Powershell RRS feed

  • שאלה

  • Hi,

     I tried to send email from my server by powershell script to my corp exchange server but I faced authenticated and certificate problems, I tried run 2 codes but still now I can't send.

    Note: Just port 25 that opened in firewall.

    Code #1 :

    $from = New-Object system.net.mail.MailAddress "myuser@mydomain.net"
    $to = New-Object system.net.mail.MailAddress "myuser@gmail.com"
    $message = new-object system.net.mail.MailMessage $from, $to
    $message.Subject = "Test Powershell "
    $message.Body = "Test Powershell"
    
    $smtpserver = "smtp.mydomain.net"
    $client = new-object system.net.mail.smtpclient $smtpserver
    $client.UseDefaultCredentials = $TRUE
    $client.Send($message)
    Error: Exception calling "Send" with "4" argument(s): "The remote certificate is invalid according to the validation procedure
    ."
    At C:\Users\Administrator\Desktop\myscript.ps1:46 char:17
    + $SMTPClient.Send <<<< ($EmailFrom, $EmailTo, $Subject, $Body)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

    PS C:\Users\Administrator\Desktop>


     

    Code #2 :

    $CredUser = "myuser"
    $CredPassword = "xxxx"
    
    $EmailFrom = "myuser@mydomain.net"
    $EmailTo = "myuser@gmail.com"
    $Subject = "Test mail Subject"
    $Body = "Test Email Body"
    $SMTPServer = "smtp.mydomain.net"
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($CredUser, $CredPassword);
    $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
    Error: Exception calling "Send" with "1" argument(s): "The SMTP server requires a secure connection or the client was not auth
    enticated. The server response was: 5.7.1 Client was not authenticated"
    At C:\Users\Administrator\Desktop\myscript.ps1:44 char:13
    + $client.Send <<<< ($message)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException






    יום רביעי 02 אוקטובר 2013 11:30

תשובות

  • Your post doesn't give any information as to why the client thinks the server certificate is invalid.  It could be that the hostname you're using to connect doesn't match what's on the SSL certificate, or it could be expired, or issued by a CA your client computer doesn't trust (or is self-signed, which amounts to the same thing).

    You can either figure out the certificate issue, or you can try adding this command before the call to Send, to disable certificate validation:

    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true }

    Note:  I've executed this command in PowerShell and it seems to work properly, but I don't have a quick way to test whether this will fix your problem.  It's a PowerShell translation of a solution to this problem in C# / VB.NET.
    • סומן כתשובה על-ידי Yan Li_ יום חמישי 10 אוקטובר 2013 02:42
    יום רביעי 02 אוקטובר 2013 11:41
  • Your exchange server is setup to require authentication, and you aren't using it in the powershell code. If this is an internal machine it may be easier to create a relay connector in exchange and allow only internal ip addresses or ip address form machine running the script to relay. If you don't have exchange IPS's will normally provide a relay server to customers, you could use that, i know emails would go out the WAN and then back in but it's a quick and easy option, to change the server your are relaying through.
    • סומן כתשובה על-ידי Yan Li_ יום חמישי 10 אוקטובר 2013 02:42
    יום רביעי 02 אוקטובר 2013 12:34

כל התגובות

  • Your post doesn't give any information as to why the client thinks the server certificate is invalid.  It could be that the hostname you're using to connect doesn't match what's on the SSL certificate, or it could be expired, or issued by a CA your client computer doesn't trust (or is self-signed, which amounts to the same thing).

    You can either figure out the certificate issue, or you can try adding this command before the call to Send, to disable certificate validation:

    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true }

    Note:  I've executed this command in PowerShell and it seems to work properly, but I don't have a quick way to test whether this will fix your problem.  It's a PowerShell translation of a solution to this problem in C# / VB.NET.
    • סומן כתשובה על-ידי Yan Li_ יום חמישי 10 אוקטובר 2013 02:42
    יום רביעי 02 אוקטובר 2013 11:41
  • Your exchange server is setup to require authentication, and you aren't using it in the powershell code. If this is an internal machine it may be easier to create a relay connector in exchange and allow only internal ip addresses or ip address form machine running the script to relay. If you don't have exchange IPS's will normally provide a relay server to customers, you could use that, i know emails would go out the WAN and then back in but it's a quick and easy option, to change the server your are relaying through.
    • סומן כתשובה על-ידי Yan Li_ יום חמישי 10 אוקטובר 2013 02:42
    יום רביעי 02 אוקטובר 2013 12:34