Link to home
Start Free TrialLog in
Avatar of epmmis
epmmis

asked on

Sending email using powershell Send-MailMessage in a batch file.

If power shell is first started; email can be successfully sent using the command line.

PS C:\> Send-MailMessage -SmtpServer 192.168.15.12 -To TestEmail@localdomain.com -From System@localdomain.com -Subject 'Sent from Inside Power Shell App'  -Body 'This is the body of the message'

From a batch file or the command line this command fails to send email.  There are no errors.  The command runs and then stops

powershell  Send-MailMessage -SmtpServer 192.168.15.12 -To TestEmail@localdomain.com -From System@localdomain.com -Subject 'Sent from Inside Power Shell App'  -Body 'This is the body of the message'

I know both versions connect to the mail server.  If the to or from address is intentionally misspelled the mail server correctly reports the error.  

Why does email succeed when it is sent from within PowerShell, but fails when sent from the command line or batch file?

Platform:  Windows 2008 R2 server
Powershell version:  2
Mail Server:  MDaemon made by Alt-N
Avatar of footech
footech
Flag of United States of America image

I don't have an answer for you, but just tested from a cmd.exe window and it worked just fine.
You might also try
powershell -command "& {Send-MailMessage -SmtpServer 192.168.15.12 -To TestEmail@localdomain.com -From System@localdomain.com -Subject 'Sent from Inside Power Shell App'  -Body 'This is the body of the message'}"

Open in new window

Avatar of Member_2_4839798
Member_2_4839798

Hello,

I would imagine that this is because when these commands are ran they are not within a PowerShell environment. Using parameters will work, -Command being a parameter this will accept the code block and pass it to a powershell environment.

Another way is to call your PowerShell Scripts from within a batch file. (PowerShell.exe C:\Scripts\MyScript.ps1 - As an example).

I hope this helps.

Regards

Michael
Avatar of epmmis

ASKER

Michael
Changed the batch file to call the PS script.

Batch File
powershell.exe C:\scripts\SendEmail.ps1

SendEmail.ps1
Send-MailMessage -SmtpServer  192.168.15.12 -To TestEmail@localdomain.com -From System@localdomain.com -Subject 'Sent from Inside Power Shell App'  -Body 'This is the body of the message'

The email did not sent.

SendEmail.ps1 was manually ran by right mouse clicking on the file and then selected "Run With PowerShell"
Still no email.

Again, Email was successfully sent by copying/pasting the script from SendEmail.ps1 into a PS window.
Avatar of epmmis

ASKER

FooTech

Changed the batch file to this format.

powershell -command "& {Send-MailMessage -SmtpServer  192.168.15.12 -To TestEmail@localdomain.com -From System@localdomain.com -Subject 'Sent from Inside Power Shell App'  -Body 'This is the body of the message'}"

No Email using this format.
Try on another machine.
Hello,

If you run the script manually and it doesn't work there is a problem with your code, there is no reason that I can see if the script is correct why it cannot be called from a batch file. I have needed to do this many times before, which have always succeeded.

I suggest you review the code in your script and try again, run the script manually until the email sends, then call it from a batch file using my suggested method.

If you are unsure if the contents of your script you are calling is correct, please post this so I or another expert can review this for you :)

In addition, you will also need to ensure that the machine you are relaying email from is allowed to do so to the send connector on the target email server.

All the best.

Michael
I cannot see any issue here either. Are you able to trace what is received by the SMTP server?
Is what you showed the exact command line?
Since you verified that To, From and SmtpServer are all evaluated correctly, the reason has to be in the Subject or Body line.
ASKER CERTIFIED SOLUTION
Avatar of epmmis
epmmis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of epmmis

ASKER

I've requested that this question be closed as follows:

Accepted answer: 0 points for epmmis's comment #a40400630
Assisted answer: 200 points for footech's comment #a40386081
Assisted answer: 150 points for MichaelIanClaridge's comment #a40387466
Assisted answer: 150 points for Qlemo's comment #a40390995

for the following reason:

It was the only thing which made the email go.
That does not make sense for me. It suggests there is a timing issue.

If you have a PS script to call, you do that with
powershell.exe   -file C:\scripts\SendEmail.ps1

Open in new window

I guess the closing double quote in the SendEmail.ps1 file (without the wait for key stuff) is a typo, because it should be single quote ...

Further, I  will have to object. Fine that you want to provide points, but there is nothing in our comments helping with your "solution".
Objection: See my last comment. The question should either be deleted, or http:#a40400630 accepted.