如何使用python自动发邮件

安装PyEmail

pip install PyEmail

python代码

import smtplib
from email.mime.text import MIMEText

#这里的“smtp.example.com”应替换为您要使用的SMTP服务器的实际名称。
smtp_server = "smtp.example.com"

# 1. 登录到SMTP服务器
smtp_user = "your_email@example.com"
smtp_password = "your_password"
smtp_connection = smtplib.SMTP(smtp_server)
smtp_connection.login(smtp_user, smtp_password)


# 2. 编写电子邮件
email_body = "This is a test email sent using Python."
email_message = MIMEText(email_body)
email_message["Subject"] = "Test Email"
email_message["From"] = smtp_user #“smtp_user”应替换为您的实际电子邮件地址
email_message["To"] = "recipient@example.com" # “recipient@example.com”应替换为电子邮件的实际收件人。


# 3. 发送电子邮件
# 这里的“smtp_user”应替换为您的实际电子邮件地址,“recipient@example.com”应替换为电子邮件的实际收件人。
smtp_connection.sendmail(smtp_user, "recipient@example.com", email_message.as_string())


# 4. 关闭SMTP连接
# 发送电子邮件后,需要关闭与SMTP服务器的连接。
smtp_connection.quit()

常用的邮箱的SMTP服务器地址

  • 阿里云邮箱(mail.aliyun.com):smtp.aliyun.com(SSL加密端口:465;非加密端口:25)
  • 谷歌邮箱(google.com):smtp.gmail.com(SSL启用端口:587)
  • 新浪邮箱(sina.com): smtp.sina.com.cn(端口:25)
  • Tom邮箱(top.com):smtp.tom.com(端口:25)
  • 网易邮箱(163.com):smtp.163.com(端口:25)
  • 126邮箱:smtp.126.com(端口:25)
  • 雅虎邮箱(yahoo.com):smtp.mail.yahoo.com
  • Foxmail邮箱(foxmail.com):SMTP.foxmail.com(端口:25)
  • QQ邮箱(mail.qq.com):smtp.qq.com(端口:25)SMTP服务器需要身份验证。
  • 搜狐邮箱(sohu.com):smtp.sohu.com(端口:25)
  • HotMail邮箱(hotmail.com):SMTP服务器地smtp.live.com(端口:587)
  • 移动139邮箱: SMTP.139.com(端口:25)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

LucaTech

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

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

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

打赏作者

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

抵扣说明:

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

余额充值