python发送邮件入门——163.com和outlook

一、163邮箱发送邮件

1. 开通授权码

登录163网页版邮箱


登录163网页邮箱

点击开启,跟着步骤操作,需要记住授权码


开启后截图

2. 代码:

# 1. 导入模块
import smtplib   # 服务器模块
from email.mime.text import MIMEText   # 构建邮件模块

# 2.构建邮件
# 2.1主体(内容)
text = MIMEText('HELLO,小张.')
# 2.2 头部
text['subject'] = 'hello'   # 主题
text['from'] = '[email protected]'   # 发件人的邮箱
text['to'] = '[email protected]'   # 收件人

# 3.登录163服务器
smtp = smtplib.SMTP_SSL(host='smtp.163.com', port=587)
smtp.login('[email protected]', '授权码')  # 密码使用授权码!!!

# 4.发送邮件
smtp.sendmail('[email protected]', ['[email protected]'], text.as_string())

# 5.关闭服务器
smtp.close()

二、outlook邮箱发送邮件

1. 查看SMTP服务器名称和开启的端口

进入设置页面

查看服务器和SMTP开启的端口

2. 代码

import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 第三方 SMTP 服务
mail_host = "smtp.office365.com"   # 设置服务器
mail_user = "[email protected]"   # 用户名
mail_pass = "***"  # 密码

sender = '[email protected]'
receivers = ['[email protected]']  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

message = MIMEText('发送测试...', 'plain', 'utf-8')

subject = '测试'
message['Subject'] = Header(subject, 'utf-8')

try:
    smtpObj = smtplib.SMTP(mail_host, 587)
    smtpObj.ehlo()
    smtpObj.starttls()
    smtpObj.login(mail_user , mail_pass)
    smtpObj.sendmail(sender, receivers, message.as_string())
    print("邮件发送成功")
except smtplib.SMTPException:
    print("Error: 无法发送邮件")

发送邮件新的补充

from ruamel import yaml
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from FBL5N.mail_reminder_0829 import read_config


# 读取配置信息
def read_config():
    print("读取配置信息")
    with open('文件路径', 'r', encoding='utf-8') as f:
        temp = yaml.load(f.read())
    return temp


def send_email():
    """
    :param receiver: 收件人
    :param subject: 邮件主题
    :param context: 邮件的正文 ;注意是HTML格式
    :return: 无返回结果
    """
    print("发送邮件")
    # =============邮件配置信息=========
    # 读取配置信息
    r_config = read_config()
    e_conf = r_config['send_email']
    # 邮箱服务器
    smtpserver = e_conf['host']
    # 邮箱用户/密码
    user = e_conf['username']
    password = e_conf['password']
    # 端口
    port = e_conf['port']
    # 发件人
    sender = e_conf['sender']
    # 收件人
    receivers = e_conf['receivers']
    # 抄送
    cc = e_conf['cc']
    # 密件抄送
    bcc = e_conf['bcc']
    # 发送邮件主题
    subject = "邮箱测试"
    # 邮件内容
    context = "测试ing"
    # 编写HTML类型的邮件正文
    msg = MIMEText(context, 'html', 'utf-8')
    # msg['Subject'] = Header(subject, 'utf-8')
    # msg['To'] = ','.join(receivers) if isinstance(receivers, list) else receivers
    # msg['Bcc'] = ','.join(bcc) if isinstance(bcc, list) else receivers

    msga = MIMEMultipart('alternative')
    msga['Subject'] = Header(subject, 'utf-8')
    msga['To'] = ','.join(receivers) if isinstance(receivers, list) else receivers
    msga['Cc'] = ','.join(cc) if isinstance(cc, list) else receivers
    msga['Bcc'] = ','.join(bcc) if isinstance(bcc, list) else receivers
    msga.attach(msg)

    resp = receivers + bcc   # 列表格式
    
    try:
        # 连接发送邮件
        smtp = smtplib.SMTP(smtpserver, port)
        # smtp.connect(smtpserver, port)
        # smtp.ehlo()  # 向 outlook 发送SMTP 'ehlo' 命令
        smtp.starttls()
        if user and password:
            smtp.login(user, password)
        smtp.sendmail(sender, resp, msga.as_string())
        smtp.quit()
        print("yes")
    except smtplib.SMTPException:
        text = "Error: 无法发送邮件"
        print("ERROR")
    finally:
        print("不管执行try还是except都要执行finally")


send_email()

你可能感兴趣的:(python发送邮件入门——163.com和outlook)