Search This Blog

Tuesday, June 21, 2016

Powershell: sending emails via office365 TLS

Recently had to work on sending emails from a powershell script using the office365 smtp server. This means I had to use TLS and also use authentication.

Script below is pretty simple and does not encrypt the password in the script this just a poc.

The script creates an array of users to send the email to ($users), then this is looped through.

This line creates the email object, this is the best way to send email and I believe the only way if you need to enable TLS.

$client = New-Object system.Net.Mail.SmtpClient $smtpserver, $port 

Using the new email object this line enables TLS

$client.EnableSsl = $true 

This next line creates the user credentials that the object will use to authenticate with office365 system when it tries to send the email.

$client.Credentials = New-Object System.Net.NetworkCredential( $office365User , $office365Password ); 

I leave in, but its commented out, but you can get it you use the account the script is run under.

#$client.Credentials = [system.Net.CredentialCache]::DefaultNetworkCredentials 

Full script

[string]$to = ""
[String]$from = "<office365User@office365domain.com>"
[String]$subject = "<subject>"
[String[]]$users = "<user1@domain.com>, <user2@domain.com>"
[String]$smtpserver = "smtp.office365.com"
[string]$office365User = '<office365User@office365domain.com>';
[string]$office365Password = '<office365UserPassword>';
[String]$port = 25

foreach ($user in $users)
{
    trap
    {
        #$CRLF added because usual `r`n in string doesnot work within trap.
        [string]$CRLF = [char]13 + [char]10
        $script:errorMessage += '"' + $smtpserver + '" : Error: {0}' -f $_.Exception.Message + $CRLF;
        $Error.Clear();
        continue;
    }
    $to      = $user
    $output = "<pre>" + "<font color='#800000'>scriptErrors: `r`n" + $script:errorMessage + "</font>`r`n"
    $output += "<b>" + $outputHeader + "</b><font color='#800000'>" + $outputAlert + "</font>" + "<b>" + $outputSubHeader + "</b>" + $outputAll + "</pre>"

    # Create mail message
    $message = New-Object system.net.mail.MailMessage
    $message.From = $from;
    foreach ($useremail in $to)
    {
        $message.To.Add($useremail);
    }
    $message.Subject = $subject;
    $message.Body = $output;
    $message.IsBodyHtml = $true;
    #Create SMTP client
    $client = New-Object system.Net.Mail.SmtpClient $smtpserver, $port
    $client.EnableSsl = $true
    # Credentials are necessary if the server requires the client # to authenticate before it will send e-mail on the client's behalf.
    #$client.Credentials = [system.Net.CredentialCache]::DefaultNetworkCredentials
    $client.Credentials = New-Object System.Net.NetworkCredential( $office365User , $office365Password );
     
    # Try to send the message
    $client.Send($message)
    # reset variables
    $body = ""
    $message.Dispose()
    $client.Dispose()
}

Share/Bookmark

No comments:

Post a Comment