11

I have a Windows 2003 R2 server and I want to send an email from the command line. This server does not have the SMTP service configured. Is there a one liner that will let me send an email? My specific use case at the moment is to send an email when a performance alert is triggered, but it would be useful in general.

I'm hoping for something like

foomail -t peter@example.org -f blah@example.org -m "Alert!  the sky is falling"

Update: I would much prefer a solution that does not involve installing 3rd party software.

Peter
  • 406
  • 1
  • 5
  • 13
  • Utilities like blat and sendemail don't require installation. They're both freestanding exe files. – John Gardeniers Feb 05 '10 at 22:26
  • I guess I have to accept that I need to at the very least copy an exe (either blat or powershell) to each machine. If that's the case, then blat is very easy to use. Thanks. I still wonder why MS left this feature out, considering how many other less useful ones they put in to the standard distro. – Peter Feb 08 '10 at 17:55

6 Answers6

15

I'd try blat. You could write a vbscript but there is no built in executable to send mail

Jim B
  • 24,081
  • 4
  • 36
  • 60
  • seconding blat. It can be a bit picky about passing parameters directly from the command line, but once you nail it, does exactly what the OP needs. – Chris Thorpe Feb 05 '10 at 23:51
  • I've been using blat for years with great success. However, If I was writing in powershell, I'd switch. – uSlackr Feb 08 '10 at 19:29
  • Does blat able to work as send mail path for PHP on Windows? – SaidbakR Aug 14 '14 at 22:02
14

Would you consider powershell rather than cmd.exe? If so, sending mail is built in:

$SmtpClient = New-Object System.Net.Mail.SmtpClient
$SmtpServer = "your.mail.host.com"
$SmtpClient.host = $SmtpServer 

$From = "Me <User@example.com>"
$To = User2@example.com
$Title = "Subject"
$Body = "Body Text" 
$SmtpClient.Send($From,$To,$Title,$Body)  

To make a one liner, save the following to a powershell script file (sendmail.ps1):

   param(  
        [string] $From = "from@example.com",
        [string] $To = "to@example.com",
        [string] $Title = "title",
        [string] $Body = "body"
    )
    $SmtpClient = New-Object System.Net.Mail.SmtpClient
    $SmtpServer = "your.mail.host.com"
    $SmtpClient.host = $SmtpServer 
    $SmtpClient.Send($From,$To,$Title,$Body)

(make sure to change the smtpserver to be your real one)

Then you can call it using:

powershell.exe c:\path\to\sendmail.ps1 "from@example.com" "to@example.com" "title" "body"
MattB
  • 11,194
  • 1
  • 30
  • 36
6

I've used bmail with great success in the past.

Usage (copied from website)

C:\>bmail /?

    Command Line SMTP Emailer V1.07
    Copyright(C) 2002-2004 Craig.Peacock@beyondlogic.org
    Usage: bmail [options]
            -s    SMTP Server Name
            -p    SMTP Port Number (optional, defaults to 25)
            -t    To: Address
            -f    From: Address
            -b    Text Body of Message (optional)
            -h    Generate Headers
            -a    Subject (optional)
            -m    Filename (optional) Use file as Body of Message
                -c    Prefix above file with CR/LF to separate body from header
                -d    Debug (Show all mail server communications)
Zypher
  • 37,405
  • 5
  • 53
  • 95
  • 1
    I have to admit, I was hoping for a built in solution, though Jim's answer seems to imply that was a bit naive of me... – Peter Feb 05 '10 at 18:49
2

Try free Mail Alert Simple Mailer: https://sourceforge.net/projects/mail-alert/

It supports SSL/TLS mail servers like gmail and its' easy to configure.

Admin
  • 21
  • 1
0

One more command-line mailer program:

It supports SSL too.

Roland Pihlakas
  • 271
  • 3
  • 7
0
sendEmail -f %from_addr% -t %to_addr% -u "Subject Line" -m "Message" -s %smtp_server%

The simplest Win utility I found and used in production environment. Standalone executable, without installation or mandated configuration. Supports both non-TLS and TLS. And has a debugger, which is sometimes helpful.

Executable can found here:

http://caspian.dotconf.net/menu/Software/SendEmail/#download