Category: PowerShell Script

  • Sending an email via PowerShell

    The below PowerShell block will give you details on how to setup a SMTP script for sending email.

    # ==============================
    # SMTP CONFIGURATION
    # ==============================
    $From        = "[email protected]"     # The "From" address (must be allowed to send mail)
    $To          = "[email protected]"         # Recipient email
    $SMTPServer  = "smtp.yourdomain.com"        # SMTP server address
    $SMTPPort    = 587                          # SMTP port (25/465/587 depending on your setup)
    $SMTPUser    = "[email protected]"     # SMTP auth user
    $SMTPPass    = "YOUR_SMTP_PASSWORD"         # SMTP password
    
    
    
    Now you've setup the basics to send out emails.
    
    Now the part of sending an actual email:
    
    
    EMAIL REPORT
    # ==============================
    Write-Host "Sending email" -ForegroundColor Cyan
    
    $SMTP = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
    $SMTP.EnableSsl = $true
    $SMTP.Credentials = New-Object System.Net.NetworkCredential($SMTPUser, $SMTPPass)
    
    $Mail = New-Object System.Net.Mail.MailMessage
    $Mail.From = $From
    $Mail.To.Add($To)
    $Mail.Subject = "Sending an email via SMTP"
    $Mail.Body = "Sending test email."
    
    
    $SMTP.Send($Mail)