# from email.mime.text import MIMEText
#
# msg = MIMEText('hello, send by Python...', 'plain', 'utf-8')
# # 输入Email地址和口令:
# from_addr = input('From: ')
# password = input('Password: ')
# # 输入SMTP服务器地址:
# smtp_server = input('SMTP server: ')
# # 输入收件人地址:
# to_addr = input('To: ')
#
# import smtplib
#
# server = smtplib.SMTP(smtp_server, 25) # SMTP协议默认端口是25
# server.set_debuglevel(1)
# server.login(from_addr, password)
# server.sendmail(from_addr, [to_addr], msg.as_string())
# server.quit() # coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header sender = 'fengyiru6369@163.com'
receiver = '1194150169@qq.com'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = 'fengyiru6369@163.com'
password = 'XXXXXXXX' # 是授权密码,而不是登录密码 msg = MIMEText('你好', 'text', 'utf-8') # 中文需参数‘utf-8’,单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8') smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
只赋值了msg['Subject'], 没有赋值msg['From'], msg['To'],导致出现554情况,更改了后就好了。
注意MIMEText初始化的时候,中文的第二个参数要用'plain',我用'text',中文就显示不出来。
msg['From'] 中人名为Tim,会和下面对应起来。