Sending an email with attached file Through powershell

I Am trying to use a ready script to send an email with a file that took from here:

but when I run the script I get an error:
Exception calling "Send" with "1" argument(s): "Failure sending mail."
At 131 char:11
+ $smtp.send <<<< ($msg)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

My question is which lines do i need to change do to fix this error?

Who is Participating?
footechConnect With a Mentor Commented:
There's nothing wrong with the script itself.  The error likely has something to do with your email server config, or the parameters passed to the sendEmail function.  You can get that error if you specified the incorrect name for your email server (in the script it is "hub1", but yours is likely different).  I haven't tested but you might also get that message if your server isn't configured to accept connections from your machine, or you need to provide credentials, or connect to a different port, etc.
solutech06Author Commented:
Thanks, it was "hub1"

Now i have another problem "Unable to connect to the remote server"
which i think related to the credentials.
what do i need to add to the script and where exactly?
Insert the following line right above "$smtp.send($msg)"
$smtp.Credentials = New-Object System.Net.NetworkCredential("username","password")

Open in new window

But I think you get a different error message when the credentials are the problem.  Make sure the port is correct and that the firewall isn't blocking the outbound traffic.  To change the port you can use something like below.
$smtp.Port = 587

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.