send mail with powershell

Posted on 2009-02-15
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
    LVL 17

    Expert Comment

    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

    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 = ""
    $ToAddress = ""
    $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

    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 = ""}
        $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) {$$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 = ""
    $sFrom = ""
    $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

    LVL 12

    Expert Comment

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

    Expert Comment

    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

    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

    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

    *mutter* ok, that last $pass should read $password. Sorry, it's been a long day.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
    The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
    This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
    The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now