Python自动群发邮件,只需20行代码!
程序员文章站
2022-06-05 18:22:18
今日分享 Python自动群发邮件 import smtplib from email import (header) from email.mime import (text, application, multipart) import time 很多人学习python,掌握了基本语法过后,不知 ......
今日分享 python自动群发邮件 import smtplib from email import (header) from email.mime import (text, application, multipart) import time
很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手。
很多已经做案例的人,却不知道如何去学习更加高深的知识。
那么针对这三类人,我给大家提供一个好的学习平台,免费领取视频教程,电子书籍,以及课程的源代码!
qq群:127341871
def sender_mail(): smt_p = smtplib.smtp() smt_p.connect(host='smtp.qq.com', port=25) sender, password = '113097485@qq.com', "**************" smt_p.login(sender, password) receiver_addresses, count_num = [ 'guozhennianhua@163.com', 'xiaoxiazi99@163.com'], 1 for email_address in receiver_addresses: try: msg = multipart.mimemultipart() msg['from'] = "zhenguo" msg['to'] = email_address msg['subject'] = header.header('这是邮件主题通知', 'utf-8') msg.attach(text.mimetext( '这是一封测试邮件,请勿回复本邮件~', 'plain', 'utf-8')) smt_p.sendmail(sender, email_address, msg.as_string()) time.sleep(10) print('第%d次发送给%s' % (count_num, email_address)) count_num = count_num + 1 except exception as e: print('第%d次给%s发送邮件异常' % (count_num, email_address)) continue smt_p.quit() sender_mail() 注意: 发送邮箱是qq邮箱,所以要在qq邮箱中设置开启smtp服务,设置完成时会生成一个授权码,将这个授权码赋值给文中的password变量。
今日分享
python自动群发邮件
import smtplib
from email import (header)
from email.mime import (text, application, multipart)
import time
def sender_mail():
smt_p = smtplib.smtp()
smt_p.connect(host='smtp.qq.com', port=25)
sender, password = '113097485@qq.com', "**************"
smt_p.login(sender, password)
receiver_addresses, count_num = [
'guozhennianhua@163.com', 'xiaoxiazi99@163.com'], 1
for email_address in receiver_addresses:
try:
msg = multipart.mimemultipart()
msg['from'] = "zhenguo"
msg['to'] = email_address
msg['subject'] = header.header('这是邮件主题通知', 'utf-8')
msg.attach(text.mimetext(
'这是一封测试邮件,请勿回复本邮件~', 'plain', 'utf-8'))
smt_p.sendmail(sender, email_address, msg.as_string())
time.sleep(10)
print('第%d次发送给%s' % (count_num, email_address))
count_num = count_num + 1
except exception as e:
print('第%d次给%s发送邮件异常' % (count_num, email_address))
continue
smt_p.quit()
sender_mail()
注意:
发送邮箱是qq邮箱,所以要在qq邮箱中设置开启smtp服务,设置完成时会生成一个授权码,将这个授权码赋值给文中的password
变量。
上一篇: 有关秒钟的文章推荐10篇
下一篇: tkinter事件键盘绑定