【Python】实现自动发送邮件

需要用到的模块:

smtplib,email
在这里插入图片描述
提醒!QQ 邮箱一般默认关闭SMTP服务,我们得先去开启它。请打开https://mail.qq.com/,登录你的邮箱。然后点击位于顶部的【设置】按钮,选择【账户设置】,然后下拉到这个位置。
在这里插入图片描述
这里详细介绍

# smtplib 用于邮件的发信动作import smtplibfrom email.mime.text import MIMEText# email 用于构建邮件内容from email.header import Header# 用于构建邮件头 # 发信方的信息:发信邮箱,QQ 邮箱授权码from_addr = 'xxx@qq.com'password = '你的授权码数字' # 收信方邮箱to_addr = 'xxx@qq.com' # 发信服务器smtp_server = 'smtp.qq.com' # 邮箱正文内容,第一个参数为内容,第二个参数为格式(plain 为纯文本),第三个参数为编码msg = MIMEText('send by python','plain','utf-8') # 邮件头信息msg['From'] = Header(from_addr)msg['To'] = Header(to_addr)msg['Subject'] = Header('python test') # 开启发信服务,这里使用的是加密传输server=smtplib.SMTP_SSL(smtp_server)
server.connect(smtp_server,465)# 登录发信邮箱server.login(from_addr, password)# 发送邮件server.sendmail(from_addr, to_addr, msg.as_string())# 关闭服务器server.quit()
————————————————
版权声明:本文为CSDN博主「LeoPhilo」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/LeoPhilo/article/details/89074232

这里实时操作

import smtplib;
from email.mime.text import MIMEText
def send_email(host,username,passwd,send_to,subject,content):
    msg = MIMEText( content.encode('utf8'), _subtype = 'html', _charset = 'utf8')
    msg['From'] = username
    msg['Subject'] = u'%s' % subject
    msg['To'] = ",".join(send_to
try:
        s = smtplib.SMTP_SSL(host,465)        
        s.login(username, passwd )
        s.sendmail(username, send_to,msg.as_strin*g())
        s.close()
    except Exception as e:
        print('Exception: send email failed:{}'.format(e))
if __name__ == '__main__':
    host = 'smtp.qq.com'#或者qq改为其他邮箱
    username = '*@qq.com'
    passwd = '*'
    to_list = ['*@qq.com']
    subject = "邮件主题"
    content = '使用Python发送邮件'
    send_email(host,username,passwd,to_list,subject,content)

效果:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值