SMTP协议简介:
SMTP(Simple Mail Transfer Protocol)是用于从源地址到目的地址传送邮件的一组规则,控制信件的中转方式。
Python发送邮件的基础:
使用
smtplib
库创建SMTP
对象,并使用sendmail方法发送邮件。SMTP
对象的创建语法:smtpObj = smtplib.SMTP([host [, port [, local_hostname]]])
。
sendmail方法的语法:SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])
。
简单的示例,演示如何使用 SMTP 发送邮件:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# 设置发件人、收件人和邮件内容
sender_email = "your_email@example.com"
receiver_email = "recipient@example.com"
password = "your_password"
subject = "Test Email"
body = "This is a test email sent from Python."
# 创建邮件对象
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
# 添加邮件内容
message.attach(MIMEText(body, "plain"))
# 使用SMTP服务器发送邮件
with smtplib.SMTP("smtp.example.com", 587) as server:
server.starttls()
server.login(sender_email, password)
text = message.as_string()
server.sendmail(sender_email, receiver_email, text)
print("Email sent successfully!")
发送HTML格式邮件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# 设置发件人、收件人和邮件内容
sender_email = "your_email@example.com"
receiver_email = "recipient@example.com"
password = "your_password"
subject = "HTML Email"
html_content = """
<html>
<head></head>
<body>
<p>This is a test email sent from Python.</p>
<p><a href="https://www.example.com">Click here</a> for more information.</p>
</body>
</html>
"""
# 创建邮件对象
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
# 添加 HTML 内容
message.attach(MIMEText(html_content, "html"))
# 使用SMTP服务器发送邮件
with smtplib.SMTP("smtp.example.com", 587) as server:
server.starttls()
server.login(sender_email, password)
text = message.as_string()
server.sendmail(sender_email, receiver_email, text)
print("HTML Email sent successfully!")
发送带附件的邮件
使用 MIMEMultipart 类来创建邮件对象,并使用 MIMEBase 类来处理附件。
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
# 设置发件人、收件人和邮件内容
sender_email = "your_email@example.com"
receiver_email = "recipient@example.com"
password = "your_password"
subject = "Email with Attachment"
body = "This is a test email with attachment sent from Python."
# 创建邮件对象
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
# 添加邮件正文
message.attach(MIMEText(body, "plain"))
# 添加附件
filename = "example_attachment.txt"
attachment = open(filename, "rb")
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",
)
message.attach(part)
# 使用SMTP服务器发送邮件
with smtplib.SMTP("smtp.example.com", 587) as server:
server.starttls()
server.login(sender_email, password)
text = message.as_string()
server.sendmail(sender_email, receiver_email, text)
print("Email with attachment sent successfully!")