locked
Send an emai from Power Shell RRS feed

  • Question

  • Hi,

    I've used the following code on Powershell both on my local company PC and on remote desktop client Server:

    $MyEmail = "jossyrossy86@gmail.com"
    $SMTP ="smtp.gmail.com"
    $To= "miri.castaldo@gmail.com"
    $Subject= "Test"
    $Body= "Ja faremo?"
    $Creds= (Get-Credential -Credential "$MyEmail")

    Start-Sleep 2

    Send-MailMessage -To $to -From $MyEmail -Subject $Subject -Body $Body -SmtpServer $SMTP -Credential $Creds -UseSsl -Port 587 -DeliveryNotificationOption Never

    The error received is:

    Send-MailMessage:Unable to connecto to the remote server

    Is it possible my company PC blocks sending of the email or is it the remote machine of the client the problem?

    Please support us, we are beginners for Powershell and we need send notification requested of our Client.

    Thanks,

    Miriana

    Wednesday, February 7, 2018 11:16 AM

Answers

  • Many companies block SMTP mail at the firewall.

    Here is the best way to use Send-MailMessage:

    $MyEmail  = 'jossyrossy86@gmail.com'
    $mailprops = @{
        From        = $MyEmail
        To          = 'miri.castaldo@gmail.com'
        Subject     = 'Test'
        Body        = 'Ja faremo?'
        Credential  = Get-Credential -Credential $MyEmail
        SmtpServer  = 'smtp.gmail.com'
    UseSsl = $true Port = 587 } Send-MailMessage @mailprops


    \_(ツ)_/



    • Edited by jrv Wednesday, February 7, 2018 11:52 AM
    • Proposed as answer by PRASOON KARUNAN V Monday, February 12, 2018 6:37 PM
    • Marked as answer by Richard MuellerMVP Saturday, February 17, 2018 11:45 AM
    Wednesday, February 7, 2018 11:51 AM

All replies

  • Hi,

    The issue seems to be the connection from your client to smtp.gmail.com.

    Maybe a firewall (on your client or somewhere else on the network) blocks the connection.

    Could you use a different smtp server to test against it?

    Wednesday, February 7, 2018 11:42 AM
  • Many companies block SMTP mail at the firewall.

    Here is the best way to use Send-MailMessage:

    $MyEmail  = 'jossyrossy86@gmail.com'
    $mailprops = @{
        From        = $MyEmail
        To          = 'miri.castaldo@gmail.com'
        Subject     = 'Test'
        Body        = 'Ja faremo?'
        Credential  = Get-Credential -Credential $MyEmail
        SmtpServer  = 'smtp.gmail.com'
    UseSsl = $true Port = 587 } Send-MailMessage @mailprops


    \_(ツ)_/



    • Edited by jrv Wednesday, February 7, 2018 11:52 AM
    • Proposed as answer by PRASOON KARUNAN V Monday, February 12, 2018 6:37 PM
    • Marked as answer by Richard MuellerMVP Saturday, February 17, 2018 11:45 AM
    Wednesday, February 7, 2018 11:51 AM
  • Have you send me an email?

    I have receveid an email form unknown account, but the subject anf the body is the same of my example above.

    How did you manage to succeed?

    The goal is to use the SMTP of the Client,but I have to ask IT Client to give me the SMTP.

    Best Regards,

    Miriana

    Wednesday, February 7, 2018 11:52 AM
  • Yes.  I used your code with my old, unused gmail account.  The code is correct.  Your connection is blocked.  Many companies block gmail access to prevent spam and for other security reasons.


    \_(ツ)_/

    Wednesday, February 7, 2018 11:54 AM
  • Have you manage to send the email from your personal PC?

    How can I find the Company SMTP?

    Miriana

    Wednesday, February 7, 2018 12:07 PM
  • You have to ask your company if that is possible.


    \_(ツ)_/

    Wednesday, February 7, 2018 12:10 PM
  • An alternative approach which may be easier provided the workstation has outlook set up and configured is to send via an outlook com object. 

    I've used the following function previously:

    function Send-Mail
    {
        [CmdletBinding()]
        [Alias()]
        [OutputType([int])]
        Param
        (
            # Specify the destination address
            [Parameter(Mandatory=$true,
                       ValueFromPipelineByPropertyName=$true,
                       Position=0)]
            $To,
    
            # Specify the subject line
            [Parameter(Mandatory=$true,
                       ValueFromPipelineByPropertyName=$true,
                       Position=0)]
            $Subject,
            # Specify the body contents
            [Parameter(Mandatory=$false,
                       ValueFromPipelineByPropertyName=$true,
                       Position=0)]
            $Body,
            # Specify an attachment
            [Parameter(Mandatory=$false,
                       ValueFromPipelineByPropertyName=$true,
                       Position=0)]
            $Attachment
        )
        Process{
            $Outlook = New-Object -ComObject Outlook.Application
            $Mail = $Outlook.CreateItem(0)
            $Mail.To = $To
            $Mail.Subject = $Subject
            $Mail.Body = $Body
            $Mail.Send()
            $Outlook.Quit()
        }
    }
    
    Send-Mail -To "email@email.com" -Subject "Subject line" -Body "Body text"
    

    Saturday, February 10, 2018 11:38 PM