使用QQ邮箱发送email(Python【支持中文】)

实际开发过程中使用到邮箱的概率很高,那么如何借助python使用qq邮箱发送邮件呢?

代码很简单,短短几行代码就可以实现这个功能。
使用到的模块有smtplib和email这个两个模块,关于这两个模块的方法就不多说了。
我们先说说网上常用的使用这那两个模块发送邮件的方法
代码如下:

import smtplib
from email.mime.text import MIMEText
from email.header import Header

def SendEmail(fromAdd, toAdd, subject, attachfile, htmlText):
  strFrom = fromAdd;
  strTo = toAdd;
  msg =MIMEText(htmlText,'plain','utf-8');
  msg['Content-Type'] = 'Text/HTML';
  msg['Subject'] = Header(subject,'utf-8').encode();
  msg['To'] = strTo;
  msg['From'] = strFrom;
  
  smtp = smtplib.SMTP('smtp.qq.com');
  smtp.login('501257367@qq.com','password');
  try:
    smtp.sendmail(strFrom,strTo,msg.as_string());
  finally:
    smtp.close;

if __name__ == "__main__":
  SendEmail("501257367@qq.com","501257367@qq.com","主题","你好","你好,世界!world!");

运行结果:

smtplib.SMTPAuthenticationError: (530, 'Error:A secure connectionis requiered(suchas ssl).More information at http://service.mail.qq.com/cgi-bin/help?id=28')

如果发送给多人,这么写:

import smtplib
from email.mime.text import MIMEText
from email.header import Header

multiPersion = [33333@qq.com, 666666@qq.com, 888888@qq.com]

def SendEmail(fromAdd, toAdd, subject, attachfile, htmlText):
  strFrom = fromAdd;
  strTo = toAdd;
  msg =MIMEText(<pre><h1>htmlText</h1></pre>,'html','utf-8');
  msg['Content-Type'] = 'Text/HTML';
  msg['Subject'] = Header(subject,'utf-8').encode();
  msg['To'] = strTo;
  msg['From'] = strFrom;
  
  smtp = smtplib.SMTP('smtp.qq.com');
  smtp.login('422700@qq.com','password');
  try:
    smtp.sendmail(strFrom,strTo,msg.as_string());
  finally:
    smtp.close;

if __name__ == "__main__":
  SendEmail("501257367@qq.com", multiPersion,"主题","你好","你好,世界!world!");





这样的报错,需要一个安全的连接,例如SSL,因此接下来我们会使用SSL的方式去登录,但是在那之前,我们需要做一些准备,打开qq邮箱,点击设置->
账户,找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务,开启IMAP/SMTP服务,然后根据要求使用手机发送到指定号码,获取授权码,
这个授权码就是你接下来登录要使用的密码,配置完成,代码如下:

##coding: utf-8


import smtplib
from email.mime.text import MIMEText
from email.header import Header


_user = "422700@qq.com"
_pwd  = "xxxxxxxxxxxxxxx"
_to   = "422700@qq.com"


msg = MIMEText("发给大兄弟的python自动发的邮件!",'plain','utf-8')
msg["Subject"] = Header("Python中文邮件测试!",'utf-8').encode()
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 


运行结果如下:

大功告成!


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,你可以使用第三方库如`smtplib`和`email`模块来发送电子邮件,包括使用QQ邮箱QQ邮箱通常会要求使用SMTP(简单邮件传输协议)服务,但需要先设置授权码而非传统密码。 以下是基本步骤: 1. 安装所需库(如果尚未安装): ```bash pip install smtplib email ``` 2. 导入必要的库并创建邮件对象: ```python import smtplib from email.mime.text import MIMEText from email.header import Header # 邮件配置信息 smtp_server = "smtp.qq.com" # QQ SMTP服务器地址 smtp_port = 465 # 常用端口为465或587(加密) sender_email = "your_qq_email@qq.com" # 发送者的QQ邮箱账号 sender_password = "your_authorization_code" # 授权码(不是密码) receiver_email = "recipient_email@example.com" # 收件人邮箱 message = MIMEText("这是测试邮件的内容", 'plain', 'utf-8') message['From'] = Header(sender_email) message['To'] = Header(receiver_email) subject = "Python邮件测试" message['Subject'] = Header(subject) # 构建邮件数据 msg = message.as_string() ``` 3. 连接并发送邮件: ```python try: with smtplib.SMTP_SSL(smtp_server, smtp_port) as server: server.login(sender_email, sender_password) server.sendmail(sender_email, receiver_email, msg) print(f"邮件已成功发送至 {receiver_email}") except Exception as e: print(f"发送邮件失败:{e}") finally: server.quit() # 关闭连接 ``` 注意:为了安全起见,请确保在实际应用中妥善处理你的授权码,并在不使用时清除它们。此外,QQ邮箱可能对非授权客户端有所限制,所以可能需要开启“QQ邮箱SMTP服务”并检查其设置是否允许从非默认客户端发送邮件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值