0

So I have a encrypted string stored in a text file that I can call again and again, this is the password for my SMTP Smart Host. This is created via:

Read-Host "Enter the Password:" -AsSecureString | ConvertFrom-SecureString | Out-File C:\EncPW.bin 

I now need to somehow pass this along with a $username to a -Credential parameter for a Send-MailMessage function. I assume I need to get this into a PSCredential format. But I'm stuck !

$password = cat C:\encPW.bin | ConvertTo-SecureString $password $cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username,$password 

As of now I have this, but it $password when I print it outputs System.Security.SecureString and I get an error:

net_io_connectionclosed 

When it tries to SMTP send.

Cheers

CALL to SMTP Send:

Send-MailMessage -from "[email protected]" -To "[email protected]" -Subject "Daily Report" -Body "Open Attachment for Report" -smtpServer 85.119.248.54 -Attachments "C:\WSB_Reports\DailyReport.txt" -Credential $cred 
12
  • securestring can not be printed out(that's the "secure" part). What have you tried? Send-MailMessage .... -Credential $cred ? Is this what outputs the error? Commented Dec 21, 2012 at 17:51
  • Yeah - then I get: Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed. Commented Dec 21, 2012 at 17:55
  • 2
    have you tried using a new credential without using Your password-bin file. just to test. $creds = Get-Credential Commented Dec 21, 2012 at 18:04
  • Can Send-MailMessage use SMTP servers external to the LAN the script runs on? i.e. a Smart Host Commented Dec 21, 2012 at 18:16
  • I'm pretty sure it can as long as you don't have some firewall between you and the server blocking port 25. Commented Dec 21, 2012 at 18:18

1 Answer 1

2

The approach you're taking appears correct assuming your username and password are correct. Is your username prefixed with the domain e.g. domain\username? I second the suggestion to try the command with a manually supplied credential via Get-Credential e.g.:

$cred = Get-Credential 

If you need to verify the password coming back from the secure string is correct, you can display it like so:

$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password) $str = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr) [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr) $str 
Sign up to request clarification or add additional context in comments.

4 Comments

I have tried with Get-Credential and have the same error - see my comment on the above post. Thanks :)
Try this - telnet mailserver.domain.com 25 and see if you can even reach the machine's SMTP port.
I can indeed, ehlo and all.
Are you sure your credentials give you permission to use the mail server?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.