0

The following Powershell script works on my Windows Server 2012:

send-mailmessage -from "from@domain.com" -to "to@domain.com" -Subject "test" -Body "test body" -smtpserver myhostip -credential "mydomain\myuser"

When I execute it, a window appears that prompts for the password of the specified user. I enter the password and the e-mail is sent.

However, the following script does not work:

$smtp = New-Object Net.Mail.SmtpClient("myhostip")
$smtp.usedefaultcredentials = $false
$smtp.enablessl = $false
$smtp.credentials = new-object net.networkcredential("myuser", "mypassword", "mydomain")
$smtp.send("from@domain.com", "to@domain.com", "test", "test body")

It results in the error message

Exception calling "Send" with "4" argument(s): "Mailbox unavailable. The server response was: 5.7.1
<to@domain.com>... Relaying denied. IP name lookup failed [ip-address-of-my-web-server]"

What's the difference? How can I get the second script to work?

Jonas Sourlier
  • 13,684
  • 16
  • 77
  • 148

1 Answers1

1

did you try turning it all into variables and plugging the variables into the first script, wouldn't that work?

Luke
  • 647
  • 1
  • 8
  • 22