如何用Python编写代码发送QQ邮箱邮件?

2026-05-29 04:151阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计577个文字,预计阅读时间需要3分钟。

如何用Python编写代码发送QQ邮箱邮件?

本示例为大家分享了如何使用Python实现QQ邮箱发送邮件的完整代码示例,供大家参考。

pythonimport smtplibfrom email.mime.text import MIMETextfrom email.header import Header

如何用Python编写代码发送QQ邮箱邮件?

def send_email(receiver, subject, content): # QQ邮箱SMTP服务器地址 smtp_server='smtp.qq.com' # 发件人邮箱用户名和密码(授权码) sender='your_email@qq.com' password='your_authorization_code'

# 创建一个MIMEText对象,用于设置邮件内容 message=MIMEText(content, 'plain', 'utf-8') message['From']=Header(发件人昵称, 'utf-8') message['To']=Header(收件人昵称, 'utf-8') message['Subject']=Header(subject, 'utf-8')

try: # 创建SMTP对象 server=smtplib.SMTP_SSL(smtp_server, 465) server.login(sender, password) server.sendmail(sender, [receiver], message.as_string()) print(邮件发送成功) except smtplib.SMTPException as e: print(邮件发送失败:, e) finally: server.quit()

使用示例send_email('receiver@example.com', '邮件', '邮件内容')

本文实例为大家分享了python实现QQ邮箱发送邮件的具体代码,供大家参考,具体内容如下

1、代码:

#!/usr/bin/python # -*- coding: utf-8 -*- import smtplib from email.mime.text import MIMEText from email.header import Header mail_host="smtp.qq.com"#设置的邮件服务器host必须是发送邮箱的服务器,与接收邮箱无关。 mail_user="**************"#qq邮箱登陆名 mail_pass="*****************" #开启stmp服务的时候并设置的授权码,注意!不是QQ密码。 sender='xxx@qq.com'#发送方qq邮箱 receivers=['xxx@qq.com']#接收方qq邮箱 message=MIMEText('测试发送 python 邮件','plain','utf-8') message['From']=Header("beibei",'utf-8') #设置显示在邮件里的发件人 message['To']=Header("wowo",'utf-8') #设置显示在邮件里的收件人 subject ='python smtp email test' message['Subject']=Header(subject,'utf-8') #设置主题和格式 try: smtpobj=smtplib.SMTP_SSL(mail_host,465) #本地如果有本地服务器,则用localhost ,默认端口25,腾讯的(端口465或587) smtpobj.set_debuglevel(1) smtpobj.login(mail_user,mail_pass)#登陆QQ邮箱服务器 smtpobj.sendmail(sender,receivers,message.as_string())#发送邮件 print("邮件发送成功") smtpobj.quit()#退出 except smtplib.SMTPException as e : print("Error:无法发送邮件") print(e)

2、执行结果:

3、遇到问题:

3.1、端口错误 SMTP 默认端口是25 ,但QQ邮箱不是

smtplib.SMTP_SSL(mail_host,465)

要用SMTP_SSL

3.2、发送被拒绝连接 535, b'Error:

3.3、获取授权码:

会在此处显示授权码,记下来。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计577个文字,预计阅读时间需要3分钟。

如何用Python编写代码发送QQ邮箱邮件?

本示例为大家分享了如何使用Python实现QQ邮箱发送邮件的完整代码示例,供大家参考。

pythonimport smtplibfrom email.mime.text import MIMETextfrom email.header import Header

如何用Python编写代码发送QQ邮箱邮件?

def send_email(receiver, subject, content): # QQ邮箱SMTP服务器地址 smtp_server='smtp.qq.com' # 发件人邮箱用户名和密码(授权码) sender='your_email@qq.com' password='your_authorization_code'

# 创建一个MIMEText对象,用于设置邮件内容 message=MIMEText(content, 'plain', 'utf-8') message['From']=Header(发件人昵称, 'utf-8') message['To']=Header(收件人昵称, 'utf-8') message['Subject']=Header(subject, 'utf-8')

try: # 创建SMTP对象 server=smtplib.SMTP_SSL(smtp_server, 465) server.login(sender, password) server.sendmail(sender, [receiver], message.as_string()) print(邮件发送成功) except smtplib.SMTPException as e: print(邮件发送失败:, e) finally: server.quit()

使用示例send_email('receiver@example.com', '邮件', '邮件内容')

本文实例为大家分享了python实现QQ邮箱发送邮件的具体代码,供大家参考,具体内容如下

1、代码:

#!/usr/bin/python # -*- coding: utf-8 -*- import smtplib from email.mime.text import MIMEText from email.header import Header mail_host="smtp.qq.com"#设置的邮件服务器host必须是发送邮箱的服务器,与接收邮箱无关。 mail_user="**************"#qq邮箱登陆名 mail_pass="*****************" #开启stmp服务的时候并设置的授权码,注意!不是QQ密码。 sender='xxx@qq.com'#发送方qq邮箱 receivers=['xxx@qq.com']#接收方qq邮箱 message=MIMEText('测试发送 python 邮件','plain','utf-8') message['From']=Header("beibei",'utf-8') #设置显示在邮件里的发件人 message['To']=Header("wowo",'utf-8') #设置显示在邮件里的收件人 subject ='python smtp email test' message['Subject']=Header(subject,'utf-8') #设置主题和格式 try: smtpobj=smtplib.SMTP_SSL(mail_host,465) #本地如果有本地服务器,则用localhost ,默认端口25,腾讯的(端口465或587) smtpobj.set_debuglevel(1) smtpobj.login(mail_user,mail_pass)#登陆QQ邮箱服务器 smtpobj.sendmail(sender,receivers,message.as_string())#发送邮件 print("邮件发送成功") smtpobj.quit()#退出 except smtplib.SMTPException as e : print("Error:无法发送邮件") print(e)

2、执行结果:

3、遇到问题:

3.1、端口错误 SMTP 默认端口是25 ,但QQ邮箱不是

smtplib.SMTP_SSL(mail_host,465)

要用SMTP_SSL

3.2、发送被拒绝连接 535, b'Error:

3.3、获取授权码:

会在此处显示授权码,记下来。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。