python使用qq邮箱(个人邮箱)发送邮件需开启qq邮箱的SMTP服务
在设置中开启pop3/SMTP服务,返回的密码就是之后代码中登录使用账户密码(在完整代码中标识了出来)
之后出现如下错误
复制代码 代码如下:
smtplib.SMTPAuthenticationError: (530, 'Error: A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bin/help?id=28')
错误说要开ssl发送邮件
在原来的代码上添加上如下三行代码即可
1
2
3
|
smtpObj.ehlo() smtpObj.starttls() smtpObj.ehlo() |
完整代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import smtplib from email.mime.text import MIMEText from email.header import Header # 第三方 SMTP 服务 mail_host = "smtp.qq.com" # 设置服务器,qq的SMTP服务host mail_user = "xxx@qq.com" # 用户名(须修改) mail_pass = "xxxxxxxxxxxxxxxx" # 此处为在qq开启SMTP服务时返回的密码 (须修改) sender = 'xxx@qq.com' # 同用户名 (须修改) receivers = [ 'xxx@qq.com' ] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 message = MIMEText( '...' , 'plain' , 'utf-8' ) message[ 'From' ] = Header( "第一封python测试邮件" , 'utf-8' ) message[ 'To' ] = Header( "测试" , 'utf-8' ) try : subject = 'Python SMTP 邮件测试' message[ 'Subject' ] = Header(subject, 'utf-8' ) smtpObj = smtplib.SMTP() smtpObj.connect(mail_host, 25 ) # 25 为 SMTP 端口号 smtpObj.ehlo() smtpObj.starttls() smtpObj.ehlo() smtpObj.login(mail_user, mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) print "邮件发送成功" except smtplib.SMTPException : print "Error: 无法发送邮件" |
在使用sina邮箱开启SMPT服务后使用python发送信息卡在了如下错误#不是很理解
1
|
smtplib.SMTPDataError: ( 553 , 'Envolope sender mismatch with header from..' ) |
成功的例子是
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import smtplib server = "smtp.sina.com" fromaddr = "xxx@sina.com" #须修改 toaddr = "xxx@qq.com" #须修改 msg = """ to:%s from:%s Hello,I am smtp server """ % (toaddr,fromaddr) s = smtplib.SMTP(server) s.set_debuglevel( 1 ) s.login( "xxx@sina.com" , "xxx" ) #须修改 s.sendmail(fromaddr,toaddr,msg) |
报错的例子是
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import smtplib from email.mime.text import MIMEText from email.header import Header server = "smtp.sina.com" fromaddr = "xxx@sina.com" toaddr = "xxx@qq.com" message = MIMEText( '...' , 'plain' , 'utf-8' ) message[ 'From' ] = Header(fromaddr, 'utf-8' ) message[ 'To' ] = Header( "toaddr" , 'utf-8' ) subject = 'Python SMTP 邮件测试' message[ 'Subject' ] = Header(subject, 'utf-8' ) s = smtplib.SMTP(server) s.set_debuglevel( 1 ) s.login( "xxx@sina.com" , "xxx" ) s.sendmail(fromaddr,toaddr,message) |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/Jacck/p/7656439.html