python发送邮件的代码实现

2022-10-28 11:25:36 浏览数 (1)

这里给出用python发送qq邮件的代码,其中默认为登录一个qq邮箱向另一个地址发送内容。

代码语言:javascript复制
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
import time

mailto_list = ['邮件接收地址']
mail_host = "smtp.qq.com"  # 设置服务器
mail_user = "登入的目标qq号"  # 用户名
mail_pass = "从qq邮箱服务里得到的口令"  # 口令
mail_postfix = "qq.com"  # 发件箱的后缀


def send_mail(to_list, sub, content):
    me = "hello"   "<"   mail_user   "@"   mail_postfix   ">"
    msg = MIMEText(content, _subtype='plain', _charset='gb2312')
    msg['Subject'] = sub
    msg['From'] = me
    msg['To'] = ";".join(to_list)
    server = smtplib.SMTP()
    server.connect(mail_host)
    server.login(mail_user, mail_pass)
    server.sendmail(me, to_list, msg.as_string())
    server.close()
    return True

在使用的时候指定三个参数,to_list即为接收方,sub好像是标题(忘了),content为邮件内容。

0 人点赞