python-各个主流邮件发送方式

smtplib 是一个用于python2.7和python3.x的内置包,所以有不需要安装。您可以导入它,而无需安装任何其他软件包。
参考:https://docs.python.org/3/library/smtplib.html?highlight=smtplib

如果你安装,会出现以下错误:
 

$ pip3 install smtplib
Collecting smtplib
  ERROR: Could not find a version that satisfies the requirement smtplib (from versions: none)
ERROR: No matching distribution found for smtplib

使用163.com发送邮件:

# !/usr/bin/python
# -*- coding: UTF-8 -*-

import smtplib
from email.mime.text import MIMEText

# 第三方 SMTP 服务
mail_host = "smtp.163.com"  # SMTP服务器
mail_port = 465
mail_user = "my-user@163.com"  # 用户名
mail_pass = "esefskjhurhsjeisjie"  # 授权密码,非登录密码 (这个不是网页版的登陆密码,需要使用"应用程序专用密码")

sender = 'my-user@163.com' # 发件人邮箱(最好写全, 不然会失败)
receivers = [ 'xxxxxxxx@qq.com' ] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

content = '我用Python学习过程!'
title = 'Python学习文档'  # 邮件主题

def sendEmail():
    message = MIMEText(content, 'plain', 'utf-8')  # 内容, 格式, 编码
    message['From'] = "{}".format(sender)
    message['To'] = ",".join(receivers)
    message['Subject'] = title

    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, mail_port)  # 启用SSL发信, 端口一般是465
        smtpObj.login(mail_user, mail_pass)  # 登录验证
        smtpObj.ehlo()
        smtpObj.sendmail(sender, receivers, message.as_string())  # 发送
        smtpObj.close()
        print("mail has been send successfully.")
    except smtplib.SMTPException as e :
        print(e)

if __name__ == '__main__':
    sendEmail()

163.com 发送邮件常见错误:http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html

使用gmail发送邮件:

# !/usr/bin/python
# -*- coding: UTF-8 -*-

import smtplib
from email.mime.text import MIMEText

# 第三方 SMTP 服务
mail_host = "smtp.gmail.com"  # SMTP服务器
mail_port = 465
mail_user = "my-user@gmail.com"  # 用户名
mail_pass = "esefskjhurhsjeisjie"  # 授权密码,非登录密码 (goole 账号设置了2级密码,需要使用"应用程序专用密码")

sender = 'my-user@gmail.com' # 发件人邮箱(最好写全, 不然会失败)
receivers = [ 'xxxxxxxx@qq.com' ] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

content = '我用Python学习过程!'
title = 'Python学习文档'  # 邮件主题

def sendEmail():
    message = MIMEText(content, 'plain', 'utf-8')  # 内容, 格式, 编码
    message['From'] = "{}".format(sender)
    message['To'] = ",".join(receivers)
    message['Subject'] = title

    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, mail_port)  # 启用SSL发信, 端口一般是465
        smtpObj.login(mail_user, mail_pass)  # 登录验证
        smtpObj.ehlo()
        smtpObj.sendmail(sender, receivers, message.as_string())  # 发送
        smtpObj.close()
        print("mail has been send successfully.")
    except smtplib.SMTPException as e :
        print(e)

if __name__ == '__main__':
    sendEmail()

gmail 发送邮件常见错误:https://support.google.com/mail/?p=BadCredentials

使用QQ邮箱发送邮件:

QQ邮箱服务器:smtp.qq.com,如果是腾讯企业邮箱,其服务器为smtp.exmail.qq.com,方式给上面的163.com和gmail一样。

发送带附件的邮件:

# !/usr/bin/python
# -*- coding: UTF-8 -*-

import smtplib
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
import email.mime.text

# 第三方 SMTP 服务
mail_host = "smtp.gmail.com"  # SMTP服务器
mail_port = 465
mail_user = "my-user@gmail.com"  # 用户名
mail_pass = "eskeskefksekjsefsek"  # 授权密码,非登录密码 (goole 账号设置了2级密码,需要使用"应用程序专用密码")

sender = 'my-user@gmail.com' # 发件人邮箱(最好写全, 不然会失败)
receivers = [ 'xxxxxx@qq.com' ] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

content = '我用Python学习过程!'
title = 'Python学习文档'  # 邮件主题


def sendEmail():
    message = MIMEMultipart()
    message['From'] = "{}".format(sender)
    message['To'] = ",".join(receivers)
    message['Subject'] = title

    # 添加邮件正文
    txt = MIMEText(content, 'plain', 'utf-8')  # 内容, 格式, 编码
    message.attach(txt)

    # 添加附件,从本地路径读取。如果添加多个附件,可以定义part_2,part_3等,然后使用part_2.add_header()和msg.attach(part_2)即可。
    part = MIMEApplication(open('/Users/my-user/PycharmProjects/py-flask/templates/list.html', 'rb').read())
    part.add_header('Content-Disposition', 'attachment', filename="list.html")  # 给附件重命名,一般和原文件名一样,改错了可能无法打开.
    message.attach(part)

    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, mail_port)  # 启用SSL发信, 端口一般是465
        smtpObj.login(mail_user, mail_pass)  # 登录验证
        smtpObj.ehlo()
        smtpObj.sendmail(sender, receivers, message.as_string())  # 发送
        smtpObj.close()
        print("mail has been send successfully.")
    except smtplib.SMTPException as e :
        print(e)

if __name__ == '__main__':
    sendEmail()

发送HTML格式的邮件:

基本跟上面一样,修改的地方有2个:

1. 内容可以是html格式的文本,如:

content = '<h1>我用Python学习过程!</h1><a href="https://www.baidu.com">baidu.com</a>'

* a 中的href 需要带删http/https,在QQ邮箱不带协议,就会打不开链接。 

2. 邮件正文,使用html格式:MIMEText(content, 'html', 'utf-8')

# !/usr/bin/python
# -*- coding: UTF-8 -*-

import smtplib
from email.mime.text import MIMEText

# 第三方 SMTP 服务
mail_host = "smtp.gmail.com"  # SMTP服务器
mail_port = 465
mail_user = "my-user@gmail.com"  # 用户名
mail_pass = "esefskjhurhsjeisjie"  # 授权密码,非登录密码 (goole 账号设置了2级密码,需要使用"应用程序专用密码")

sender = 'my-user@gmail.com' # 发件人邮箱(最好写全, 不然会失败)
receivers = [ 'xxxxxxxx@qq.com' ] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱

content = '<h1>我用Python学习过程!</h1><a href="https://www.baidu.com">baidu.com</a>'
title = 'Python学习文档'  # 邮件主题

def sendEmail():
    message = MIMEText(content, 'html', 'utf-8')  # 内容, 格式, 编码
    message['From'] = "{}".format(sender)
    message['To'] = ",".join(receivers)
    message['Subject'] = title

    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, mail_port)  # 启用SSL发信, 端口一般是465
        smtpObj.login(mail_user, mail_pass)  # 登录验证
        smtpObj.ehlo()
        smtpObj.sendmail(sender, receivers, message.as_string())  # 发送
        smtpObj.close()
        print("mail has been send successfully.")
    except smtplib.SMTPException as e :
        print(e)

if __name__ == '__main__':
    sendEmail()

参考:

https://www.runoob.com/python/python-email.html
https://stackabuse.com/how-to-send-emails-with-gmail-using-python/

例子:

https://www.runoob.com/python/python-email.html 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值