背景:
- 工作中有一个业务需要每个月25号发送一个下月使用的文件给相关人员。
- 在此之前是用QQ邮箱的定时发送功能,先一次性地编缉好一年12个月的,到今年是第3年了。
- QQ定时发送的邮件还不能太多,好象最大只能30、40封。
- 以前想过自己的桌面PC定时发,但可能不及时可发送不成功。忽然想起阿里云用了2年,可以放在云上自动发。
- 先是想用LUA来做,遂找工具,最先是blat(一个非常好的工具),整了好几个小时不成功,发现是blat不支持TLS的原因,暂时放弃。
- 然后找到SendmailViaSMTP这个工具,发现很好用,可能半小时就发送成功了。见: https://my.oschina.net/SamXIAO/blog/1790383 SendmailViaSMTP实践-命令行smtp发送邮件-通过QQ邮箱第三方客户端登录授权码
- 今天使用blat+stunnel也成功发送了,在此记录。
- QQ邮箱需要使用TLS进行身份认证。
- 温馨提示:在第三方登录QQ邮箱,可能存在邮件泄露风险,甚至危害Apple ID安全,建议使用QQ邮箱手机版登录。
继续获取授权码登录第三方客户端邮箱 。 生成授权码
操作
-
下载stunnel-5.44-win32-installer.exe
- https://www.stunnel.org/downloads.html
-
下载blat3.2.19: blat3219_32.full.zip
-
http://www.blat.net/
https://sourceforge.net/projects/blat/files/Blat%20Full%20Version/32%20bit%20versions/Win2000%20and%20newer/
-
-
安装stunnel 5.44.
- 全部使用默认安装项即可。
- 以下配置全部按回车键也可以
-
配置stunnel
编缉 C:\Program Files (x86)\stunnel\config\stunnel.conf
[gmail-smtp]
client = yes
accept = 127.0.0.1:10025
connect = smtp.gmail.com:465
verifyChain = yes
CAfile = ca-certs.pem
checkHost = smtp.gmail.com
OCSPaia = yes修改[gmail-smtp]段如下:
[gmail-smtp]
client = yes
accept = 127.0.0.1:10025
connect = smtp.qq.com:465
verifyChain = yes
CAfile = ca-certs.pem
checkHost = smtp.qq.com
OCSPaia = yes -
解压缩blat ,运行CMD命令行工具
- 命令行切换到: blat3219_32.full\blat3219\full
- Windows 2012r2:
-
>ver Microsoft Windows [版本 6.3.9600] >cd D:\backup\tools\电子邮件\Blat - Windows Command Line SMTP Mailer\blat3219_32.full\blat3219\full
-
配置blat
-
$>blat -install 127.0.0.1 [email protected] 3 10025 Blat v3.2.19 (build : Nov 18 2017 03:14:35) 32-bit Windows, Full, Unicode SMTP server set to 127.0.0.1 on port 10025 with user [email protected], retry 3 time(s)
注意这里的port 10025要和stunnel配置文件stunnel.conf的accept = 127.0.0.1:10025 中的相同即可,可以使用其它非知名值。
-
-
命令行发送邮件
-
正确的运行结果:[2018/04/06 周五 10:36:14.24] D:\backup\tools\电子邮件\Blat - Windows Command Li \full > blat .\body.txt -to [email protected],[email protected] -u "[email protected]" -pw "rXXXXxzexzgXXXX" -subject "嗨!HELLO from blat " -attach .\Lic4XXXXXXX_202609.runSN.txt -delay 5 -debug
-
Blat v3.2.19 (build : Nov 18 2017 03:14:35) 32-bit Windows, Full, Unicode <<<getline<<< 220 smtp.qq.com Esmtp QQ Mail Server >>>putline>>> EHLO samxiao-k660.qq.com <<<getline<<< 250-smtp.qq.com <<<getline<<< 250-PIPELINING <<<getline<<< 250-SIZE 73400320 <<<getline<<< 250-AUTH LOGIN PLAIN <<<getline<<< 250-AUTH=LOGIN <<<getline<<< 250-MAILCOMPRESS <<<getline<<< 250 8BITMIME Sending .\body.txt to [email protected], [email protected] Subject: Login name is [email protected] Attached binary file: .\Lic4XXXX_202609.runSN.txt Try number 1 of 3. >>>putline>>> AUTH PLAIN ***** <<<getline<<< 235 Authentication successful >>>putline>>> MAIL FROM:<[email protected]> BODY=8BITMIME <<<getline<<< 250 Ok >>>putline>>> RCPT TO:<[email protected]> <<<getline<<< 250 Ok >>>putline>>> RCPT TO:<[email protected]> <<<getline<<< 250 Ok >>>putline>>> DATA <<<getline<<< 354 End data with <CR><LF>.<CR><LF> <<<getline<<< 250 Ok: queued as >>>putline>>> QUIT <<<getline<<< 221 Bye
-
TLS使用587端口(即:connect = smtp.qq.com:587),不能发送,返回结果如下:
-
Blat v3.2.19 (build : Nov 18 2017 03:14:35)
32-bit Windows, Full, Unicode
Error: Connection to server was dropped.
Error: Connection to server was dropped.
Error: Connection to server was dropped.
*** Error *** SMTP server error
Error: Not a socket.
Error: Not a socket.
-
-
结果-QQ邮箱收到的邮件
参考
Using Blat, Stunnel and Gmail Together
January 13, 2010 3 minute readby Jeff Kastner
这篇文章比较旧了,STUNNEL版本也比较老,用的是GOOGLE的GMAIL
其它
写本文及上一篇共用了3小时。
stunnel.log 文件
stunnel.conf 作以下配置,可以生成stunnel.log文件。
; Debugging stuff (may be useful for troubleshooting)
;debug = info
output = stunnel.log
stunnel.conf 中[gmail-smtp] 可以任意命名而不影响使用
只要accept = 127.0.0.1:10025 中的端口和blat中的配置相同即可。
2018.04.06 10:55:03 LOG5[1]: Service [smtp] connected remote server from 192.168.31.180:19971
2018.04.06 10:58:56 LOG5[0]: Service [gmail-smtp] accepted connection from 127.0.0.1:20593
2018.04.06 11:04:00 LOG5[0]: Service [gmail-sddmtp] connected remote server from 192.168.31.180:21083
2018.04.06 11:05:25 LOG5[1]: Service [gmdd] accepted connection from 127.0.0.1:21129
blat直接给smtp.qq.com:465发送邮件不成功:
$cf465
Blat v3.2.19 (build : Nov 18 2017 03:14:35)
32-bit Windows, Full, Unicode
SMTP server set to smtp.qq.com on port 465 with user [email protected], retry 3 time(s)
$s
$blat .\body.txt -to [email protected] -u "[email protected]" -pw "rsjehxzexzgmbgjh" -subject "嗨!HELLO
from blat " -attach ".\Lic4xxxxx_202609.runSN.txt",".\pf.conf","s.bat" -delay 5 -debug
Blat v3.2.19 (build : Nov 18 2017 03:14:35)
32-bit Windows, Full, Unicode
Error: Wait a bit (possible timeout).
^C终止批处理操作吗(Y/N)? y
blat直接给smtp.qq.com:587发送邮件不成功:
$cf587
Blat v3.2.19 (build : Nov 18 2017 03:14:35)
32-bit Windows, Full, Unicode
SMTP server set to smtp.qq.com on port 587 with user [email protected], retry 3 time(s)
$s
$blat .\body.txt -to [email protected],[email protected] -u "[email protected]" -pw "xxxxxxxxxxgmbgjh" -subject "嗨!HELLOfrom blat " -attach ".\Lic4exxxx.runSN.txt",".\pf.conf","s.bat" -delay 5 -debug
Blat v3.2.19 (build : Nov 18 2017 03:14:35)
32-bit Windows, Full, Unicode
<<<getline<<< 220 smtp.qq.com Esmtp QQ Mail Server
>>>putline>>> EHLO samxiao-k660.qq.com
<<<getline<<< 250-smtp.qq.com
<<<getline<<< 250-PIPELINING
<<<getline<<< 250-SIZE 73400320
<<<getline<<< 250-STARTTLS
<<<getline<<< 250-AUTH LOGIN PLAIN
<<<getline<<< 250-AUTH=LOGIN
<<<getline<<< 250-MAILCOMPRESS
<<<getline<<< 250 8BITMIME
Sending .\body.txt to [email protected]
Subject: Login name is [email protected]
Attached binary file: .\Lic4exxx1_202609.runSN.txt
Attached binary file: .\pf.conf
Attached binary file: s.bat
Try number 1 of 3.
>>>putline>>> AUTH PLAIN *****
<<<getline<<< 530 Must issue a STARTTLS command first.
*** Warning *** The SMTP server did not accept Auth PLAIN value.
*** Warning *** Are your login userid and password correct?
>>>putline>>> AUTH LOGIN
<<<getline<<< 530 Must issue a STARTTLS command first.
*** Warning *** The SMTP server does not require AUTH LOGIN.
*** Warning *** Are you sure server supports AUTH?
>>>putline>>> MAIL FROM:<[email protected]> BODY=8BITMIME
<<<getline<<< 530 Must issue a STARTTLS command first.
*** Error *** The SMTP server does not like the sender name.
*** Error *** Have you set your mail address correctly?
>>>putline>>> QUIT
<<<getline<<< 221 Bye