python邮件自动发送怎么实现

24次阅读
没有评论

共计 991 个字符,预计需要花费 3 分钟才能阅读完成。

要实现 Python 邮件自动发送,你可以使用 Python 的内置模块 smtplibemail。下面是一个简单的示例代码:

import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 发件人邮箱 
sender = 'your_email@example.com'
# 收件人邮箱 
receiver = 'recipient_email@example.com'
# SMTP 服务器地址 
smtp_server = 'smtp.example.com'
# 发件人邮箱密码(注意不是登录密码,而是 SMTP 服务器的授权码)
password = 'your_password'

# 创建一个带有内容的邮件对象 
message = MIMEText(' 邮件内容 ', 'plain', 'utf-8')
message['From'] = Header(' 发件人姓名 ', 'utf-8')
message['To'] = Header(' 收件人姓名 ', 'utf-8')
message['Subject'] = Header(' 邮件主题 ', 'utf-8')

# 使用 SMTP 服务器登录并发送邮件 
try:
    server = smtplib.SMTP(smtp_server)
    server.login(sender, password)
    server.sendmail(sender, receiver, message.as_string())
    print(' 邮件发送成功 ')
except Exception as e:
    print(' 邮件发送失败:', str(e))
finally:
    server.quit()

需要替换示例代码中的发件人邮箱、收件人邮箱、SMTP 服务器地址和发件人邮箱密码。注意,有些邮箱服务提供商可能需要使用 SSL 或 TLS 加密方式连接 SMTP 服务器,你可以根据需要调整代码来适应特定的邮箱服务。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-16发表,共计991字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)