Python SMTP发送邮件

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!")
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬砖的诗人Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值