Python SMTP发送邮件的13个实例与常见错误解决办法


【蜂邮EDM】:邮件群发系统,EDM邮件营销平台,邮件代发服务。 查看价格
【AokSend邮件API】:触发式邮件API,15元/万封,99%送达率。 查看价格
【烽火邮箱】:新人领取免费域名邮箱,可用作企业邮箱公司邮箱。 查看价格
Python SMTP发送邮件的13个实例与常见错误解决办法
Python SMTP发送邮件的基本介绍
Python SMTP发送邮件是通过Python的smtplib库实现的,该库提供了与SMTP服务器的通信接口,支持邮件的发送。通过SMTP协议,Python能够帮助开发者轻松实现邮件的自动化发送。
Python SMTP发送邮件的13个实例
下面将通过13个实例,展示如何使用Python的smtplib库发送邮件。
1. 发送简单的文本邮件
最简单的邮件发送方式是发送纯文本邮件,代码如下:
import smtplibfrom email.mime.text import MIMETextmsg = MIMEText("这是一封测试邮件")msg['From'] = "your_email@example.com"msg['To'] = "recipient_email@example.com"msg['Subject'] = "测试邮件"server = smtplib.SMTP('smtp.example.com')server.login("your_email@example.com", "your_password")server.sendmail("your_email@example.com", "recipient_email@example.com", msg.as_string())server.quit()
2. 发送带附件的邮件
要发送带附件的邮件,可以使用email库中的MIME模块,下面是代码示例:
from email.mime.multipart import MIMEMultipartfrom email.mime.base import MIMEBasefrom email import encodersmsg = MIMEMultipart()msg['From'] = "your_email@example.com"msg['To'] = "recipient_email@example.com"msg['Subject'] = "带附件的邮件"# 添加附件filename = "file.txt"attachment = open("file.txt", "rb")part = MIMEBase('application', 'octet-stream')part.set_payload(attachment.read())encoders.encode_base64(part)part.add_header('Content-Disposition', f"attachment; filename={filename}")msg.attach(part)server = smtplib.SMTP('smtp.example.com')server.login("your_email@example.com", "your_password")server.sendmail("your_email@example.com", "recipient_email@example.com", msg.as_string())server.quit()
3. 发送HTML邮件
发送HTML邮件可以使用MIMEText并指定邮件内容的类型为HTML:
Powered By 蜂.邮.EDMfrom email.mime.text import MIMETextmsg = MIMEText("HTML邮件
这是一封HTML格式的邮件
", "html")msg['From'] = "your_email@example.com"msg['To'] = "recipient_email@example.com"msg['Subject'] = "HTML邮件"server = smtplib.SMTP('smtp.example.com')server.login("your_email@example.com", "your_password")server.sendmail("your_email@example.com", "recipient_email@example.com", msg.as_string())server.quit()
4. 发送带多个收件人的邮件
如果您想一次发送邮件给多个收件人,可以在“To”字段中指定多个邮箱地址:
msg['To'] = "recipient1@example.com, recipient2@example.com"
5. 发送邮件并抄送给其他人
除了主要收件人,还可以使用“CC”字段抄送给其他人:
msg['Cc'] = "cc@example.com"
6. 发送带有主题的邮件
邮件的主题通过设置“Subject”字段来指定:
msg['Subject'] = "邮件主题"
7. 发送邮件并加密
如果需要加密邮件内容,可以使用SSL连接到SMTP服务器:
server = smtplib.SMTP_SSL('smtp.example.com', 465)
8. 发送批量邮件
通过循环,可以批量发送邮件给多个用户:
🔔🔔🔔 【烽火邮箱】:烽火邮箱是一款简洁高效的企业邮箱平台,新客户赠送免费企业邮箱,一个起卖、按月付费(低至9.9元);支持别名邮箱及群组邮箱,支持定制无限邮箱。高权重纯净IP池,系统自带反垃圾机制。
立即查看 >> :企业邮箱价格
【蜂邮EDM】:邮件群发系统,EDM邮件营销平台,邮件代发服务,专业研发定制邮件营销系统及邮件群发解决方案!蜂邮自研产品线主要分为标准版、外贸版、企业版、定制版,及邮件API邮件SMTP接口服务。
立即查看 >> :邮件发送价格
【AokSend邮件API】:专注触发式邮件API发送服务。15元/万封,发送验证码邮件、忘记密码邮件、通知告警邮件等,不限速。综合送达率99%、进箱率98%。触发邮件也叫事务性邮件或推送邮件,包含:验证码邮件、重置密码邮件、余额提醒邮件、会员到期邮件、账号认证邮件等!
立即查看 >> :邮件发送价格
for recipient in recipient_list:server.sendmail("your_email@example.com", recipient, msg.as_string())
9. 发送邮件时处理错误
在发送邮件时,我们可以使用try-except块来处理SMTP错误:
try:server.sendmail("your_email@example.com", "recipient_email@example.com", msg.as_string())except smtplib.SMTPException as e:print(f"Error sending mail: {e}")
10. 发送带有图片的邮件
如果邮件中需要包含图片,可以将图片嵌入HTML邮件中:
html = "
"msg = MIMEText(html, 'html')msg.add_header('Content-ID', '')
11. 发送带有链接的邮件
HTML邮件可以包含链接:
from email.mime.multipart import MIMEMultipartfrom email.mime.base import MIMEBasefrom email import encodersmsg = MIMEMultipart()msg['From'] = "your_email@example.com"msg['To'] = "recipient_email@example.com"msg['Subject'] = "带附件的邮件"# 添加附件filename = "file.txt"attachment = open("file.txt", "rb")part = MIMEBase('application', 'octet-stream')part.set_payload(attachment.read())encoders.encode_base64(part)part.add_header('Content-Disposition', f"attachment; filename={filename}")msg.attach(part)server = smtplib.SMTP('smtp.example.com')server.login("your_email@example.com", "your_password")server.sendmail("your_email@example.com", "recipient_email@example.com", msg.as_string())server.quit()
0
12. 使用代理发送邮件
如果您的网络需要使用代理,您可以通过设置代理来发送邮件:
from email.mime.multipart import MIMEMultipartfrom email.mime.base import MIMEBasefrom email import encodersmsg = MIMEMultipart()msg['From'] = "your_email@example.com"msg['To'] = "recipient_email@example.com"msg['Subject'] = "带附件的邮件"# 添加附件filename = "file.txt"attachment = open("file.txt", "rb")part = MIMEBase('application', 'octet-stream')part.set_payload(attachment.read())encoders.encode_base64(part)part.add_header('Content-Disposition', f"attachment; filename={filename}")msg.attach(part)server = smtplib.SMTP('smtp.example.com')server.login("your_email@example.com", "your_password")server.sendmail("your_email@example.com", "recipient_email@example.com", msg.as_string())server.quit()
1
13. 发送邮件时指定发送时间
邮件发送的时间可以通过任务调度来控制,使用定时任务工具(如Cron)来自动化邮件发送。
常见错误与解决办法
在使用Python的SMTP功能时,您可能会遇到一些常见的错误。下面是几种常见错误及其解决方法:
1. SMTPAuthenticationError
这个错误通常表示您的邮箱用户名或密码错误,检查用户名和密码是否正确,并确保SMTP服务器支持身份验证。
2. SMTPConnectError
如果您无法连接到SMTP服务器,可能是因为服务器地址或端口号错误,或网络问题。
3. SMTPRecipientsRefused
这个错误通常是由于收件人邮箱地址错误导致的,检查收件人地址的格式是否正确。
总结
通过以上的13个实例,您可以轻松使用Python的smtplib库发送不同类型的邮件。若遇到问题,检查SMTP服务器设置、邮件内容格式或使用合适的错误处理机制。对于需要批量发送邮件的用户,可以选择MailBing(https://www.mailbing.com/)等工具来提升邮件发送效率。


【蜂邮EDM】:邮件群发系统,EDM邮件营销平台,邮件代发服务。 查看价格
【AokSend邮件API】:触发式邮件API,15元/万封,99%送达率。 查看价格
【烽火邮箱】:新人领取免费域名邮箱,可用作企业邮箱公司邮箱。 查看价格