python3发送smtp邮件

来源:互联网 发布:js闪动文字 编辑:程序博客网 时间:2024/06/07 00:13

python3发送smtp邮件

import smtplibfrom email.mime.text import MIMETextfrom email.header import Headerdef sendmail():    smtphost = 'smtp.163.com'  # smtp服务器    port = 465  # smtp服务器端口    user = 'xxx@163.com'  # 邮箱账号    pwd = 'xxx'  # 邮箱密码    receiver = 'xxx@qq.com'  # 收件人    # 主题 和 内容    subject = '出塞'    content = '''    你好:        秦时明月汉时关,万里长征人未还。        但是龙城飞将在,不教胡马度阴山。    好好学习    '''    msg = MIMEText(content, 'plain', )    msg['from'] = Header(user)    msg['to'] = Header(receiver)    msg['subject'] = Header(subject)    try:        smtpObj = smtplib.SMTP_SSL(smtphost, port) # SSL加密        smtpObj.login(user, pwd)        smtpObj.sendmail(user, receiver, msg.as_string())        print("send mail successfully")    except smtplib.SMTPException as e:        print(e)# 测试发送邮件sendmail()