Search code examples
powershellsendmail

How to send mail from powershell without giving username and password details from a server which is in domain and it is logged in as service account


I tried send-mailmessage with no luck. I tried with New-Object Net.Mail.SmtpClient() with no luck thanks in advance


Solution

  • I Agree with @Theo, it would be helpful if you build up the question a little. That said, if you are using Send-MailMessage it'll use windows authentication, meaning the username & password of the user running the process. In an Exchange environment (which I assume you mean by "domain"), this often causes issues because the user doesn't have rights to submit mail. A quick workaround is to create a anonymous credential and cite it in the Send-MailMessage command. That would look something like:

    $Pass = New-Object System.Security.SecureString
    $Creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "NT AUTHORITY\ANONYMOUS LOGON", $Pass
    
    Send-MailMessage -To [email protected] From [email protected] -Subject "Test" -SmtpServer mySMTPServer.myorg.com -Credential $Cred
    

    You can read a bit about the issue here , Note: I'm Nashiooka in that conversation.

    A slightly more concise version, using some newer conventions:

    $Cred = [System.Management.Automation.PSCredential]::new( 'NT AUTHORITY\ANONYMOUS LOGON', [System.Security.SecureString]::New() )
    
    Send-MailMessage -To [email protected] From [email protected] -Subject "Test" -SmtpServer mySMTPServer.myorg.com -Credential $Cred
    

    Obviously you should update with your own information.