Python使用QQ邮箱发送多收件人email
来源:互联网 发布:mfc windows程序设计 编辑:程序博客网 时间:2024/06/11 00:47
实际开发过程中使用到邮箱的概率很高,那么如何借助python使用qq邮箱发送邮件呢?
代码很简单,短短几行代码就可以实现这个功能。
使用到的模块有smtplib和email这个两个模块,关于这两个模块的方法就不多说了。
代码如下:
#coding:utf-8 # 强制使用utf-8编码格式# 加载smtplib模块import smtplibfrom email.mime.text import MIMETextimport string#第三方SMTP服务mail_host = "smtp.qq.com" # 设置服务器mail_user = "572****@qq.com" # 用户名mail_pwd = "***********" # 口令,QQ邮箱是输入授权码,在qq邮箱设置 里用验证过的手机发送短信获得,不含空格mail_to = ['12345678@qq.com','8888888@qq.com'] #接收邮件列表,是list,不是字符串#邮件内容msg = MIMEText("尊敬的用户:您的注册申请已被接受。您可以尝试点击下面的连接进行激活操作。") # 邮件正文msg['Subject'] = "A test email for python !" # 邮件标题msg['From'] = mail_user # 发件人msg['To'] = ','.join(mail_to) # 收件人,必须是一个字符串try: smtpObj = smtplib.SMTP_SSL(mail_host, 465) smtpObj.login(mail_user, mail_pwd) smtpObj.sendmail(mail_user,mail_to, msg.as_string()) smtpObj.quit() print("邮件发送成功!")except smtplib.SMTPException: print ("邮件发送失败!")
细心的读者会发现代码中有这样一句:msg[‘to’]=’,’.join(strTo),但是msg[[‘to’]并没有在后面被使用,这么写明显是不合理的,但是这就是stmplib的bug。你只有这样写才能群发邮件。
The problem is that SMTP.sendmail and email.MIMEText need two different things.email.MIMEText sets up the “To:” header for the body of the e-mail. It is ONLY used for displaying a result to the human beingat the other end, and like all e-mail headers, must be a single string. (Note that it does not actually have to have anything to do with the people who actually receive the message.)SMTP.sendmail, on the other hand, sets up the “envelope” of the message for the SMTP protocol. It needs a Python list of string, each of which has a single address.So, what you need to do is COMBINE the two replies you received. Set msg‘To’ to a single string, but pass the raw list to sendmail.
1 0
- Python使用QQ邮箱发送多收件人email
- 使用QQ邮箱发送email(Python【支持中文】)
- Java使用qq邮箱发送email
- javamail实现腾讯邮箱(企业邮箱,qq邮箱)发送邮件(多收件人多附件)
- 记common-email发送邮件,使用qq邮箱服务器
- python 发送email到qq
- python 发送email到qq
- Python QQ 发送Email SMTP
- 发送Email的多种方式(多收件人,附件)
- 发送email给列表中的邮箱--python
- 使用qq邮箱发送邮件
- 这里是通过QQ邮箱 sina新浪邮箱 163邮箱的简单发送Email
- Python发送邮件给qq邮箱
- python 发送邮件(QQ邮箱)
- python给qq邮箱发送邮件
- python QQ邮箱发送邮件实例
- python smtp QQ邮箱 发送附件
- Spring 框架使用QQ邮箱发送邮件
- 左右法则
- 计算机中的大小端存储
- UFLDL教程Exercise答案(4):Softmax Regression
- 程序的编译,连接
- 解密ThreadLocal
- Python使用QQ邮箱发送多收件人email
- 判断字符是否为汉字
- JavaEE:会话技术笔记
- SCSI——小型计算机系统接口
- UFLDL教程Exercise答案(8):Convolution and Pooling
- git基础知识学习
- 关于js的跨域问题解决办法
- 第11周项目2-用二叉树求解代数表达式
- listview设置item高度或间距