email - In PowerShell, is there a way to dispose of Send-MailMessage resources? -


i've written powershell script sends email message. used send-mailmessage commandlet.

send-mailmessage -smtpserver $mailserver ` -to $mailto ` -from $mailfrom ` -subject $mailsubject ` -body $mailbody 

this concise. if execute script rapidly in succession on workstation, following error appears in powershell console.

unable read data transport connection: established connection aborted software in host machine.

what suspect resources aren't being released or thread getting blocked. below current workaround, has advantage of being disposable. , can run in rapid succession no transport connection errors. more verbose send-mailmessage.

[object]$smtpclient = new-object system.net.mail.smtpclient [object]$mailmessage = new-object system.net.mail.mailmessage $smtpclient.host = $mailserver $mailmessage.to.add($mailto) $mailmessage.from = $mailfrom $mailmessage.subject = $mailsubject $mailmessage.body = $mailbody $smtpclient.send($mailmessage) $mailmessage.dispose() $smtpclient.dispose() 

is there way force send-mailmessage release resources when i'm done it, perhaps via dispose or c# style using statement? thanks.

frankly, "it works it's verbose" shouldn't huge concern, when "verbose" means 10 lines. , mean, can simplify syntax using class constructors:

$smtpclient = new-object -typename system.net.mail.smtpclient -argumentlist $mailserver $mailmessage = new-object -typename system.net.mail.mailmessage -argumentlist $mailfrom, $mailto, $mailsubject, $mailbody $smtpclient.send($mailmessage) $mailmessage.dispose() $smtpclient.dispose() 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -