python之使用smtplib模块发送邮件
1 # 使用smtplib模块发送邮件 2 import smtplib 3 from email.mime.text import MIMEText 4 from email.header import Header 5 6 # 发送邮箱 7 sender = 'xxx@163.com' 8 # 接收邮箱 9 receiver = 'xxx@qq.com' 10 # 发送邮件主题 11 subject = '测试邮件主题' 12 # 发送邮箱服务器 13 smtpserver = 'smtp.163.com' 14 # 发送邮箱用户/密码 15 username = 'xxx@163.com' 16 password = 'xxx' 17 # 组装邮件内容和标题,中文需参数‘utf-8’,单字节字符不需要 18 msg = MIMEText('你好!这是一封测试邮件!', 'plain', 'utf-8') 19 msg['Subject'] = Header(subject, 'utf-8') 20 msg['From'] = 'Larry<xxx@163.com>' 21 msg['To'] = "xxx@qq.com" 22 # 登录并发送邮件 23 smtp = smtplib.SMTP() 24 smtp.connect(smtpserver) 25 smtp.login(username, password) 26 smtp.sendmail(sender, receiver, msg.as_string()) 27 smtp.quit()