一、安装和配置BLAT
1. 下载BLAT
- 从官方源下载:http://www.blat.net
- 或直接下载:http://www.blat.net/~fullblat/2505/fullblat-2505.win32.zip
2. 安装BLAT
# 解压到C:\blat目录
mkdir C:\blat
# 将blat.exe和相关文件复制到C:\blat
# 注册BLAT(可选,简化后续使用)
blat -install smtp.example.com sender@example.com [sender_name] [port] [profile_name]
二、基本使用方法
1. 发送简单邮件
blat -to recipient@example.com -subject "测试邮件" -body "这是邮件内容" -server smtp.example.com -f sender@example.com
2. 发送带附件的邮件
blat -to recipient@example.com -subject "带附件的邮件" -body "请查看附件" -attach "C:\files\document.pdf" -server smtp.example.com -f sender@example.com
3. 发送HTML格式邮件
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
四、批量发送脚本示例
batch脚本示例
@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"
PowerShell结合BLAT
$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
五、常见SMTP服务器配置
Office 365 / Exchange Online
-server smtp.office365.com
-port 587
-u your-email@example.com
-pw your-password
Gmail SMTP
-server smtp.gmail.com
-port 587
-u username@gmail.com
-pw app-specific-password
本地Exchange服务器
-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
测试SMTP连接
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
使用加密连接
- 如果SMTP服务器支持,使用TLS/SSL
-port 587
-tryTLS
限制权限
九、常见问题解决
认证失败
- 确认用户名密码正确
- 检查是否启用SMTP认证
- Office 365可能需要启用"应用密码"
连接被拒绝
- 检查防火墙设置
- 确认SMTP服务器和端口正确
- 验证网络连接
中文乱码
-charset GB2312 # 简体中文
-charset BIG5 # 繁体中文
-charset UTF-8 # 通用编码
这种方法适用于Windows Server的各种自动化任务,如备份通知、监控报警、定期报告等。