• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2409
  • Last Modified:

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
0
epmmis
Asked:
epmmis
  • 4
  • 3
  • 2
  • +1
1 Solution
 
footechCommented:
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

0
 
Michael Ian ClaridgeActing Service Delivery ManagerCommented:
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
0
 
epmmisAuthor Commented:
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.
0
Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

 
epmmisAuthor Commented:
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.
0
 
footechCommented:
Try on another machine.
0
 
Michael Ian ClaridgeActing Service Delivery ManagerCommented:
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
0
 
QlemoC++ DeveloperCommented:
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.
0
 
epmmisAuthor Commented:
I found the solution.

In the batch file I call the PS1 script this way.
  powershell.exe   -command " & {C:\scripts\SendEmail.ps1}"

The SendEmail.ps1 file
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 code above by itself does not send email
By appending this code to the SendEmail.ps1 file, the email is sent.

# If running in the console, wait for input before closing.
if ($Host.Name -eq "ConsoleHost")
{
      Write-Host "Press any key to continue..."
        $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null
}
0
 
epmmisAuthor Commented:
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.
0
 
QlemoC++ DeveloperCommented:
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".
0
 
QlemoC++ DeveloperCommented:
Objection: See my last comment. The question should either be deleted, or http:#a40400630 accepted.
0

Featured Post

Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now