Python3 发送outlook邮件

import smtplib
from email.mime.text import MIMEText  # 引入smtplib和MIMEText

def send_email(self, to, subject, template):

        mailserver = smtplib.SMTP('smtp.office365.com', 587)
        msg = MIMEText(template, 'html')  # 设置正文为符合邮件格式的HTML内容
        msg['subject'] = subject  # 设置邮件标题
        msg['from'] = self.app.config['MAIL_DEFAULT_SENDER']  # 设置发送人
        msg['to'] = to  # 设置接收人

        mailserver.ehlo()
        mailserver.starttls()
        mailserver.login(self.app.config['MAIL_DEFAULT_SENDER'], self.app.config['MAIL_PASSWORD'])
        mailserver.sendmail(self.app.config['MAIL_DEFAULT_SENDER'], to, msg.as_string())
        mailserver.quit()

你可能感兴趣的:(Python3 发送outlook邮件)