send mail with powershell

Posted on 2009-02-15
Medium Priority
Last Modified: 2012-06-21

i would like to send a email from server with exchange tools installed but my script says :

5.7.1 Client was not authenticated

i don't known how to use Credentials with script in powershell
Question by:youp_johan
  • 6
LVL 17

Expert Comment

ID: 23647553
Just make the following changes :

1. Uncheck the TLS checkbox that is under the Basic Authentication selection on
your default receive connector and restart Microsoft Exchange Transport service.
2. Check the options Log on using Secure Password Authentication (SPA) on your
Outlook client.

Author Comment

ID: 23647645
ok, but in my script what is the command to use authentification ? (i don't use outlook)

###Send mailbox statistics script

###First, the administrator must change the mail message values in this section
$FromAddress = "info@xxx.com"
$ToAddress = "jma@xxx.com"
$MessageSubject = "Mailbox Size Report"
$MessageBody = "Attached is the current list of mailbox sizes."
$SendingServer = "hub1"

###Now get the stats and store in a text file
Get-MailboxStatistics -server mbx | Sort-Object TotalItemSize -Descending | ft DisplayName,@{label="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}}, ItemCount > c:\mailboxes.txt

###Create the mail message and add the statistics text file as an attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress,
$MessageSubject, $MessageBody
$Attachment = New-Object Net.Mail.Attachment("c:\mailboxes.txt")

$SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer
$SMTPClient.UseDefaultCredentials = False
$SMTPClient.Credentials = System.Net.NetworkCredential("user", "pass")     ->    does not work ?

LVL 12

Expert Comment

ID: 24328494
YOu shouldn't have to authenticate if you do what the x-sam says. Another way is to use connect to a hub server to send from. My code looks as follows:

First I use a function for the meat of the send mail script. The rest is just filling in the blanks.
function Send-SMTPmail($to, $from, $subject, $body, $attachment, $cc, $bcc, $port, $timeout, $smtpserver, [switch] $html, [switch] $alert) {
    if ($smtpserver -eq $null) {$smtpserver = "HARUMPH.HARUMPH.com"}
    $mailer = new-object Net.Mail.SMTPclient($smtpserver)
    if ($port -ne $null) {$mailer.port = $port}
    if ($timeout -ne $null) {$mailer.timeout = $timeout}
    $msg = new-object Net.Mail.MailMessage($from,$to,$subject,$body)
    if ($html) {$msg.IsBodyHTML = $true}
    if ($cc -ne $null) {$msg.cc.add($cc)}
    if ($bcc -ne $null) {$msg.bcc.add($bcc)}
    if ($alert) {$msg.Headers.Add("message-id", "<3bd50098e401463aa228377848493927-1>")}
    if ($attachment -ne $null) {
        $attachment = new-object Net.Mail.Attachment($attachment)
$Addr = "dude@domain.com"
$sFrom = "dude2@domain.com"
$sTitle = "This is the subject of the email"
$sBody = "<p> The body of the email in HTML format. </p>"
Send-SMTPmail -to "$Addr" -from "$sFrom" -subject "$sTitle" -body "$sBody" -html

Open in new window

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 12

Expert Comment

ID: 24328553
Under the $smtpserver in the function, that is where you would put your hub server address.
LVL 12

Expert Comment

ID: 24329616
In answer to your actual question however, try the following article:


It discusses how to store credentials securely and then pass them securely to your script.
LVL 12

Expert Comment

ID: 24329651
In a nutshell, the blog says to do the following:
First, we need to get our password, then pump it into a file.  Doing this encodes the password and stores it in our output file so no-one can read it.

read-host -assecurestring | convertfrom-securestring | out-file C:\cred.txt
$password = get-content C:\cred.txt | convertto-securestring
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "myusername",$pass

After that you would simply use:
$SMTPClient.Credentials = $credentials
LVL 12

Expert Comment

ID: 24329689
I know I keep adding more, but I feel I should also point out, after you enter read-host -assecurestring | convertfrom-securestring | out-file C:\cred.txt, it will wait for you to put in your password. This only needs to be run once to create the file. After than, until your password changes, you can use the same file over and over again in your script with just the following code added to it:

$password = get-content C:\cred.txt | convertto-securestring
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "myusername",$pass

$SMTPClient.Credentials = $credentials
LVL 12

Accepted Solution

GusGallows earned 2000 total points
ID: 24329720
*mutter* ok, that last $pass should read $password. Sorry, it's been a long day.

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post, we will learn to set up the Group Naming policy and will see how it is going to impact the Display Name and the Email addresses of the Group.
There are literally thousands of Exchange recovery applications out there. So how do you end up picking one that’s ideal for your business & purpose? By carefully scouting the product’s features, the benefits it offers you, & reading ample reviews f…
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question