SMTP send mail to users

HI,

I found this PowerShell on internet, when I run the PowerShell script I get the following error.  All my users are on a domain. do I need to pass something into the powershell?


send-mailmessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
At line:38 char:1
+ send-mailmessage -to $email -from noreply@domain.org -Subject "Password Reminder: ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage


import-module ActiveDirectory;
$maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
Get-ADUser -filter * -properties PasswordLastSet, PasswordExpired, PasswordNeverExpires, EmailAddress, GivenName | foreach {
    $today=get-date
    $UserName=$_.GivenName
    $Email=$_.EmailAddress
   if (!$_.PasswordExpired -and !$_.PasswordNeverExpires) {
        $ExpiryDate=$_.PasswordLastSet + $maxPasswordAgeTimeSpan
        $DaysLeft=($ExpiryDate-$today).days
        if ($DaysLeft -lt 5 -and $DaysLeft -gt 0){
        $WarnMsg = "
<p style='font-family:calibri'>Hi $UserName,</p>
<p style='font-family:calibri'>Your Windows login password will expire in 5 days, please press CTRL-ALT-DEL and change your password.  As a reminder, you will have to enter your new password into your DOMAIN connected mobile device if prompted.</p>
<p style='font-family:calibri'>Requirements for the password are as follows:</p>
<ul style='font-family:calibri'>
<li>Must not contain the user's account name or parts of the user's full name that exceed two consecutive characters</li>
<li>Must not be one of your last 7 passwords</li>
<li>Contain characters from three of the following four categories:</li>
<li>English uppercase characters (A through Z)</li>
<li>English lowercase characters (a through z)</li>
<li>Base 10 digits (0 through 9)</li>
<li>Non-alphabetic characters (for example, !, $, #, %)</li>
</ul>
<p style='font-family:calibri'>For any assistance, visit the <a href='http://ITHelpDesk'>IT Help Desk</a></p>
<p style='font-family:calibri'>-Technology Services</p>
"
ForEach ($email in $_.EmailAddress) {
send-mailmessage -to $email -from noreply@domain.org -Subject "Password Reminder: Your password will expire in $DaysLeft days" -body $WarnMsg  -smtpserver smtpserverIP -BodyAsHtml }

Write-Host "$email"
         }
    }
}
jwc1972Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Satyendra SharmaMicrosoft UC Technical ArchitectCommented:
Where are you running this script from?
Also are you running this under a valid domain user credential?

If you are running this script on a separate machine you might want to add the ip address of the machine to the relay list on exchange servers recieve connector.
jwc1972Author Commented:
HI,

I'm running the script from my local PC, with admin rights
I have add the Ip of the exchange server

$cred = get-credential
then insert this into sendmail
footechCommented:
If the SMTP server requires you to authenticate and you can't change that, then you have to provide credentials.
$cred = Get-Credential
send-mailmessage -to $email -from noreply@domain.org -Subject "Password Reminder: Your password will expire in $DaysLeft days" -body $WarnMsg  -smtpserver smtpserverIP -BodyAsHtml -credential $cred

Open in new window

You would want to have $cred defined somewhere outside of a loop (like line 1 or 2) so that you aren't repeatedly asked to provide credentials for each user.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Virus Depot: Cyber Crime Becomes Big Business

The rising threat of malware-as-a-service is not one to be overlooked. Malware-as-a-service is growing and easily purchased from a full-service cyber-criminal store in a “Virus Depot” fashion. View our webinar recording to learn how to best defend against these attacks!

jwc1972Author Commented:
HI,

nope no luck, everything looks right, I'm wondering if something on the exchange server is blocking this

thank you for trying
Satyendra SharmaMicrosoft UC Technical ArchitectCommented:
Can you look at the protocol log on the receive connector on the exchange server?
If you have a dedicated relay connector can you try using that?
jwc1972Author Commented:
ok cheers,

our exchange server is hosted by another company, I will check in the morning
footechCommented:
I would've waited till you had verified a solution before closing the question.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.