cmail.exe -host:smtp.example.com -to:email@example.com -from:firstname.lastname@example.org "-subject:Simple test e-mail" "-body:This is just a test."
cmail.exe -host:smtp.example.com -to:email@example.com -from:firstname.lastname@example.org "-subject:The file we discussed" "-body:See attached" "-a:c:\file_path\file_name"
Using a text file as message body
cmail.exe -host:Bob:email@example.com -to:firstname.lastname@example.org -from:email@example.com "-subject:Authenticated message" "-body:This message was sent using the username 'Bob' and the password 'qwerty'."
To use the content of a text file as the message body, you can either pipe text into CMail or specify a file using the -body-file setting.
type file.txt | cmail.exe -host:smtp.example.com -to:firstname.lastname@example.org -from:email@example.com "-subject:Body from text file" --
or (CMail 0.7.7 or later)
Adding recipients from file (CMail 0.8.0 or later)
cmail.exe -host:smtp.example.com -to:firstname.lastname@example.org -from:email@example.com "-subject:Body from text file" -body-file:file.txt
The -to, -cc and -bcc settings can accept a file name, prefixed with '@'. The file should contain one entry per line, each consisting of an e-mail address or an alias with an e-mail address in angled brackets (the same format used in e-mail headers).
cmail.exe -host:smtp.example.com -to:@recipients.txt -from:firstname.lastname@example.org "-subject:Recipients from file"
Example recipient file.
Example User <email@example.com> firstname.lastname@example.org
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.
From 0.8.0 onwards, STARTTLS will be used automatically if authentication is not available prior to establishing a secure connection. The -starttls setting must still be used to ensure credentials are not sent over insecure connections if authentication is offered without TLS.
cmail.exe -host:Bob:email@example.com:587 -starttls -to:firstname.lastname@example.org -from:email@example.com "-subject:Secure message" "-body:This message was sent securely if the server supported STARTTLS."
This method is for servers requiring SMTPS. You should use the method 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.exe -host:Bob:firstname.lastname@example.org -secureport -to:email@example.com -from:firstname.lastname@example.org "-subject:Secure message" "-body:This message was sent securely."
With CMail version 0.8 and later, Unicode can be used in message subjects and bodies. These characters may not display correctly on the command line and may be rendered differently by various platforms. CMail configuration files are assumed to be UTF-8. The -clutf8 option may be required when calling CMail from another application.
Password encryption (CMail 0.8.0 or later)
cmail.exe -host:Bob:email@example.com -to:firstname.lastname@example.org -from:email@example.com "-subject:📊" "-body:💑💒🍆👶🏻 ✔️"
Running CMail with the -encryptpass option will generate an encrypted password which can be used in place of the plain text password in the -host setting. The password is protected using a static key by default, and can be bound to a specific Windows install using the -bindpass option. Encrypted passwords provide protection against casual observation only.
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 popd exit /b :send cmail.exe -host:smtp.example.com -to:firstname.lastname@example.org -from:email@example.com -config:attach.txt del attach.txt exit /b