如何使用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 = "[email protected]"
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"] = "[email protected]" # “[email protected]”应替换为电子邮件的实际收件人。


# 3. 发送电子邮件
# 这里的“smtp_user”应替换为您的实际电子邮件地址,“[email protected]”应替换为电子邮件的实际收件人。
smtp_connection.sendmail(smtp_user, "[email protected]", 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)

猜你喜欢

转载自blog.csdn.net/xili1342/article/details/129685512