python 发送邮件

封装的方法:

目录

封装的方法:


import xlrd
import time
import xlsxwriter
from xlutils.copy import  copy
from email.mime.multipart import  MIMEMultipart
from email.header import Header
import smtplib
from email.mime.text import MIMEText
#发送邮箱
def emlis(user,password,subject,count,receivers):
    host = 'smtp.qq.com' #  qq邮箱服务器地址
    port = 465 #   qq邮箱默认端口
    user = user     #发件人邮箱号
    password = password # 发件账号授权码
    sender = user # 发件人邮箱号
    receivers = receivers  # 创建收件人账号列表,可以和发件人邮箱一样
    subject = subject    # 邮件标题
    # MIMEText有三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
    try:
        message = MIMEText(count, 'plain', 'utf-8')
        # 邮件标题
        message['Subject'] = Header(subject, 'utf-8')
        #%%
        message['From'] = sender    #发件人邮箱
        #%%
        message['To'] = ';'.join(receivers)      #引入收件人邮箱列表

        smtp_obj = smtplib.SMTP_SSL(host)

        smtp_obj.connect(host, port) #连接qq邮箱
        smtp_obj.login(user, password) # 登录邮箱
        smtp_obj.sendmail(sender, receivers, message.as_string()) #发送邮件
        print ("邮件发送成功")
    except smtplib.SMTPException:
        print ("邮件发送失败")
if __name__ == '__main__':
    # 发送邮件
    user=''  #发送人的qq邮箱
    password=''  #发送人的密钥
    subject=input('请输入标题:')  #邮件的标题
    count=input('请输入内容:') #邮件的内容
    receivers=''  #接收人的邮箱(可以是多个人的列表,发送给多个人)
    emlis(user,password,subject,count,receivers)

版权声明:本文为m0_64224975原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。