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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.