0


汇总Python发邮件的15个常用方式(附代码)

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

文章目录

1. 使用

  1. smtplib

模块

  1. smtplib

是 Python 内置的一个模块,用于发送邮件。下面是一个使用

  1. smtplib

发送简单文本邮件的示例:

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

2. 使用

  1. yagmail

  1. yagmail

是一个简化的第三方库,用于发送电子邮件。它封装了

  1. smtplib

,使发送邮件更加容易。

首先,安装

  1. yagmail

库:

  1. pip install yagmail

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

  1. import yagmail
  2. defsend_email_yagmail():
  3. sender_email ="your_email@example.com"
  4. password ="your_password"
  5. receiver_email ="receiver_email@example.com"
  6. yag = yagmail.SMTP(user=sender_email, password=password)
  7. subject ="Test Email from yagmail"
  8. contents ="This is a test email sent from Python using yagmail."try:
  9. yag.send(to=receiver_email, subject=subject, contents=contents)print("Email sent successfully!")except Exception as e:print(f"Error: {
  10. e}")
  11. send_email_yagmail()

3. 使用

  1. email

模块和

  1. smtplib
  1. email

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

  1. smtplib

发送邮件。

  1. import smtplib
  2. from email.message import EmailMessage
  3. defsend_email_email_module():
  4. sender_email ="your_email@example.com"
  5. receiver_email ="receiver_email@example.com"
  6. password ="your_password"# 创建邮件对象
  7. message = EmailMessage()
  8. message.set_content("This is a test email sent from Python using email module and smtplib.")
  9. message["Subject"]="Test Email"
  10. message["From"]= sender_email
  11. message["To"]= receiver_email
  12. try:# 连接到 SMTP 服务器
  13. server = smtplib.SMTP("smtp.example.com",587)
  14. server.starttls()# 启用安全传输
  15. server.login(sender_email, password)# 发送邮件
  16. server.send_message(message)print("Email sent successfully!")except Exception as e:print(f"Error: {
  17. e}")finally:
  18. server.quit()
  19. send_email_email_module()

4. 使用

  1. flask-mail

库(适用于 Flask 应用)

  1. flask-mail

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

首先,安装

  1. flask-mail

  1. pip install Flask-Mail

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

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

本文转载自: https://blog.csdn.net/m0_57021623/article/details/139757692
版权归原作者 今晚务必早点睡 所有, 如有侵权,请联系我们删除。

“汇总Python发邮件的15个常用方式(附代码)”的评论:

还没有评论