使用SMTP模块发送邮件
| import smtplib |
| from email.mime.text import MIMEText |
| from email.header import Header |
| |
| msg_from = '7498@qq.com' |
| passwd = 'nztjmkbfie' |
| msg_to = ['1384@163.com'] |
| |
| |
| subject = "邮件标题" |
| content = "邮件内容:你好啊" |
| |
| msg = MIMEText(content) |
| |
| msg['Subject'] = subject |
| |
| |
| |
| msg['From'] = msg_from |
| |
| msg['To'] = '1384@163.com' |
| |
| try: |
| |
| s = smtplib.SMTP_SSL("smtp.qq.com", 465) |
| |
| s.login(msg_from, passwd) |
| |
| s.sendmail(msg_from, msg_to, msg.as_string()) |
| print('成功') |
| except s.SMTPException as e: |
| print(e) |
| finally: |
| s.quit() |

发送html格式邮件
| import smtplib |
| from email.mime.text import MIMEText |
| from email.header import Header |
| |
| msg_from = '740@qq.com' |
| passwd = 'nzfie' |
| msg_to = ['1374@163.com'] |
| |
| |
| subject = "邮件标题" |
| content = "你好啊" \ |
| "<p><a href='http://www.baidu.com'>百度地址</a></p>" |
| |
| msg = MIMEText(content,'html') |
| |
| msg['Subject'] = subject |
| |
| |
| |
| msg['From'] = msg_from |
| |
| msg['To'] = msg_to |
| |
| try: |
| |
| s = smtplib.SMTP_SSL("smtp.qq.com", 465) |
| |
| s.login(msg_from, passwd) |
| |
| s.sendmail(msg_from, msg_to, msg.as_string()) |
| print('成功') |
| except s.SMTPException as e: |
| print(e) |
| finally: |
| s.quit() |
