汇总Python发邮件的15个常用方式(附代码)
创始人
2024-11-08 05:36:53

Python 提供了多种发送电子邮件的方式,一些常见的方法及其代码汇总:

文章目录

      • 1. 使用 `smtplib` 模块
      • 2. 使用 `yagmail` 库
      • 3. 使用 `email` 模块和 `smtplib`
      • 4. 使用 `flask-mail` 库(适用于 Flask 应用)
      • 5. 使用 `smtplib` 和 `email` 模块的结合(高级用法)
      • 6. 使用 `secure-smtplib`
      • 7. 使用 `mailgun` 库
      • 8. 使用 `sendgrid` 库
      • 9. 使用 `AWS SES` (Amazon Simple Email Service)
      • 10. 使用 `django.core.mail`(Django 项目)
      • 11. 使用 `EmailHandle`
      • 12. 使用 `exchangelib`
      • 13. 使用 `SimpleEmail`
      • 14. 使用 `outlook` 库
      • 15. 使用 `zoho-mail`

1. 使用 smtplib 模块

smtplib 是 Python 内置的一个模块,用于发送邮件。下面是一个使用 smtplib 发送简单文本邮件的示例:

import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart  def send_email_smtp():     sender_email = "your_email@example.com"     receiver_email = "receiver_email@example.com"     password = "your_password"      # 创建邮件对象     message = MIMEMultipart()     message["From"] = sender_email     message["To"] = receiver_email     message["Subject"] = "Test Email from smtplib"      # 邮件正文     body = "This is a test email sent from Python using smtplib."     message.attach(MIMEText(body, "plain"))      try:         # 连接到 SMTP 服务器         server = smtplib.SMTP("smtp.example.com", 587)         server.starttls()  # 启用安全传输         server.login(sender_email, password)          # 发送邮件         server.sendmail(sender_email, receiver_email, message.as_string())         print("Email sent successfully!")     except Exception as e:         print(f"Error: {      e}")     finally:         server.quit()  send_email_smtp() 

2. 使用 yagmail

yagmail 是一个简化的第三方库,用于发送电子邮件。它封装了 smtplib,使发送邮件更加容易。

首先,安装 yagmail 库:

pip install yagmail 

然后,使用以下代码发送邮件:

import yagmail  def send_email_yagmail():     sender_email = "your_email@example.com"     password = "your_password"     receiver_email = "receiver_email@example.com"      yag = yagmail.SMTP(user=sender_email, password=password)     subject = "Test Email from yagmail"     contents = "This is a test email sent from Python using yagmail."      try:         yag.send(to=receiver_email, subject=subject, contents=contents)         print("Email sent successfully!")     except Exception as e:         print(f"Error: {      e}")  send_email_yagmail() 

3. 使用 email 模块和 smtplib

email 模块提供了一个用于构建复杂邮件内容的接口,可以结合 smtplib 发送邮件。

import smtplib from email.message import EmailMessage  def send_email_email_module():     sender_email = "your_email@example.com"     receiver_email = "receiver_email@example.com"     password = "your_password"      # 创建邮件对象     message = EmailMessage()     message.set_content("This is a test email sent from Python using email module and smtplib.")     message["Subject"] = "Test Email"     message["From"] = sender_email     message["To"] = receiver_email      try:         # 连接到 SMTP 服务器         server = smtplib.SMTP("smtp.example.com", 587)         server.starttls()  # 启用安全传输         server.login(sender_email, password)          # 发送邮件         server.send_message(message)         print("Email sent successfully!")     except Exception as e:         print(f"Error: {      e}")     finally:         server.quit()  send_email_email_module() 

4. 使用 flask-mail 库(适用于 Flask 应用)

flask-mail 是一个 Flask 扩展,用于在 Flask 应用中发送电子邮件。

首先,安装 flask-mail

pip install Flask-Mail 

然后,使用以下代码在 Flask 应用中发送邮件:

from flask import Flask from flask_mail import Mail, Message  app = Flask(__name__) app.config['MAIL_SERVER'] = 'smtp.example.com' app.config['MAIL_PORT'] = 587 app.config

相关内容

热门资讯

裸辞做“一人公司”,我后悔了 去年这个时候,一位以色列程序员正在东南亚旅行。他顺手把一个在脑子里转了很久的想法做成了产品,一个让任...
南京建成国内首个Pre-6G试... 4月21日,2026全球6G技术与产业生态大会在南京开幕。全息互动技术展台前,一名远在北京的工作人员...
超梵求职受邀参加“2025抖音... 超梵求职受邀参加“2025抖音巨量引擎成人教育行业生态大会”,探讨分享优质内容传播,服务万千学员。 ...
摩托罗拉Razr 2026(R... IT之家 4 月 22 日消息,摩托罗拉宣布新一代 Razr 折叠手机将于 4 月 29 日在美国发...
库克卸任,特纳斯领航:苹果新纪... 苹果首席执行官蒂姆·库克将卸任,硬件工程主管约翰·特纳斯将接任,苹果公司今天宣布此事。 库克将在夏季...