How can I send an email using Powershell?

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.
100questionsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mohammed KhawajaManager - Infrastructure:  Information TechnologyCommented:
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
Joe KlimisCommented:
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
Joe KlimisCommented:
which version of Powershell are you running ? as send-mailmessage has only been available from Powershell V3 onwards.
0
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

Ganesh Kumar ASr Infrastructure SpecialistCommented:
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
100questionsAuthor Commented:
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
100questionsAuthor Commented:
@GaneshKumar -  Thanks, I will try this.  Is this a Powershell script?
0
Joe KlimisCommented:
yes this is a Powershell script
0
100questionsAuthor Commented:
@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
Joe KlimisCommented:
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
100questionsAuthor Commented:
@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
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
100questionsAuthor Commented:
@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
100questionsAuthor Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
it worked for me, as said, with Exchange and Outlook. The HTML mail approach however is more reliable.
0
100questionsAuthor Commented:
Works for me.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.