当前位置:Gxlcms > Python > Python邮件如何发送?

Python邮件如何发送?

时间:2021-07-01 10:21:17 帮助过:13人阅读

import smtplib
from email.mime.text import MIMEText
_user = "1147016115@qq.com"#发件人
_pwd = "wcpxldrtuthagjbc"#qq邮箱授权码
_to = "1208832227@qq.com"#收件人

msg = MIMEText("Hellow,This is my first Email!")#邮件内容
msg["Subject"] = "come form xieolei!"#收件方显示的邮件主题
msg["From"] = _user#收件方显示的发件人
msg["To"] = _to#收件方显示的收件人

try:
  s = smtplib.SMTP_SSL("smtp.qq.com", 465)
  s.login(_user, _pwd)
  s.sendmail(_user, _to, msg.as_string())
  s.quit()

  print ("Success!")

except smtplib.SMTPException.e:

  print ("Falied,%s"%e)

#######运行结果:###############

附:代码源码:

import smtplibfrom email.mime.text import MIMEText
_user = "1147016165@qq.com"#发件人_pwd = "hhvzrkmhnrfgicee"#qq邮箱授权码_to = "1208832367@qq.com"#收件人msg = MIMEText("Hellow,This is my first Email!")#邮件内容msg["Subject"] = "come form xieolei!"#收件方显示的邮件主题msg["From"] = _user#收件方显示的发件人msg["To"] = _to#收件方显示的收件人try:
     s = smtplib.SMTP_SSL("smtp.qq.com", 465)
     s.login(_user, _pwd)
     s.sendmail(_user, _to, msg.as_string())
     s.quit()     print ("Success!")except smtplib.SMTPException.e:     print ("Falied,%s"%e)
View Code

获取QQ授权码的方法:

进入个人QQ邮箱》设置》账户

图一:如图所示打开IMAP/SMTP

图二:生成授权码

以上就是Python邮件如何发送?的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行