• Python邮件发送程序代码


    1. # -*- coding:utf-8 -*-
    2. # 2024-3-4
    3. # Pythonit教程网:blog.pythonit.cn
    4. # 云服务器地址:woaiyundun.cn
    5. # Python全栈课件视频教程:www.dqu.cc
    6. import smtplib
    7. from email.mime.text import MIMEText
    8. from email.mime.multipart import MIMEMultipart
    9. from email.mime.base import MIMEBase
    10. from email import encoders
    11. import os
    12. from email.header import Header
    13. class EmailSender:
    14. def __init__(self, sender_email, sender_password, smtp_server, smtp_port, sender_name=None):
    15. self.sender_email = sender_email
    16. self.sender_password = sender_password
    17. self.smtp_server = smtp_server
    18. self.smtp_port = smtp_port
    19. self.sender_name = sender_name # 添加这一行
    20. def send_email(self, receiver_email, subject, message, attachment_path=None, smtp_port=None):
    21. msg = MIMEMultipart()
    22. sender_header = Header(self.sender_name, 'utf-8') if self.sender_name else self.sender_email
    23. msg['From'] = sender_header
    24. msg['To'] = receiver_email
    25. msg['Subject'] = subject
    26. msg.attach(MIMEText(message, 'plain'))
    27. if attachment_path and os.path.exists(attachment_path):
    28. attachment = open(attachment_path, 'rb')
    29. part = MIMEBase('application', 'octet-stream')
    30. part.set_payload(attachment.read())
    31. encoders.encode_base64(part)
    32. part.add_header('Content-Disposition', f'attachment; filename= {os.path.basename(attachment_path)}')
    33. msg.attach(part)
    34. try:
    35. with smtplib.SMTP(self.smtp_server, self.smtp_port if not smtp_port else smtp_port) as server:
    36. server.starttls()
    37. server.login(self.sender_email, self.sender_password)
    38. server.sendmail(self.sender_email, receiver_email, msg.as_string())
    39. print("邮件发送成功!")
    40. except smtplib.SMTPException as e:
    41. print(f"发送邮件时出现错误:{e}")
    42. # 测试调用
    43. def main():
    44. sender_email = '你的邮箱地址' # 发送者邮箱
    45. sender_password = '你的邮箱密码' # smtp密码或者自建邮箱的邮箱密码
    46. smtp_server = 'smtp.example.com' # 邮箱服务器地址
    47. smtp_port = 587 # 根据你的邮箱服务商选择合适的端口,例如Gmail是587,QQ邮箱是465(SSL)或587(STARTTLS)
    48. sender_name = '你的名字' # 可选,发件人名字
    49. email_sender = EmailSender(sender_email, sender_password, smtp_server, smtp_port, sender_name=sender_name)
    50. email_sender.send_email('接收者邮箱地址', '邮件主题', '邮件内容', attachment_path='/path/to/attachment.txt')
    51. if __name__ == '__main__':
    52. main()
    1. 你需要将sender_emailsender_passwordsmtp_serversmtp_portsender_name替换为实际的值。
    2. 你需要将receiver_emailattachment_path替换为实际的接收者邮箱地址和附件路径(如果你想添加附件的话)。
    3. 根据你的邮箱服务商,smtp_port可能需要更改。例如,Gmail使用587端口,而QQ邮箱可以使用465(SSL)或587(STARTTLS)端口。
    4. 某些邮箱服务商可能需要你在账户设置中开启“不够安全的应用”的访问权限,或者使用应用专用密码(而非你的邮箱密码)作为sender_password
    5. 在真实环境中,不建议在代码中硬编码敏感信息(如邮箱密码),而应使用
  • 相关阅读:
    Vuex环境搭建
    TEM和CWEM的优缺点
    DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰二氧化硅颗粒用
    PHP 定时任务获取微信 access_token
    计算机视觉与深度学习-图像分割-视觉识别任务01-语义分割-【北邮鲁鹏】
    【JavaScript复习八】内置对象String和Math
    【独家专访】“数网”同防筑牢屏障——新型电力系统网络安全保障体系需加快调整
    吐槽嫌弃测试周期太长?开发自测一下
    Hadoop中SecondaryNameNode工作机制
    2023最全CTF入门指南(建议收藏,文章末尾有福利)
  • 原文地址:https://blog.csdn.net/bbos2004/article/details/136461114