Sending email from a PC error

Hi,

I was trying to send emails through powershell on a computer PC01. and received error message blow
"InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException"

I also tried to run the same script on another computer PC02 in the same subnet. it worked.

Both PCs can telnet to the smtp server on 25 successfully.

I have also tried to use ISP smtp to send email which was the same result PC01 didn't work with the same error. PC02 worked.
I checked the even log, no outstanding errors or warnings found.
I have also tried to turned off firewall and antivirus program without luck (which shouldn't be the issue as I can telnet to port 25 on the smtp server)
jzrobbieAsked:
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.

Sunil ChauhanLead AdministratorCommented:
have you come across this? same issue.

https://social.technet.microsoft.com/Forums/ie/en-US/7242541e-5722-4064-86bf-a108daf8711a/sendmail-powershell-issues?forum=winserverpowershell

also, can you test if you are able to submit the email using telnet...

telnet smtp.domain.com 25
ehlo mydomain.com
mail from:testu@mydomain.com
rcpt to:recipient@theirdomain.com
data
This is a test.
.
quit

https://technet.microsoft.com/en-us/library/aa995718(v=exchg.65).aspx
ste5anSenior DeveloperCommented:
hmm, use better exception handling and output the exception message and when available also the inner exceptions. Something like

try {
    #Send e-mail code..
}
catch {
    Write-Host $_.Exception.Message
    if ($_.Exception.InnerException) {
        Write-Host $_.Exception.InnerException.Message
    }
}

Open in new window

jzrobbieAuthor Commented:
Hi ste5an,

I have tried your code. And received similar error
+ CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
   ion
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
ste5anSenior DeveloperCommented:
Some kind SMTP of setup error. First thing after rereading:

PORT 25???? This should be closed. And you should use 587/TCP (Message Submission for Mail), cause you're acting as MUA.

Then I would use a network sniffer to analyze, what is happening.
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
Powershell

From novice to tech pro — start learning today.