Python发送邮件功能示例【使用QQ邮箱】
作者:Yort2016 时间:2021-08-29 10:27:41
本文实例讲述了Python发送邮件功能。分享给大家供大家参考,具体如下:
这里以QQ邮箱为例说明
登录邮箱点账号
开启smtp
开启时会要求你发送一条短信,发送完成后点已发送。
就有授权码了
代码如下,只需更改发件人、收件人等信息即可
# encoding: utf-8
import logging
import smtplib
from email.mime.text import MIMEText
import email.utils
from datetime import datetime
_logger = logging.getLogger('utils.mailclient')
class MailClient(object):
def __init__(self, host, port, user, pwd=''):
self._host = host
self._port = port
self._user = user
self._pwd = pwd
self._smtp_server = None
self._debug = False
self._keepalive = True
def set_keepalive(self, keepalive):
self._keepalive = keepalive
def set_debug(self, debug):
self._debug = debug
if self._smtp_server is not None:
self._smtp_server.set_debuglevel(self._debug)
def _connect(self):
if self._smtp_server is None:
_smtp_server = smtplib.SMTP(self._host, self._port)
_smtp_server.ehlo()
_smtp_server.starttls()
_smtp_server.set_debuglevel(self._debug)
if self._pwd:
_smtp_server.login(self._user, self._pwd)
self._smtp_server = _smtp_server
return self._smtp_server
def _dispose(self):
if self._smtp_server is not None:
self._smtp_server.quit()
self._smtp_server = None
def send(self, to_addrs, subject, content, isdispose=True):
_smtp_server = self._connect()
if not isinstance(to_addrs, list):
to_addrs = [to_addrs]
_msg = MIMEText(content, 'html', 'utf-8')
_msg['Subject'] = subject
_msg['From'] = self._user
_msg['To'] = '; '.join(to_addrs)
_msg['Date'] = datetime.now().strftime('%Y-%d-%m %H:%M:%S')
_smtp_server.sendmail(self._user, to_addrs, _msg.as_string())
isdispose and (not self._keepalive) and self._dispose()
def send_mails(self, mails):
_smtp_server = self._connect()
for mail in mails:
self._send_mail(mail.get('to'), mail.get('subject'), mail.get('content'), False)
(not self._keepalive) and self._dispose()
def close(self):
self._dispose()
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)s %(levelname)s:%(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
import time
s = MailClient('smtp.qq.com', 587, '10010@qq.com', '授权码') # 注意,此处端口要用587,465端口是ssl使用的。
s.set_keepalive(False)
s.set_debug(True)
st = time.time()
s.send('收信人邮箱地址', '测试', '测试邮件 From QQ') #要发送的账号、标题、内容
print(time.time() - st)
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/Yort2016/article/details/75109854
标签:Python,发送邮件
0
投稿
猜你喜欢
Golang最大递减数算法问题分析
2023-07-16 13:49:21
laravel 5异常错误:FatalErrorException in Handler.php line 38的解决
2024-06-05 09:22:11
Python实现新版正方系统滑动验证码识别
2022-11-08 09:14:32
pytorch DataLoader的num_workers参数与设置大小详解
2022-12-22 12:15:58
Selenium结合BeautifulSoup4编写简单的python爬虫
2022-04-04 11:22:33
利用Python自动化操作AutoCAD的实现
2022-02-14 21:32:28
python之数字图像处理方式
2023-02-02 18:27:09
vuex新手进阶篇之actions的使用方法
2024-04-27 16:13:54
python实现KNN分类算法
2023-03-01 07:53:36
pycharm 实现本地写代码,服务器运行的操作
2023-04-10 21:12:44
教你如何用python开发一款数字推盘小游戏
2021-11-03 23:22:14
使用roolup构建你的lib(实现步骤)
2022-11-19 13:47:27
浅谈anaconda python 版本对应关系
2023-12-06 01:02:35
使用 SQL 语句实现一个年会抽奖程序的代码
2024-01-25 06:28:36
PyQt5高级界面控件之QTableWidget的具体使用方法
2023-09-05 15:36:41
胜过语言的图形符号
2009-05-06 12:43:00
Go实现文件上传和下载
2023-06-19 07:42:32
PHP获取网页标题的3种实现方法代码实例
2024-05-03 15:51:06
在SQL Server中编写通用数据访问方法
2009-01-20 11:35:00
go语言环境搭建简述
2024-05-22 10:12:57