How to Send Automated Emails via Command Line Using SwithMail
Automating email notifications is a core requirement for system administrators and developers. It allows scripts to send alerts when backups finish, servers fail, or scheduled tasks complete. While built-in tools like PowerShell can send emails, they often require complex authentication configurations. SwithMail is a lightweight, silent command-line utility for Windows that simplifies this process by sending emails via SMTP directly from the Command Prompt. Why Use SwithMail?
SwithMail stands out because it operates as a standalone executable (.exe) with no installation required.
Zero Installation: Run it directly from any folder or a USB drive.
Silent Execution: Suppresses all graphical interfaces for seamless background scripting.
SSL/TLS Support: Works securely with modern email providers like Gmail, Outlook, and Microsoft 365.
GUI Code Generator: Includes a built-in tool to visually generate your command-line arguments. Setting Up SwithMail
Before writing your first automated script, you need to download SwithMail and gather your SMTP credentials.
Download the latest version of SwithMail from its official repository or trusted source.
Extract the ZIP file to a permanent directory, such as C:\Scripts\SwithMail</code>.
Obtain your email provider’s SMTP settings (Server address, Port, Username, and Password).
Note: If you are using Gmail or Microsoft 365, you cannot use your regular account password. You must generate an App Password from your account security settings to bypass Multi-Factor Authentication (MFA). Generating Commands with the SwithMail GUI
SwithMail includes a graphical user interface designed specifically to help you build your command-line syntax.
Double-click SwithMail.exe to open the configuration window.
Fill in the SMTP Settings (Server, Port, Username, Password, and SSL/TLS preferences). Enter the Email Settings (To, From, Subject, and Body). Click the Generate CLI String button at the bottom. Copy the generated string to use in your scripts. Understanding the Command-Line Syntax
A typical SwithMail command looks long, but it follows a strict, logical structure of variables. Here is a baseline example:
SwithMail.exe /s /server ://example.com /p 587 /SSL /u [email protected] /pass MySecretPassword /from [email protected] /to [email protected] /sub “Backup Complete” /b “The daily backup executed successfully.” Use code with caution. Key Parameter Breakdown /s: Runs the program silently without showing a window.
/server: Specifies your email provider’s SMTP server address.
/p: Identifies the network port (usually 587 for TLS or 465 for SSL). /SSL: Enables secure encryption for the connection.
/u and /pass: Your SMTP authentication username and password. /from and /to: The sender and recipient email addresses. /sub and /b: The email subject line and body text. Advanced Automation Examples 1. Attaching Files to Emails
To attach log files, reports, or backup archives, append the /attachment parameter to your command string:
SwithMail.exe /s /server ://example.com /p 587 /SSL /u [email protected] /pass MySecretPassword /from [email protected] /to [email protected] /sub “Daily Report” /b “Please find the requested report attached.” /attachment “C:\Logs\daily_report.pdf” Use code with caution. 2. Using HTML Formatting
If your email body requires rich text, bolding, or tables, use the /html parameter. This tells SwithMail to render the body text as HTML code rather than plain text:
Alert
The server storage is 90% full.
” /html Use code with caution. Integrating SwithMail into Windows Batch Scripts
To fully automate this process, save your command inside a Windows Batch file (.bat). This allows you to combine SwithMail with other system commands.
Create a file named send_alert.bat and paste the following code:
@echo off echo Running database backup… :: [Your backup commands here] echo Sending email notification… “C:\Scripts\SwithMail\SwithMail.exe” /s /server ://example.com /p 587 /SSL /u [email protected] /pass MySecretPassword /from [email protected] /to [email protected] /sub “Task Scheduler Success” /b “The automated batch script finished processing at %TIME%.” Use code with caution. Scheduling the Email Automation
To make the script run completely unattended, hook your batch file up to the Windows Task Scheduler: Open Task Scheduler from the Windows Start Menu. Click Create Basic Task in the right-hand Actions pane.
Set your desired Trigger (e.g., Daily at 2:00 AM, or when a specific system event occurs). Set the Action to Start a program. Browse and select your send_alert.bat file.
Check the box to “Run whether user is logged on or not” in the task properties if you want it to execute invisibly in the background. Troubleshooting Common Errors
If your emails are not arriving, temporarily remove the /s (silent) switch from your command line. Running the command without /s forces SwithMail to display an on-screen error log.
Authentication Failed: Double-check your username and password. Ensure you are using an App Password if your provider uses Two-Factor Authentication.
Connection Timed Out: Ensure your firewall is not blocking outbound traffic on ports 587 or 465. Verify that the SMTP server address is correct.
Attachment Not Found: Ensure you use absolute file paths (e.g., C:\Folder\file.txt) rather than relative file paths (e.g., .\file.txt) when calling the script from Task Scheduler.
By combining SwithMail’s straightforward syntax with Windows Task Scheduler, you can establish a reliable, zero-overhead monitoring and alert infrastructure for any environment.
To help refine this setup for your environment, please let me know:
Which email provider are you planning to use? (Gmail, Outlook, custom SMTP?)