当前位置:   article > 正文

Python SMTP发送邮件_python通过 587端口发送邮件

python通过 587端口发送邮件

如何使用Python发送QQ邮件?如何发送带附件的邮件?这篇文章将详细说明

目录

一、发送邮件

二、发送HTML格式的邮件

三、在HTML中添加图片

四、发送带附件的邮件

五、最终整合版

六、配置指引


一、发送邮件
  1. import smtplib
  2. from email.mime.text import MIMEText
  3. from email.mime.multipart import MIMEMultipart
  4. # 邮件服务器配置
  5. smtp_server = 'smtp.qq.com'
  6. smtp_port = 587 # QQ邮箱的端口号为587
  7. # 发送方邮箱账号和密码
  8. sender_email = 'your_sender_email@qq.com'
  9. sender_password = 'your_sender_email_password'
  10. # 接收方邮箱地址
  11. receiver_email = 'receiver_email@qq.com'
  12. # 创建邮件内容
  13. subject = 'Python SMTP 邮件测试'
  14. body = '这是一封使用Python发送的测试邮件。'
  15. message = MIMEMultipart()
  16. message['From'] = sender_email
  17. message['To'] = receiver_email
  18. message['Subject'] = subject
  19. # 添加邮件正文
  20. # MIMEText有三个参数第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码可不填
  21. message.attach(MIMEText(body, 'plain'))
  22. # 发送邮件
  23. try:
  24. smtp = smtplib.SMTP(smtp_server, smtp_port)
  25. smtp.starttls() # 开启TLS加密连接
  26. smtp.login(sender_email, sender_password)
  27. smtp.sendmail(sender_email, receiver_email, message.as_string())
  28. print("邮件发送成功!")
  29. except smtplib.SMTPException as e:
  30. print("邮件发送失败:", e)
  31. finally:
  32. smtp.quit()

二、发送HTML格式的邮件
  1. import smtplib
  2. from email.mime.text import MIMEText
  3. from email.mime.multipart import MIMEMultipart
  4. # 邮件服务器配置
  5. smtp_server = 'smtp.qq.com'
  6. smtp_port = 587 # QQ邮箱的端口号为587
  7. # 发送方邮箱账号和密码
  8. sender_email = 'your_sender_email@qq.com'
  9. sender_password = 'your_sender_email_password'
  10. # 接收方邮箱地址
  11. receiver_email = 'receiver_email@qq.com'
  12. # 创建邮件内容
  13. subject = 'Python SMTP 邮件测试'
  14. body = body = """<p>Python 邮件发送测试...</p>
  15. <p><a href="http://www.baidu.com">这是一个链接</a></p>"""
  16. message = MIMEMultipart()
  17. message['From'] = sender_email
  18. message['To'] = receiver_email
  19. message['Subject'] = subject
  20. # 添加邮件正文
  21. # MIMEText有三个参数第一个为文本内容,第二个 html设置文本格式,第三个 utf-8 设置编码可不填
  22. message.attach(MIMEText(body, 'html'))
  23. # 发送邮件
  24. try:
  25. smtp = smtplib.SMTP(smtp_server, smtp_port)
  26. smtp.starttls() # 开启TLS加密连接
  27. smtp.login(sender_email, sender_password)
  28. smtp.sendmail(sender_email, receiver_email, message.as_string())
  29. print("邮件发送成功!")
  30. except smtplib.SMTPException as e:
  31. print("邮件发送失败:", e)
  32. finally:
  33. smtp.quit()

三、在HTML中添加图片
  1. import smtplib
  2. from email.mime.text import MIMEText
  3. from email.mime.multipart import MIMEMultipart
  4. from email.mime.image import MIMEImage
  5. # 邮件服务器配置
  6. smtp_server = 'smtp.qq.com'
  7. smtp_port = 587 # QQ邮箱的端口号为587
  8. # 发送方邮箱账号和密码
  9. sender_email = 'your_sender_email@qq.com'
  10. sender_password = 'your_sender_email_password'
  11. # 接收方邮箱地址
  12. receiver_email = 'receiver_email@qq.com'
  13. # 创建邮件内容
  14. subject = 'Python SMTP 邮件测试'
  15. body = """<p>Python 邮件发送测试...</p>
  16. <p><a href="http://www.baidu.com">这是一个链接</a></p>
  17. <p>图片演示:</p>
  18. <p><img decoding="async" src="cid:image1"></p>"""
  19. message = MIMEMultipart()
  20. message['From'] = sender_email
  21. message['To'] = receiver_email
  22. message['Subject'] = subject
  23. # 添加邮件正文
  24. message.attach(MIMEText(body, 'html'))
  25. # 指定图片为当前目录
  26. fp = open('E:\demo\head_image.png', 'rb')
  27. msgImage = MIMEImage(fp.read())
  28. fp.close()
  29. # 定义图片 ID,在 HTML 文本中引用
  30. msgImage.add_header('Content-ID', '<image1>')
  31. message.attach(msgImage)
  32. # 发送邮件
  33. try:
  34. smtp = smtplib.SMTP(smtp_server, smtp_port)
  35. smtp.starttls() # 开启TLS加密连接
  36. smtp.login(sender_email, sender_password)
  37. smtp.sendmail(sender_email, receiver_email, message.as_string())
  38. print("邮件发送成功!")
  39. except smtplib.SMTPException as e:
  40. print("邮件发送失败:", e)
  41. finally:
  42. smtp.quit()

四、发送带附件的邮件
  1. import smtplib
  2. from email.mime.text import MIMEText
  3. from email.mime.multipart import MIMEMultipart
  4. from email.mime.application import MIMEApplication
  5. from email.mime.image import MIMEImage
  6. # 邮件服务器配置
  7. smtp_server = 'smtp.qq.com'
  8. smtp_port = 587 # QQ邮箱的端口号为587
  9. # 发送方邮箱账号和密码
  10. sender_email = 'your_sender_email@qq.com'
  11. sender_password = 'your_sender_email_password'
  12. # 接收方邮箱地址
  13. receiver_email = 'receiver_email@qq.com'
  14. # 创建邮件内容
  15. subject = 'Python SMTP 邮件测试'
  16. body = """<p>Python 邮件发送测试...</p>
  17. <p><a href="http://www.baidu.com">这是一个链接</a></p>"""
  18. message = MIMEMultipart()
  19. message['From'] = sender_email
  20. message['To'] = receiver_email
  21. message['Subject'] = subject
  22. # 添加邮件正文
  23. message.attach(MIMEText(body, 'html'))
  24. # 添加附件
  25. import os
  26. file_path = 'E:\\demo\\《Python+Cookbook》.pdf'
  27. file_name = os.path.basename(file_path) # 只获取文件名
  28. with open(file_path, 'rb') as file:
  29. part = MIMEApplication(file.read(), Name=file_name) # Name参数指定了附件的文件名
  30. part['Content-Disposition'] = f'attachment; filename="{file_path}"'
  31. message.attach(part)
  32. # 发送邮件
  33. try:
  34. smtp = smtplib.SMTP(smtp_server, smtp_port)
  35. smtp.starttls() # 开启TLS加密连接
  36. smtp.login(sender_email, sender_password)
  37. smtp.sendmail(sender_email, receiver_email, message.as_string())
  38. print("邮件发送成功!")
  39. except smtplib.SMTPException as e:
  40. print("邮件发送失败:", e)
  41. finally:
  42. smtp.quit()

五、最终整合版
  1. import smtplib
  2. from email.mime.text import MIMEText
  3. from email.mime.multipart import MIMEMultipart
  4. from email.mime.application import MIMEApplication
  5. from email.mime.image import MIMEImage
  6. # 邮件服务器配置
  7. smtp_server = 'smtp.qq.com'
  8. smtp_port = 587 # QQ邮箱的端口号为587
  9. # 发送方邮箱账号和密码
  10. sender_email = 'your_sender_email@qq.com'
  11. sender_password = 'your_sender_email_password'
  12. # 接收方邮箱地址
  13. receiver_email = 'receiver_email@qq.com'
  14. # 创建邮件内容
  15. subject = 'Python SMTP 邮件测试'
  16. body = """<p>Python 邮件发送测试...</p>
  17. <p><a href="http://www.baidu.com">这是一个链接</a></p>
  18. <p>图片演示:</p>
  19. <p><img decoding="async" src="cid:image1"></p>"""
  20. message = MIMEMultipart()
  21. message['From'] = sender_email
  22. message['To'] = receiver_email
  23. message['Subject'] = subject
  24. # 添加邮件正文
  25. message.attach(MIMEText(body, 'html'))
  26. # 指定图片为当前目录
  27. fp = open('E:\demo\head_image.png', 'rb')
  28. msgImage = MIMEImage(fp.read())
  29. fp.close()
  30. # 定义图片 ID,在 HTML 文本中引用
  31. msgImage.add_header('Content-ID', '<image1>')
  32. message.attach(msgImage)
  33. # 添加附件
  34. file_path = 'E:\\demo\\《Python+Cookbook》.pdf'
  35. file_name = os.path.basename(file_path) # 只获取文件名
  36. with open(file_path, 'rb') as file:
  37. part = MIMEApplication(file.read(), Name=file_name)
  38. part['Content-Disposition'] = f'attachment; filename="{file_path}"'
  39. message.attach(part)
  40. # 发送邮件
  41. try:
  42. smtp = smtplib.SMTP(smtp_server, smtp_port)
  43. smtp.starttls() # 开启TLS加密连接
  44. smtp.login(sender_email, sender_password)
  45. smtp.sendmail(sender_email, receiver_email, message.as_string())
  46. print("邮件发送成功!")
  47. except smtplib.SMTPException as e:
  48. print("邮件发送失败:", e)
  49. finally:
  50. smtp.quit()
六、配置指引

您需要将 your_sender_email@qq.com your_sender_email_password 替换为实际的发件人邮箱账号和密码,将 receiver_email@qq.com 替换为收件人的 QQ 邮箱地址。同时,确保开启了发件人邮箱的 SMTP 服务

其中your_sender_email_password 的密码应当在QQ邮箱设置--帐户中找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务,点击管理服务,在跳转的新页面中点击生成授权码,这将作为你的密码使用

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/494076
推荐阅读
相关标签
  

闽ICP备14008679号