Solved

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

Posted on 2014-10-16
12
1,302 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
  • 4
  • 3
  • 2
  • +1
12 Comments
 
LVL 39

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
 

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 39

Expert Comment

by:footech
ID: 40386081
Try on another machine.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 68

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 68

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 68

Expert Comment

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Import PST to Exchange using Power Shell new-mailboximportrequest command, you can simply import the PST file into Exchange mailbox or archived. To know How to import PST into Exchange  2013 read the complete article.
Marketers need statistics and metrics like everybody else needs oxygen. In this article we explain how to enable marketing campaign statistics for Microsoft Exchange mail.
In this video we show how to create a mailbox database in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Servers >> Data…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now