您的位置:

Python SMTP:使用SMTP发送电子邮件的完整教程

通过Python可以很方便地使用SMTP连接到邮件服务器,发送邮件到指定的邮箱地址。在这里,我们将详细阐述如何使用Python SMTP发送电子邮件。

一、SMTP介绍

SMTP(简单邮件传输协议)是一种用于发送电子邮件的标准协议。它定义了电子邮件是如何传输到其他服务器和如何通过其他服务器进行传输的规则。

SMTP服务器通常是免费的,您可以在网上找到许多可供选择的SMTP服务器。当您需要使用SMTP服务器发送电子邮件时,您需要提供以下信息:

  • SMTP服务器地址
  • SMTP服务器的端口号
  • 发件人邮箱地址
  • 发件人邮箱的密码
  • 收件人邮箱地址
  • 邮件主题
  • 邮件正文

二、Python SMTP模块

在Python SMTP模块中,我们可以轻松地与SMTP服务器进行交互。Python通过SMTP和smtplib模块来提供SMTP客户端服务。该smtplib模块定义了SMTP类,该类有用于连接到SMTP服务器并在一个或多个步骤中进行电子邮件发送的方法。

下面是一个简单的Python程序,用于连接到SMTP服务器并发送电子邮件:

import smtplib

smtp_server = "smtp.gmail.com"
smtp_port = 587
username = "your_email_address"
password = "your_email_password"
recipient = "recipient_email_address"
subject = "Sample Subject"
message = "This is a sample message."

server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(username, password)
server.sendmail(username, recipient, "Subject: {}\n\n{}".format(subject, message))
server.quit()

三、发送HTML邮件

SMTP邮件可以包含格式化文本、图片以及链接。对于发送HTML邮件而言,SMTP模块也能胜任。下面是一个程序示例,用于连接到SMTP服务器并以HTML格式发送电子邮件:

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

smtp_server = "smtp.gmail.com"
smtp_port = 587
username = "your_email_address"
password = "your_email_password"
recipient = "recipient_email_address"
subject = "HTML Sample Subject"
message_body = "

HTML Sample Message

This is a sample HTML email message.

Here is a link to Google

" message = MIMEMultipart('alternative') message['From'] = username message['To'] = recipient message['Subject'] = subject message.attach(MIMEText(message_body, 'html')) server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() server.login(username, password) server.sendmail(username, recipient, message.as_string()) server.quit()

四、发送带附件的电子邮件

SMTP邮件还可以包含附件。要发送带附件的电子邮件,需要将附件添加到电子邮件消息中,然后将该消息发送到SMTP服务器。下面是一个程序示例,用于连接到SMTP服务器并发送带附件的电子邮件:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders

smtp_server = "smtp.gmail.com"
smtp_port = 587
username = "your_email_address"
password = "your_email_password"
recipient = "recipient_email_address"
subject = "Attachment Sample Subject"
message_body = "This is a sample message with an attachment."

message = MIMEMultipart()
message['From'] = username
message['To'] = recipient
message['Subject'] = subject
message.attach(MIMEText(message_body))

file_path = "path/to/attachment/file"
attachment = open(file_path, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % file_path.split("/")[-1])
message.attach(part)

server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(username, password)
server.sendmail(username, recipient, message.as_string())
server.quit()

五、结束语

Python SMTP模块提供了一个简单而强大的方法来给指定的邮箱地址发送电子邮件。不论你是需要发送包含文本、图片、超链接、附件的电子邮件,或是需要以HTML格式发送电子邮件,Python SMTP模块都能够胜任。希望这篇Python SMTP的完整教程能够对你有所帮助。