使用Python SMTP Server轻松发送邮件

一、使用Python SMTP Server发送邮件的优势

Python提供了一个简单而强大的SMTP模块,方便开发者使用Python代码发送邮件。使用Python SMTP Server发送邮件的优势主要有以下几点:

1、Python SMTP Server模块提供了许多高级功能,例如SSL支持、身份验证和多个收件人。

2、使用Python SMTP Server发送邮件比使用其他邮件客户端发送邮件更方便和安全。

3、Python SMTP Server对于邮件服务器错误的处理能力很强,让开发者可以轻松处理邮件发送失败、服务器不可用和其他网络问题。

二、Python SMTP Server发送简单的文本邮件

下面是一个使用Python SMTP Server发送简单文本邮件的代码示例:

import smtplib

# 发送人邮箱
sender_email = "sender@example.com"
# 接收人邮箱
receiver_email = "receiver@example.com"

# SMTP服务器
smtp_server = "smtp.example.com"
smtp_port = 587

# 发送人邮箱及密码
username = "sender@example.com"
password = "password"

# 邮件内容
message = """\
Subject: Hello, World!

This is a plain text email from Python SMTP Server.
"""

# SSL加密方式
context = ssl.create_default_context()

# 发送邮件
with smtplib.SMTP(smtp_server, smtp_port) as server:
    server.starttls(context=context)
    server.login(username, password)
    server.sendmail(sender_email, receiver_email, message)

在代码中,我们首先将SMTP服务器的地址、端口号、邮箱和密码等信息设置好,并将要发送的邮件内容填写好,之后使用SMTP模块的SMTP()方法连接上SMTP服务器,并使用starttls()方法开启SSL加密。

最后使用login()方法登录SMTP服务器,使用sendmail()方法将邮件发送给指定收件人。其中,sendmail()方法的三个参数分别为发送人邮箱、接收人邮箱以及邮件内容。

三、Python SMTP Server发送带附件邮件

下面是一个使用Python SMTP Server发送带附件邮件的代码示例:

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

# 发送人邮箱
sender_email = "sender@example.com"
# 接收人邮箱
receiver_email = "receiver@example.com"

# SMTP服务器
smtp_server = "smtp.example.com"
smtp_port = 587

# 发送人邮箱及密码
username = "sender@example.com"
password = "password"

# 邮件内容
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = "Python SMTP Server with Attachment"

# 添加正文
message.attach(MIMEText("This is a plain text email with attachment from Python SMTP Server."))

# 添加附件
with open("example.jpg", "rb") as attachment:
    part = MIMEApplication(attachment.read(), _subtype="jpg")
    part.add_header('Content-Disposition', "attachment", filename="example.jpg")
    message.attach(part)

# SSL加密方式
context = ssl.create_default_context()

# 发送邮件
with smtplib.SMTP(smtp_server, smtp_port) as server:
    server.starttls(context=context)
    server.login(username, password)
    server.sendmail(sender_email, receiver_email, message.as_string())

我们可以在创建邮件内容message的时候,使用MIMEMultipart()方法创建一个带有多个部分的邮件。其中一部分是正文,另一部分是附件。我们使用MIMEText()方法将正文内容添加到邮件内容中,使用MIMEApplication()方法添加附件并指定附件的文件名,之后使用add_header()方法指定附件的内容。

在发送邮件的时候,我们使用了as_string()方法将邮件内容转换为字符串形式,并使用sendmail()方法发送邮件。

四、Python SMTP Server发送HTML邮件

下面是一个使用Python SMTP Server发送HTML邮件的代码示例:

import smtplib
import ssl
from email.mime.text import MIMEText

# 发送人邮箱
sender_email = "sender@example.com"
# 接收人邮箱
receiver_email = "receiver@example.com"

# SMTP服务器
smtp_server = "smtp.example.com"
smtp_port = 587

# 发送人邮箱及密码
username = "sender@example.com"
password = "password"

# 邮件内容
message = MIMEText("

Hello, World!

This is a HTML email from Python SMTP Server.

", "html") message["From"] = sender_email message["To"] = receiver_email message["Subject"] = "Python SMTP Server with HTML" # SSL加密方式 context = ssl.create_default_context() # 发送邮件 with smtplib.SMTP(smtp_server, smtp_port) as server: server.starttls(context=context) server.login(username, password) server.sendmail(sender_email, receiver_email, message.as_string())

在这个代码示例中,我们使用了MIMEText()方法创建邮件内容。我们将邮件内容目标类型设置为html,并在邮件内容中嵌入HTML标记。在最后发送邮件时,我们也是使用as_string()方法将邮件内容转换为字符串形式。

五、Python SMTP Server发送带有图片的HTML邮件

下面是一个使用Python SMTP Server发送带有图片的HTML邮件的代码示例:

import smtplib
import ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage

# 发送人邮箱
sender_email = "sender@example.com"
# 接收人邮箱
receiver_email = "receiver@example.com"

# SMTP服务器
smtp_server = "smtp.example.com"
smtp_port = 587

# 发送人邮箱及密码
username = "sender@example.com"
password = "password"

# 邮件内容
message = MIMEMultipart("related")
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = "Python SMTP Server with Image"

# 添加HTML内容
body = """
    

Hello, World!

This is a HTML email with image from Python SMTP Server.

""" message.attach(MIMEText(body, "html")) # 添加图片 with open("example.jpg", "rb") as image: img = MIMEImage(image.read()) img.add_header('Content-ID', '') message.attach(img) # SSL加密方式 context = ssl.create_default_context() # 发送邮件 with smtplib.SMTP(smtp_server, smtp_port) as server: server.starttls(context=context) server.login(username, password) server.sendmail(sender_email, receiver_email, message.as_string())

在这个代码示例中,我们使用了MIMEMultipart()方法创建邮件内容,将多个邮件正文及图片组合在一起。我们使用MIMEText()方法将邮件正文指定为HTML格式,并在邮件内容中嵌入<img>标记以显示图片。

在添加图片img时,我们使用了add_header()方法指定了图片的Content-ID。相应地,在嵌入图片的<img>标记中,我们使用了cid:协议方案指定了图片的Content-ID,达到了在HTML邮件中嵌入图片的效果。

六、总结

通过本篇文章的介绍,我们可以看到Python SMTP Server提供了非常强大的功能,只需要几行Python代码即可轻松发送各种类型的邮件。使用Python SMTP Server不仅方便,而且也更加安全和稳定,可以满足开发者对邮件发送的各种要求。

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/237965.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-12 12:08
下一篇 2024-12-12 12:08

相关推荐

  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29

发表回复

登录后才能评论