0

I've run the following script:

PS C:\> Send-MailMessage -To <EmailAddress1> -From <EmailAddress2> -Body "This is a test" -Subject "TEST MAIL" -SmtpServer <INTERNAL IP OF SMTP SERVER>

And I receive the following error:

Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed.

At line:1 char:17
+ Send-MailMessage <<<<  -To <EmailAddress1> -From <EmailAddress2> -Body "This is a test" -Subject "TEST MAIL" -SmtpServer <INTERNAL IP OF SMTP SERVER>
+ CategoryInfo: InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-Mail Message], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage`

I was made aware of the fact that I need permissions to send email from my local machine through the SMTP server, and as far as I know, I've been granted those rights.

Would somebody please help point me in the right direction on this one?

The ultimate goal is to be able to send emails as part of some Powershell scripts.

Thanks!

5
  • some info : social.technet.microsoft.com/Forums/hu/winserverpowershell/… Commented Mar 21, 2012 at 15:23
  • Sorry, YES the SMTP is an exchange server. Commented Mar 21, 2012 at 15:26
  • 2
    I think it's time to go talk to your Exchange admin. Commented Mar 21, 2012 at 15:29
  • Thanks for the help guys. I searched the web for that error message, and came up dry (there were results but they were unclear).Just thought I'd ask here. Thanks again. Commented Mar 21, 2012 at 15:40
  • 1
    I found this link and it works for my case stackoverflow.com/questions/9807359/… Commented Nov 12, 2013 at 21:43

2 Answers 2

3

I prefer the Net.Mail.SmtpClient method of sending email. This script would send the contents of a file passed as a parameter.

$emailFrom = "[email protected]"
$emailTo = "[email protected]"
$subject = "TEST"
get-content $Args[0] | %{$Body+= " {0} `n" -f $_}
$smtpServer = "mailserver.somehost.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)

Though, your error sounds more like a networking issue of some type.

You might want to check that you can reach the SMTP port on the server:

https://learn.microsoft.com/en-us/Exchange/mail-flow/test-smtp-with-telnet

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help. I will have to wait for the admin to be available. I tried to telnet to the server; couldn't connect. I'm guessing there's some permission that isn't correctly set. Once I get this working, I will look into using your method, although your 4th line of code is a little unclear to me. I'm accepting your answer. Thanks again!
In the 4th line of code, I am dumping the file contents into the body of the email. $Args[0] contains the file name I passed into the script.
As the error message from Send-MailMessage shows, it too "just" uses System.Net.Mail.SmtpClient to do its work.
2

Probably your antivirus file service is locking mail delivery.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.