?
Solved

How can I send an email using Powershell?

Posted on 2014-10-08
15
Medium Priority
?
203 Views
Last Modified: 2014-10-14
I would like to send an automated email using Powershell, in an Exchange environment, in a network setting.
The email needs to look at a text file and insert the data inside the text file in the body of the email, without attaching the text file.

A sample script would be useful.
0
Comment
Question by:100questions
[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
  • 7
  • 4
  • 2
  • +2
15 Comments
 
LVL 25

Expert Comment

by:Mohammed Khawaja
ID: 40368420
You could use send-mailmessage cmdlet.  Below is an example:

send-mailmessage -from "UserA <userA@mydomain.com>" -to "UserB <userB@mydomain.com.com>"" -subject "Requested data with attachment" -body "As promised, included is the attachment." -Attachments "file1.csv" -priority High -dno onSuccess, onFailure -smtpServer smtp.mydomain.com

Also look at this technote document for more info:

http://technet.microsoft.com/en-us/library/hh849925.aspx
0
 
LVL 11

Expert Comment

by:Joe Klimis
ID: 40368427
Hi

have a look at this site many good examples

https://4sysops.com/archives/send-mailmessage-paramaters-and-examples/

you will need to enter to exchange server in as the smtp server.  You also may need to add a rule in exchange to allow relaying from the sending host.
let me know how you get on.

Regards
Joe
0
 
LVL 11

Expert Comment

by:Joe Klimis
ID: 40368431
which version of Powershell are you running ? as send-mailmessage has only been available from Powershell V3 onwards.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 12

Expert Comment

by:Ganesh Kumar A
ID: 40368471
Here is the sample script you can help for good start, but later you need to modify or enhance whenever required..

#READ LINE & WRITE AS INPUT FROM TEXT FILE TO MAIL BODY
# ALL CAPS are variable which needs to be replaced with your mail server info and email id and subject.

$smtpServer = "YOUR_MAIL_SERVER_NAME"
$smtpFrom = "FROM_EMAIL_ID"
$smtpTo = "TO_EMAIL_ID"
$messageSubject = "YOUR_MAIL_SUBJECT"

[string]$messagebody = ""

$txtinput = Get-Content "C:\test.txt"

foreach ($lineinputfile in $txtinput)
{

      $messagebody = $messagebody + $lineinputfile + "`r`n"

}

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)
0
 

Author Comment

by:100questions
ID: 40368501
Thanks to all.  This seems to work - however what I need to do is copy the contents of a text file into the body of the email.

This is the script I have so far which works however I need to copy the contents of a text file into the body of the attachment, not attach the text file to the email.   The text file must not be attached to the email.

send-mailmessage -from "Name <email@email.com>" -to "Name <email@email.com>" -subject "Requested data with attachment" -body "As promised, included is the attachment." -priority High -dno onSuccess, onFailure -smtpServer info.domain.local
0
 

Author Comment

by:100questions
ID: 40368513
@GaneshKumar -  Thanks, I will try this.  Is this a Powershell script?
0
 
LVL 11

Expert Comment

by:Joe Klimis
ID: 40368535
yes this is a Powershell script
0
 

Author Comment

by:100questions
ID: 40368584
@GaneshKumar - Thanks, this works, however it does not respect the spacing in the original text file when it copies the contents of the data into the body of the email.

For instance this is the data in the source text file.

PO Number: 1234567894
Item 1: UPC: 12345678912341, Desc: Desc 1, Qty: 10, Price: $5.00
Item 2: UPC: 56456465465465, Desc: Desc 2, Qty: 1, Price: $10
Item 3: UPC: 45646464646555, Desc: Desc 3, Qty: 2, Price: $22.00
Item 4: UPC: 45646545646545, Desc: Desc 4, Qty: 4, Price: $10
Item 5: UPC: 15615656156161, Desc: Desc 5, Qty: 5, Price: $12.00
Item 6: UPC: 54654656465465, Desc: Desc 6, Qty: 5, Price: $4.00
Item 7: UPC: 45646645464646, Desc: Desc 7, Qty: 1, Price: $2
Total: $100.00

But inside the body of the email it's not shown orderly as it's shown above..

It does not respect the break at each line.. it bunches data together.
When each line ends, then it needs to break to a new line, or else the data is not being properly imported into the body.

Any modifications to the script?
0
 
LVL 11

Expert Comment

by:Joe Klimis
ID: 40368653
try this
#READ LINE & WRITE AS INPUT FROM TEXT FILE TO MAIL BODY
# ALL CAPS are variable which needs to be replaced with your mail server info and email id and subject.

$smtpServer = "YOUR_MAIL_SERVER_NAME"
$smtpFrom = "FROM_EMAIL_ID"
$smtpTo = "TO_EMAIL_ID"
$messageSubject = "YOUR_MAIL_SUBJECT"

$messagebody= (Get-Content C:\test.txt | out-string )
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)

Open in new window

0
 

Author Comment

by:100questions
ID: 40368674
@JoeKlimis - thanks Joe, however it only respects the break at the first line and the last line, the rest is attached together and not properly outputting correctly.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40369656
Joe, I know the online pages tell different, but Send-MailMessage is available since v2 (see for example http://theucguy.net/send-emails-easily-using-powershell-v2/), and I've used and suggested it since.

100questions, something like this worked fine for me:
Send-MailMessage -SmtpServer mail.there.com -From me@here.com -To you@there.com -Subject Test -Body ((get-content file.txt) -join "`r`n")

Open in new window

0
 

Author Comment

by:100questions
ID: 40370936
@Olemo -  Thanks however this does not work well at all, since it does not respect the separate lines, again it bunches the 2nd and penultim line together incorrectly.
0
 

Accepted Solution

by:
100questions earned 0 total points
ID: 40370950
This solution works for me:

$Data = Get-Content "C:\Users\user\data\Text.txt"
 
  Send-MailMessage -From 'email@email.com' -To 'email@email.com','email@email.com' `
  -SmtpServer 'servername.local' `
  -Subject 'Information needed - test ' `
  -BodyAsHtml `
  -Body ($Data -join "<br>")
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40371290
it worked for me, as said, with Exchange and Outlook. The HTML mail approach however is more reliable.
0
 

Author Closing Comment

by:100questions
ID: 40379401
Works for me.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

771 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