Solved

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

Posted on 2014-10-16
12
1,680 Views
Last Modified: 2014-11-04
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
Comment
Question by:epmmis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
12 Comments
 
LVL 40

Expert Comment

by:footech
ID: 40385629
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
 
LVL 10

Expert Comment

by:Michael Ian Claridge
ID: 40385758
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
 

Author Comment

by:epmmis
ID: 40386033
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

Author Comment

by:epmmis
ID: 40386035
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
 
LVL 40

Expert Comment

by:footech
ID: 40386081
Try on another machine.
0
 
LVL 10

Expert Comment

by:Michael Ian Claridge
ID: 40387466
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
 
LVL 70

Expert Comment

by:Qlemo
ID: 40390995
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
 

Accepted Solution

by:
epmmis earned 0 total points
ID: 40400630
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
 

Author Comment

by:epmmis
ID: 40406100
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
 
LVL 70

Expert Comment

by:Qlemo
ID: 40400664
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
 
LVL 70

Expert Comment

by:Qlemo
ID: 40406101
Objection: See my last comment. The question should either be deleted, or http:#a40400630 accepted.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

742 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question