Sending a simple e-mail

cmail -host:smtp.example.com -to:user1@example.com -from:user2@example.com "-subject:Simple test e-mail" "-body:This is just a test."

Sending attachments

cmail -host:smtp.example.com -to:user1@example.com -from:user2@example.com "-subject:The file we discussed" "-body:See attached" "-a:c:\file_path\file_name"

Authenticating with the server

cmail -host:Bob:qwerty@smtp.example.com -to:user1@example.com -from:user2@example.com "-subject:Authenticated message" "-body:This message was sent using the username 'Bob' and the password 'qwerty'."

Using a text file as the message body

To use the content of a text file as the message body, you can pipe it in to CMail, and instruct CMail to read the message body from stdin using the -- option. As of 0.7.7, to assist with cases where CMail is being called from other programmes, you can also use -body-file.

type file.txt | cmail -host:smtp.example.com -to:user1@example.com -from:user2@example.com "-subject:Body from text file" --

Using SSL/TLS, method 1

This method is for servers supporting STARTTLS. This is the method you should use to send e-mail via GMail. In this example, the -host option is specifying port 587, which some services require you to use for message submission. You can use -requiretls instead of -starttls to only send when encryption is possible, otherwise CMail will attempt to send messages even if encryption is not offered.

cmail -host:Bob:qwerty@smtp.example.com:587 -starttls -to:user1@example.com -from:user2@example.com "-subject:Secure message" "-body:This message was sent securely if the server supported STARTTLS."

Using SSL/TLS, method 2

This method is for servers requiring SMTPS. You should use method 1 (above) if supported, as SMTPS is considered deprecated. This method is the equivalent to using Blat with stunnel. Note, -secureport changes the default port to 465.

cmail -host:Bob:qwerty@smtp.example.com -secureport -to:user1@example.com -from:user2@example.com "-subject:Secure message" "-body:This message was sent securely."

A practical example

CMail can do a lot, but there's no such thing as a free lunch. The example below addresses the practical problem of sending a directory full of PDF files, while remaining under the default 10MB message limit imposed by many mail systems.

The script operates by constructing a supplementary configuration file for CMail (append.txt) containing a list of attachment commands, and keeping track of the total file size. When the next file would put the e-mail over the size limit, the e-mail is sent, and a new attachment list is started. There are a couple of key points to note here. Firstly, the size limit is set at 7,340,032 bytes, which is 7MB. Adding base64 encoding overhead, the resulting e-mail will safely remain under 10MB. The second consideration is that the attachments explicitly use base64 transfer encoding (-a64 setting). If CMail is asked to select the encoding to use (-a setting), any file that contains mostly text in the first 4KB would be quoted-printable encoded, which aside from being much less efficient for binary data, could put the e-mail over the maximum size.

Note, this script does not split files. Any PDF that is too large will be sent in its own e-mail, and would likely fail to send. If limits are to be exceeded, use of an archival tool such as 7-Zip is advised. This script is provided as-is. Use at your own risk.

setlocal enabledelayedexpansion
set MAXSZ=7340032
set SRC=C:\Some Directory

set SZ=0
pushd "%SRC%"
if exist attach.txt del attach.txt
for /f "delims=" %%a in ('dir /b /os *.pdf') do (
    set /a NEWSZ=!SZ!+%%~za
    if !NEWSZ! gtr %MAXSZ% (
        call :send
        set SZ=0
    echo a64:%%a>>attach.txt
    set /a SZ=!SZ!+%%~za
call :send
exit /b

cmail.exe -host:smtp.example.com -to:recipient@example.com -from:sender@example.com -config:attach.txt 
del attach.txt
exit /b