python发送email邮件
2021-01-28 10:15
                         标签:服务   head   block   name   验证   新窗口   第一步   smtp   size    发送邮件需要使用SMTP服务器,常用的免费服务器有:163、126、qq等邮箱。     python发送email邮件 标签:服务   head   block   name   验证   新窗口   第一步   smtp   size    原文地址:https://www.cnblogs.com/wanglle/p/12838856.html一.开启邮箱SMTP服务
QQ邮箱配置方式如下:
1) 登录qq邮箱,选择【设置】-【账户】

2) 在POP3/IMAP/SMAP/Exchage/CardDAV/CalDAV服务中,找到POP3/SMTP服务和IMAP/SMTP服务,点击开启。



163邮箱配置方式如下:
1)注册163邮箱,登录后设置。

2) 在新页面点击“客户端授权密码”,勾选“开启”,弹出新窗口填写手机验证码。

3) 填写授权码

4) 提示开启成功。

二、使用python发送邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
1、发送普通的邮件
# 发送邮件的步骤
import smtplib
from email.mime.text import MIMEText     # 用来构造文本类型的邮件
from email.header import Header          # 用来构造邮件的头部
# 第一步:创建一个SMTP的对象
s = smtplib.SMTP()
# 第二步:连接到SMTP的服务器
host = ‘smtp.163.com‘    # 设置163邮箱服务器,端口为:25
port = 25
#  host = ‘smtp.qq.com‘  port = 465  # 设置qq邮箱服务器,端口为:465
s.connect(host,port)       # 连接服务器
# s.connect(host = ‘smtp.163.com‘,port = 25)
# 第三步:登录SMTP服务器
mail_user = ‘18814726725@163.com‘           # 163邮箱的用户名
mail_pass = ‘password‘                      # 注意:此处填写的是邮箱的SMTP服务器授权码
s.login(user=mail_user,password=mail_pass)
# 第四步:构建邮件内容
content = ‘使用python测试发送邮件‘             # 构建邮件内容
msg = MIMEText(content,_charset=‘utf8‘)     # _charset 指定编码格式
msg[‘Subject‘] = Header(‘测试报告‘,‘utf8‘)   # 邮件主题
msg[‘From‘] = ‘wl18814726725@163.com‘       # 发件人邮箱,可传入列表,用于给多个人发送文件
msg[‘To‘] = ‘1572533878@qq.com‘             # 收件人
# 第五步:发送邮件
s.sendmail(from_addr=msg[‘From‘],to_addrs=msg[‘To‘],msg=msg.as_string())     #将邮件内容转换为字符串
2、发送HTML格式邮件及附件
import smtplib
from email.mime.text import MIMEText     # 文本类型的邮件,用来构造邮件
from email.header import Header          # 用来构造邮件的头部
from email.mime.application import MIMEApplication   
from email.mime.multipart import MIMEMultipart  # 用来构造附件
# 发送邮件的步骤
# 第一步:创建一个SMTP的对象
s = smtplib.SMTP()
# 第二步:连接到SMTP的服务器
host = ‘smtp.163.com‘    # 设置163邮箱服务器,端口为:25
port = 25
#  host = ‘smtp.qq.com‘    # 设置qq邮箱服务器,端口为:465
s.connect(host,port)   # 连接服务器
# 第三步:登录SMTP服务器
mail_user = ‘wl18814726725@163.com‘     # 163邮箱的用户名
mail_pass = ‘wl987654321‘               # 注意:此处填写的是邮箱的SMTP服务器授权码
s.login(user=mail_user,password=mail_pass)
# 构造文本邮件内容
content = ‘使用python测试发送邮件‘             # 构建邮件内容
textcontent = MIMEText(content,_charset=‘utf8‘)        # _charset 指定编码格式
# 构造附件(二进制字节流形式)
part = MIMEApplication(open("report.html",‘rb‘).read(),_subtype=None) 
# part = MIMEApplication(open("report.html",‘rb‘).read()) 需要查看_subtype=None 是否会引发异常
part.add_header(‘content-disposition‘, ‘attachment‘, filename=‘report18.html‘)  # 对方收到邮件之后,附件在邮件中显示的名称
# 封装一封邮件
msg = MIMEMultipart()
# 加入文本内容
msg.attach(textcontent)
msg.attach(part)
# 发送邮件
msg[‘From‘] = ‘wl18814726725@163.com‘       #发件人邮箱
msg[‘To‘] = ‘1572533878@qq.com‘             #收件人
#第五步:发送邮件
s.sendmail(from_addr=‘wl18814726725@163.com‘,to_addrs=‘1572533878@qq.com‘,msg=msg.as_string())      # 将邮件内容转换为字符串
  定义send_email函数
import smtplib
from email.mime.text import MIMEText     #文本类型的邮件,用来构造邮件
from email.header import Header          #用来构造邮件的头部
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart  #用来构造附件
def send_email(filepath):
    """
    :param filepath:   #传入报告文件的路径
    :return:
    """
    # 发送邮件的步骤
    # 第一步:创建一个SMTP的对象
    s = smtplib.SMTP()
    # 第二步:连接到SMTP的服务器
    host = ‘smtp.163.com‘    #设置163邮箱服务器,端口为:25
    port = 25
    #  host = ‘smtp.qq.com‘    #设置qq邮箱服务器,端口为:465
    s.connect(host,port)   #连接服务器
    # 第三步:登录SMTP服务器
    mail_user = ‘wl18814726725@163.com‘     #163邮箱的用户名
    mail_pass = ‘wl987654321‘               #注意:此处填写的是邮箱的SMTP服务器授权码
    s.login(user=mail_user,password=mail_pass)
    #构造文本邮件内容
    content = ‘使用python测试发送邮件‘             #构建邮件内容
    textcontent = MIMEText(content,_charset=‘utf8‘)        #_charset 指定编码格式
    #构造附件(二进制字节流形式)
    part = MIMEApplication(open(filepath,‘rb‘).read())
    part.add_header(‘content-disposition‘, ‘attachment‘, filename=‘report988.html‘)  #对方收到邮件之后,附件在邮件中显示的名称
    # 封装一封邮件
    msg = MIMEMultipart()
    #加入附件和文本内容
    msg.attach(textcontent)
    msg.attach(part)
    #发送邮件
    msg[‘From‘] = ‘wl18814726725@163.com‘       #发件人邮箱
    msg[‘To‘] = ‘1572533878@qq.com‘             #收件人
    #第五步:发送邮件
    s.sendmail(from_addr=msg[‘From‘],to_addrs=msg[‘To‘],msg=msg.as_string())      #将邮件内容转换为字符串
send_email(‘report.html‘)
import smtplib
from email.mime.text import MIMEText     # 文本类型的邮件,用来构造邮件
from email.header import Header          # 用来构造邮件的头部
from email.mime.application import MIMEApplication   
from email.mime.multipart import MIMEMultipart  # 用来构造附件
# 发送邮件的步骤
# 第一步:创建一个SMTP的对象
s = smtplib.SMTP()
# 第二步:连接到SMTP的服务器
host = ‘smtp.163.com‘    # 设置163邮箱服务器,端口为:25
port = 25
#  host = ‘smtp.qq.com‘    # 设置qq邮箱服务器,端口为:465
s.connect(host,port)   # 连接服务器
# 第三步:登录SMTP服务器
mail_user = ‘wl18814726725@163.com‘     # 163邮箱的用户名
mail_pass = ‘wl987654321‘               # 注意:此处填写的是邮箱的SMTP服务器授权码
s.login(user=mail_user,password=mail_pass)
# 构造文本邮件内容
content = ‘使用python测试发送邮件‘             # 构建邮件内容
textcontent = MIMEText(content,_charset=‘utf8‘)        # _charset 指定编码格式
# 构造附件(二进制字节流形式)
part = MIMEApplication(open("report.html",‘rb‘).read(),_subtype=None) 
# part = MIMEApplication(open("report.html",‘rb‘).read()) 需要查看_subtype=None 是否会引发异常
part.add_header(‘content-disposition‘, ‘attachment‘, filename=‘report18.html‘)  # 对方收到邮件之后,附件在邮件中显示的名称
# 封装一封邮件
msg = MIMEMultipart()
# 加入文本内容
msg.attach(textcontent)
msg.attach(part)
# 发送邮件
msg[‘From‘] = ‘wl18814726725@163.com‘       #发件人邮箱
msg[‘To‘] = ‘1572533878@qq.com‘             #收件人
#第五步:发送邮件
s.sendmail(from_addr=‘wl18814726725@163.com‘,to_addrs=‘1572533878@qq.com‘,msg=msg.as_string())      # 将邮件内容转换为字符串
定义send_email函数
import smtplib
from email.mime.text import MIMEText     #文本类型的邮件,用来构造邮件
from email.header import Header          #用来构造邮件的头部
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart  #用来构造附件
def send_email(filepath):
"""
:param filepath:   #传入报告文件的路径
:return:
    """
# 发送邮件的步骤
    # 第一步:创建一个SMTP的对象
s = smtplib.SMTP()
# 第二步:连接到SMTP的服务器
host = ‘smtp.163.com‘    #设置163邮箱服务器,端口为:25
port = 25
#  host = ‘smtp.qq.com‘    #设置qq邮箱服务器,端口为:465
s.connect(host,port)   #连接服务器
    # 第三步:登录SMTP服务器
mail_user = ‘wl18814726725@163.com‘     #163邮箱的用户名
mail_pass = ‘wl987654321‘               #注意:此处填写的是邮箱的SMTP服务器授权码
s.login(user=mail_user,password=mail_pass)
#构造文本邮件内容
content = ‘使用python测试发送邮件‘             #构建邮件内容
textcontent = MIMEText(content,_charset=‘utf8‘)        #_charset 指定编码格式
    #构造附件(二进制字节流形式)
part = MIMEApplication(open(filepath,‘rb‘).read())
    part.add_header(‘content-disposition‘, ‘attachment‘, filename=‘report988.html‘)  #对方收到邮件之后,附件在邮件中显示的名称
    # 封装一封邮件
msg = MIMEMultipart()
#加入附件和文本内容
msg.attach(textcontent)
    msg.attach(part)
#发送邮件
msg[‘From‘] = ‘wl18814726725@163.com‘       #发件人邮箱
msg[‘To‘] = ‘1572533878@qq.com‘             #收件人
    #第五步:发送邮件
s.sendmail(from_addr=msg[‘From‘],to_addrs=msg[‘To‘],msg=msg.as_string())      #将邮件内容转换为字符串
send_email(‘report.html‘)
上一篇:Java开发环境搭建