python 利用 smtplib 发送邮件方法

说明

python 自带了 smtplib 库
可以直接调用并进行邮件发送
默认状态下, python 利用 base64 进行用户名密码传递
测试期间, 可以打开 debug 功能, 方便进行排错

测试代码

import smtplib

subject = 'message subject'
to = '收邮件的邮箱ex: [email protected]'
sender = '发邮件邮箱 ex: [email protected]'
user = '账号'
password = '密码'
mailserver = '邮件服务器地址 ex: smtp.163.com'
mailport = '邮件服务器端口 ex:25'
try:
    smtpserver = smtplib.SMTP(mailserver, mailport)
    smtpserver.set_debuglevel(1)  # 这里可以打开调试模式, 调试模式可以阅读当前邮件服务器支持哪些认证方法这个很重要
    smtpserver.ehlo()
    smtpserver.starttls()   # 启用 tls 验证方法, 如果服务器需要提供 KEY, 那么把 keyfile 放到函数中
    smtpserver.ehlo()
    user = user
    password = password
    smtpserver.login(user, password)
    header = 'To:' + to + '\n' + 'From: ' + sender + '\n' + 'Subject:' + subject + '\n'
    message = header + '\n This is my message'
    smtpserver.sendmail(sender, to, message)
    smtpserver.close()
except Exception as e:
    print e
finally:
    print "done"

提示

错误信息如下

smtplib.SMTPAuthenticationError: (535, '5.7.3 Authentication unsuccessful'

描述

常见情况是, 当前用户名与密码填写有问题
user 部分不需要填写整个邮件地址, 即不需要带 @163.com 的域名

debug 信息

常见 debug 信息如下, 参考一下

send: 'ehlo this-is-my-hostname\r\n'
reply: '250-mail.server.repose Hello [xx.xxx.xxx.xx]\r\n'
reply: '250-SIZE 36700160\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-DSN\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-AUTH NTLM LOGIN\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-BINARYMIME\r\n'
reply: '250 CHUNKING\r\n'
reply: retcode (250); Msg: mail.server.repose Hello [10.xxx.xx.xx]
SIZE 36700160
PIPELINING
DSN
ENHANCEDSTATUSCODES
STARTTLS
AUTH NTLM LOGIN
8BITMIME
BINARYMIME
CHUNKING
send: 'ehlo pre-pinyun.localdomain\r\n'
reply: 'this-is-my-hostname Hello [x.x.x.x]\r\n'
reply: '250-SIZE 36700160\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-DSN\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-AUTH NTLM LOGIN\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-BINARYMIME\r\n'
reply: '250 CHUNKING\r\n'
reply: retcode (250); Msg: this-is-my-hostname Hello [x.x.x.x]
SIZE 36700160
PIPELINING
DSN
ENHANCEDSTATUSCODES
STARTTLS
AUTH NTLM LOGIN
8BITMIME
BINARYMIME
CHUNKING
send: 'AUTH LOGIN c2FuLnpoYW5n\r\n'
reply: '334 UGFzc3dvcmQ6\r\n'
reply: retcode (334); Msg: UGFzc3dvcmQ6
send: 'YWJjLjEyMzQ=\r\n'
reply: '235 2.7.0 Authentication successful\r\n'
reply: retcode (235); Msg: 2.7.0 Authentication successful
send: 'mail FROM:<[email protected]> size=97\r\n'
reply: '250 2.1.0 Sender OK\r\n'
reply: retcode (250); Msg: 2.1.0 Sender OK
send: 'rcpt TO:<[email protected]>\r\n'
reply: '250 2.1.5 Recipient OK\r\n'
reply: retcode (250); Msg: 2.1.5 Recipient OK
send: 'data\r\n'
reply: '354 Start mail input; end with <CRLF>.<CRLF>\r\n'
reply: retcode (354); Msg: Start mail input; end with <CRLF>.<CRLF>
data: (354, 'Start mail input; end with <CRLF>.<CRLF>')
send: 'To:[email protected]\r\nFrom: [email protected]\r\nSubject:message subject\r\n\r\n This is my message\r\n.\r\n'
reply: '250 2.6.0 <faedbf573d104b7b8daf061999e1fa84@mail-server> [InternalId=14018773254225, Hostname=this-is-my-hostname] Queued mail for delivery\r\n'
reply: retcode (250); Msg: 2.6.0 <[email protected]> [InternalId=14018773254225, Hostname=this-is-my-hostname] Queued mail for delivery
data: (250, '2.6.0 <faedbf573d104b7b8daf061999e1fa84@this-is-my-hostname> [InternalId=14018773254225, Hostname=this-is-my-hostname] Queued mail for delivery')
done

留意这里就是登陆验证的过程, 数据经过了 base64 加密

send: 'AUTH LOGIN cs2FuLnxxxxxpoYW5n\r\n'
reply: '334 UGF3zcxxxxx3dvcmQ6\r\n'         <- 其中 334 就是服务器正常应答的代码
reply: retcode (334); Msg: UGFzc3xxxxxdvcmQ6
send: 'YWsJwjLjxxxxxEyMzQ=\r\n'             <- 这里发送的就是你的密码加密信息
reply: '235 2.7.0 Authentication successful\r\n'    <- 这里确认了验证是否正确

猜你喜欢

转载自blog.csdn.net/signmem/article/details/80987864
今日推荐