如何用Python编写发送带附件的QQ邮件脚本?

2026-04-20 06:181阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

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

如何用Python编写发送带附件的QQ邮件脚本?

本例展示了如何使用Python发送QQ邮箱邮件。以下是一个简单的示例代码,用于发送邮件:

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

如何用Python编写发送带附件的QQ邮件脚本?

邮箱用户名和密码sender='your_qq_email@qq.com'password='your_password'

接收者邮箱receiver='receiver_email@example.com'

邮件内容mail_content='这是一封来自Python的测试邮件。'

邮件主题mail_title='Python发送邮件测试'

创建MIMEText对象,设置邮件内容message=MIMEText(mail_content, 'plain', 'utf-8')message['From']=Header(sender, 'utf-8')message['To']=Header(receiver, 'utf-8')message['Subject']=Header(mail_title, 'utf-8')

SMTP服务器设置smtp_server='smtp.qq.com'smtp_port=465

连接SMTP服务器并发送邮件try: smtp_obj=smtplib.SMTP_SSL(smtp_server, smtp_port) smtp_obj.login(sender, password) smtp_obj.sendmail(sender, [receiver], message.as_string()) print(邮件发送成功)except smtplib.SMTPException as e: print(邮件发送失败,错误信息:, e)finally: smtp_obj.quit()

请注意,你需要替换`your_qq_email@qq.com`和`your_password`为你的QQ邮箱用户名和授权码,`receiver_email@example.com`为接收者的邮箱地址。这段代码将发送一封简单的纯文本邮件。

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

东西比较简单,简单讲一下,直接贴代码了,其他邮箱都类似。

1.首先在qq 邮箱里面把stmp服务 打开

2.拉到下面,开启第一个,发送短信验证后会得到一个授权码:

3.代码,要注意的地方我都贴了注释:

# coding=utf-8 import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication #写成了一个通用的函数接口,想直接用的话,把参数的注释去掉就好 def sen_email(msg_from,passwd,msg_to,text_content,file_path=None): #msg_from = '1095133888@qq.com' # 发送方邮箱 #passwd = 'zjvoymwngfhigjss' # 填入发送方邮箱的授权码(就是刚刚你拿到的那个授权码) #msg_to = '1095133998@qq.com' # 收件人邮箱 msg = MIMEMultipart() subject = "Test My Email" # 主题 #text_content = "你好啊,你猜这是谁发的邮件" text = MIMEText(text_content) msg.attach(text) #docFile = 'C:/Users/main.py' 如果需要添加附件,就给定路径 if file_path: #最开始的函数参数我默认设置了None ,想添加附件,自行更改一下就好 docFile = file_path docApart = MIMEApplication(open(docFile, 'rb').read()) docApart.add_header('Content-Disposition', 'attachment', filename=docFile) msg.attach(docApart) msg['Subject'] = subject msg['From'] = msg_from msg['To'] = msg_to try: s = smtplib.SMTP_SSL("smtp.qq.com", 465) s.login(msg_from, passwd) s.sendmail(msg_from, msg_to, msg.as_string()) print "发送成功" except smtplib.SMTPException, e: print "发送失败" finally: s.quit()

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

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

如何用Python编写发送带附件的QQ邮件脚本?

本例展示了如何使用Python发送QQ邮箱邮件。以下是一个简单的示例代码,用于发送邮件:

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

如何用Python编写发送带附件的QQ邮件脚本?

邮箱用户名和密码sender='your_qq_email@qq.com'password='your_password'

接收者邮箱receiver='receiver_email@example.com'

邮件内容mail_content='这是一封来自Python的测试邮件。'

邮件主题mail_title='Python发送邮件测试'

创建MIMEText对象,设置邮件内容message=MIMEText(mail_content, 'plain', 'utf-8')message['From']=Header(sender, 'utf-8')message['To']=Header(receiver, 'utf-8')message['Subject']=Header(mail_title, 'utf-8')

SMTP服务器设置smtp_server='smtp.qq.com'smtp_port=465

连接SMTP服务器并发送邮件try: smtp_obj=smtplib.SMTP_SSL(smtp_server, smtp_port) smtp_obj.login(sender, password) smtp_obj.sendmail(sender, [receiver], message.as_string()) print(邮件发送成功)except smtplib.SMTPException as e: print(邮件发送失败,错误信息:, e)finally: smtp_obj.quit()

请注意,你需要替换`your_qq_email@qq.com`和`your_password`为你的QQ邮箱用户名和授权码,`receiver_email@example.com`为接收者的邮箱地址。这段代码将发送一封简单的纯文本邮件。

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

东西比较简单,简单讲一下,直接贴代码了,其他邮箱都类似。

1.首先在qq 邮箱里面把stmp服务 打开

2.拉到下面,开启第一个,发送短信验证后会得到一个授权码:

3.代码,要注意的地方我都贴了注释:

# coding=utf-8 import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication #写成了一个通用的函数接口,想直接用的话,把参数的注释去掉就好 def sen_email(msg_from,passwd,msg_to,text_content,file_path=None): #msg_from = '1095133888@qq.com' # 发送方邮箱 #passwd = 'zjvoymwngfhigjss' # 填入发送方邮箱的授权码(就是刚刚你拿到的那个授权码) #msg_to = '1095133998@qq.com' # 收件人邮箱 msg = MIMEMultipart() subject = "Test My Email" # 主题 #text_content = "你好啊,你猜这是谁发的邮件" text = MIMEText(text_content) msg.attach(text) #docFile = 'C:/Users/main.py' 如果需要添加附件,就给定路径 if file_path: #最开始的函数参数我默认设置了None ,想添加附件,自行更改一下就好 docFile = file_path docApart = MIMEApplication(open(docFile, 'rb').read()) docApart.add_header('Content-Disposition', 'attachment', filename=docFile) msg.attach(docApart) msg['Subject'] = subject msg['From'] = msg_from msg['To'] = msg_to try: s = smtplib.SMTP_SSL("smtp.qq.com", 465) s.login(msg_from, passwd) s.sendmail(msg_from, msg_to, msg.as_string()) print "发送成功" except smtplib.SMTPException, e: print "发送失败" finally: s.quit()

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