50

We have a script to backup files. After the backup operation is over, we would like to send a report as an email notification to some of our email addresses.

How could this be done?

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
user73628
  • 3,715
  • 5
  • 29
  • 24

6 Answers6

38

Blat:

blat -to user@example.com -server smtp.example.com -f batch_script@example.com -subject "subject" -body "body"
Colin Pickard
  • 45,724
  • 13
  • 98
  • 148
  • if you want to send emails to multiple addresses then use command like this blat -to "user1@example.com","user2@example.com" – Sam B Aug 10 '20 at 14:59
21

You can also use a Power Shell script:

$smtp = new-object Net.Mail.SmtpClient("mail.example.com")

if( $Env:SmtpUseCredentials -eq "true" ) {
    $credentials = new-object Net.NetworkCredential("username","password")
    $smtp.Credentials = $credentials
}
$objMailMessage = New-Object System.Net.Mail.MailMessage
$objMailMessage.From = "script@mycompany.com"
$objMailMessage.To.Add("you@yourcompany.com")
$objMailMessage.Subject = "eMail subject Notification"
$objMailMessage.Body = "Hello world!"

$smtp.send($objMailMessage)
Philibert Perusse
  • 4,026
  • 5
  • 24
  • 26
15

PowerShell comes with a built in command for it. So running directly from a .bat file:

powershell -ExecutionPolicy ByPass -Command Send-MailMessage ^
    -SmtpServer server.address.name ^
    -To someone@what.ever ^
    -From noreply@possibly.fake ^
    -Subject Testing ^
    -Body 123

NB -ExecutionPolicy ByPass is only needed if you haven't set up permissions for running PS from CMD

Also for those looking to call it from within powershell, drop everything before -Command [inclusive], and ` will be your escape character (not ^)

Hashbrown
  • 12,091
  • 8
  • 72
  • 95
  • As a side note don't forget that when you run something like `powershell -Command Send-MailMessage...` it needs to be ran with surrounding double quotes and single quotes such as `powershell -Command "Send-MailMessage -SmtpServer 'server.address.name' -To 'someone@what.ever' -From 'noreply@possibly.fake' -Subject 'This is a test' -Body 'BLAH BLAH BLAH'"` – Arvo Bowen Mar 21 '18 at 20:52
  • the above worked fine for me, does yours not work without them? Is it because you use have spaces in the subject and require single quotes? – Hashbrown Mar 23 '18 at 07:18
  • 1
    Possibly, but I could not get it to work without the double quotes for me. I ran across the suggestion from others experiencing the same problem. Figured I would share incase someone else hit the same wall. Even so usually all my Subjects and Bodies in my emails contain spaces. ;) - Side note, maybe it has to do with the fact my command is all on one line as opposed to yours the is multiline? – Arvo Bowen Mar 23 '18 at 13:38
8

bmail. Just install the EXE and run a line like this:

bmail -s myMailServer -f Sender@foo.com -t receiver@foo.com -a "Production Release Performed"
aseques
  • 537
  • 4
  • 21
RossFabricant
  • 12,364
  • 3
  • 41
  • 50
  • Here I cant install bamil as per security purpose. plz tell me is there any alernate way to solve my problem. – user73628 Apr 02 '09 at 13:40
  • Have you tried with other command-line email clients? A Google search lists quite many freely available tools. – Dirk Vollmar Apr 02 '09 at 13:43
3

We use blat to do this all the time in our environment. I use it as well to connect to Gmail with Stunnel. Here's the params to send a file

blat -to user@example.com -server smtp.example.com -f batch_script@example.com -subject "subject" -body "body" -attach c:\temp\file.txt

Or you can put that file in as the body

blat c:\temp\file.txt -to user@example.com -server smtp.example.com -f batch_script@example.com -subject "subject"
Keng
  • 52,011
  • 32
  • 81
  • 111
1

There are multiple methods for handling this problem.

My advice is to use the powerful Windows freeware console application SendEmail.

sendEmail.exe -f sender.from@mail.com -o message-file=body.txt -u subject message -t to.email.address@mail.com -a attachment.zip -s smtp.gmail.com:446 -xu gmail.login -xp gmail.password
Mofi
  • 46,139
  • 17
  • 80
  • 143