简体   繁体   中英

Trouble sending email using Powershell

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!

您的防病毒文件服务可能正在锁定邮件传递。

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

$emailFrom = "AUTOMATED_PRERUN@somehost.com"
$emailTo = "somebody@somehost.com"
$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:

 http://support.microsoft.com/kb/153119

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM