# 解压到C:\blat目录
mkdir C:\blat
# 将blat.exe和相关文件复制到C:\blat
# 注册BLAT(可选,简化后续使用)
blat -install smtp.example.com sender@example.com [sender_name] [port] [profile_name]
blat -to recipient@example.com -subject "测试邮件" -body "这是邮件内容" -server smtp.example.com -f sender@example.com
blat -to recipient@example.com -subject "带附件的邮件" -body "请查看附件" -attach "C:\files\document.pdf" -server smtp.example.com -f sender@example.com
blat -to recipient@example.com -subject "HTML邮件" -body "<h1>标题</h1><p>HTML内容</p>" -html -server smtp.example.com -f sender@example.com
blat ^
-to "user1@example.com;user2@example.com" ^
-cc "cc@example.com" ^
-bcc "bcc@example.com" ^
-subject "服务器报告" ^
-body "服务器运行正常" ^
-server "smtp.office365.com" ^
-port 587 ^
-u "username@example.com" ^
-pw "yourpassword" ^
-f "sender@example.com" ^
-charset UTF-8 ^
-log "C:\logs\mail.log" ^
-timestamp
@echo off
set SERVER=smtp.office365.com
set PORT=587
set FROM=sender@example.com
set USER=username@example.com
set PASSWORD=yourpassword
blat ^
-to "admin@example.com" ^
-subject "每日备份报告 - %date%" ^
-body "备份任务已完成。^&echo.^&详细信息请查看日志。" ^
-attach "C:\backup\backup_log.txt" ^
-server %SERVER% ^
-port %PORT% ^
-u %USER% ^
-pw %PASSWORD% ^
-f %FROM% ^
-log "C:\logs\mail_send_%date:~0,4%%date:~5,2%%date:~8,2%.log"
$subject = "服务器监控报告"
$body = Get-Content "C:\reports\daily_report.txt" | Out-String
$recipients = "admin1@example.com", "admin2@example.com"
& C:\blat\blat.exe `
-to ($recipients -join ";") `
-subject $subject `
-body $body `
-server "smtp.example.com" `
-f "noreply@example.com" `
-u "username" `
-pw "password" `
-port 587
-server smtp.office365.com
-port 587
-u your-email@example.com
-pw your-password
-server smtp.gmail.com
-port 587
-u username@gmail.com
-pw app-specific-password
-server mail.example.com
-port 25
-f sender@example.com
blat -saveSettings profileName -server smtp.example.com -f sender@example.com -u username -pw password -port 587
blat -profile profileName -to recipient@example.com -subject "使用配置" -body "邮件内容"
blat -to recipient@example.com -subject "测试" -body "内容" -server smtp.example.com -debug -log C:\logs\blat.log
blat -testSMTP smtp.example.com 25
不要硬编码密码
# 使用环境变量
set MAIL_PASSWORD=yourpassword
blat ... -pw %MAIL_PASSWORD%
# 或从文件读取
for /f "delims=" %%i in ('type C:\secure\password.txt') do set PASSWORD=%%i
使用加密连接
-port 587
-tryTLS
限制权限
认证失败
连接被拒绝
中文乱码
-charset GB2312 # 简体中文
-charset BIG5 # 繁体中文
-charset UTF-8 # 通用编码
这种方法适用于Windows Server的各种自动化任务,如备份通知、监控报警、定期报告等。