Python撰写mail
版本1 指定邮箱进行发送
""" 说明:指定账户密码进行邮件发送 由312051952@qq.com-->c4kaichen@163.com """import smtplib from email.mime.text import MIMEText from email.header import Header #登录邮箱步骤 smtpConnect=smtplib.SMTP('smtp.qq.com',587) smtpConnect.ehlo() smtpConnect.starttls() smtpConnect.login('312051952@qq.com','邮箱密码') sender='312051952@qq.com' #receiver=[input('输入邮箱地址:')] receiver=['c4kaichen@163.com'] #显示收件人 message=MIMEText('python邮件发送测试3','plain','utf-8')#邮箱内容 message['To'] = Header('c4kaichen@163.com') subject='python smtp测试3' #邮箱主题 message['Subject']=Header(subject,'utf-8') smtpConnect.sendmail(sender,receiver,message.as_string()) smtpConnect.quit()
版本2 手动输入对方邮箱进行登录
#发送邮件并手动输入对方邮箱 import smtplib from email.mime.text import MIMEText from email.header import Header #登录邮箱步骤 smtpConnect=smtplib.SMTP('smtp.qq.com',587) smtpConnect.ehlo() smtpConnect.starttls() smtpConnect.login('312051952@qq.com','邮箱密码') sender='312051952@qq.com' receiver=input('输入邮箱地址:') receiverdone=[receiver] #要用列表显示 #receiver=['c4kaichen@163.com'] #显示收件人 message=MIMEText('python邮件发送测试5','plain','utf-8')#邮箱内容 message['To'] = Header(receiver) subject='python smtp测试4' #邮箱主题 message['Subject']=Header(subject,'utf-8') smtpConnect.sendmail(sender,receiverdone,message.as_string()) smtpConnect.quit()
最新版本:
分别输入要登录的QQ邮箱密码账号
以及需要发送的对方账号以及邮件内容
#发送邮件并手动输入对方邮箱 import smtplib from email.mime.text import MIMEText from email.header import Header #登录邮箱步骤 smtpConnect=smtplib.SMTP('smtp.qq.com',587) smtpConnect.ehlo() smtpConnect.starttls() user=input('请输入QQ邮箱地址:') passwd=input('请输入QQ邮箱密码:') smtpConnect.login(user,passwd) sender=user receiver=input('输入对方邮箱地址:') content=input('请输入要发送的内容:') receiverdone=[receiver] #要用列表显示 #receiver=['c4kaichen@163.com'] #显示收件人 message=MIMEText(content,'plain','utf-8')#邮箱内容 message['To'] = Header(receiver) subject='python smtp测试4' #邮箱主题 message['Subject']=Header(subject,'utf-8') smtpConnect.sendmail(sender,receiverdone,message.as_string()) smtpConnect.quit()
手动输入邮箱及密码之后手动输入对方的邮箱,邮件主题,内容进行邮件的发送工作
# 发送邮件并手动输入对方邮箱 # 注意此处是针对QQ邮箱进行撰写 import smtplib from email.mime.text import MIMEText from email.header import Header# 登录邮箱步骤 smtpConnect = smtplib.SMTP('smtp.qq.com', 587) smtpConnect.ehlo() smtpConnect.starttls() user = input('请输入QQ邮箱地址:') passwd = input('请输入QQ邮箱密码:') smtpConnect.login(user, passwd) sender = user receiver = input('输入对方邮箱地址:') subject = input('请输入邮件主题:') content = input('请输入要发送的内容:') receiverdone = [receiver] # 要用列表显示 # receiver=['c4kaichen@163.com'] #显示收件人 message = MIMEText(content, 'plain', 'utf-8') # 邮箱内容 message['To'] = Header(receiver) message['Subject'] = Header(subject, 'utf-8') smtpConnect.sendmail(sender, receiverdone, message.as_string()) smtpConnect.quit()
新版本:每天的菜谱进行选择然后自动发送邮件
import smtplib from email.mime.text import MIMEText from email.header import Header import time def current_time():time_format='%Y-%d-%m %X'time_current=time.strftime(time_format)return time_current #登录邮箱步骤 def sendmail(x):smtpConnect=smtplib.SMTP('smtp.qq.com',587)smtpConnect.ehlo()smtpConnect.starttls()smtpConnect.login('312051952@qq.com','邮箱密码')sender='312051952@qq.com'receiver=['c4kaichen@163.com'] #显示收件人message=MIMEText(x,'plain','utf-8')#邮箱内容 此处建议采用plain 如果换成text发送过去的会是附件形式message['To'] = Header('c4kaichen@163.com')time_current=current_time()subject='%s每日菜单'%time_current #邮箱主题:每日菜单message['Subject']=Header(subject,'utf-8')smtpConnect.sendmail(sender,receiver,message.as_string())smtpConnect.quit() import random list_menu=['排骨冬瓜','清炒包菜',' 蒜泥西蓝花',' 清炒青菜',' 筒骨汤',' 香干肉丝',' 丝瓜炒蛋',' 面条',' 沙县',' 品客'] def write_menu(args):with open('menu_info','w',encoding='utf-8') as f:f.writelines(args) def read_menu():with open('menu_info','r',encoding='utf-8') as f2:data=f2.read()return datanum=0 item_menu=[] count=input('请输入今天要吃几个菜:>>').strip() if count.isdigit():count=int(count)while num<count:item=random.choice(list_menu)item_menu.append(item)num+=1if num==count:print('\033[31;1m今日选择的菜单如下:\033[0m')for p_item in item_menu:print(p_item)write_menu(item_menu)x=read_menu()sendmail(x) else:print('请重新输入') input('press Enter to exit')
View Code
#add at 18-11-11
import smtplib from email.header import Header from email.mime.text import MIMEText # 第三方 SMTP 服务 mail_host = "smtp.163.com" # SMTP服务器 mail_user = "c4kaichen" # 用户名 mail_pass = "XXXXX" # 授权密码,非登录密码 sender = 'c4kaichen@163.com' # 发件人邮箱(最好写全, 不然会失败) receivers = '312051952@qq.com' # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 title = '标题info'#标题 content = '正文info' # 正文 def sendEmail():message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码message['From'] = "{}".format(sender)#message['To'] = ",".join(receivers)message['To'] = receiversmessage['Subject'] = titletry:smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465smtpObj.login(mail_user, mail_pass) # 登录验证smtpObj.sendmail(sender, receivers, message.as_string()) # 发送print("mail has been send successfully.")except smtplib.SMTPException as e:print(e)def send_email2(SMTP_host, from_account, from_passwd, to_account, subject, content):email_client = smtplib.SMTP(SMTP_host)email_client.login(from_account, from_passwd)# create msgmsg = MIMEText(content, 'plain', 'utf-8')msg['Subject'] = Header(subject, 'utf-8') # subjectmsg['From'] = from_accountmsg['To'] = to_accountemail_client.sendmail(from_account, to_account, msg.as_string())email_client.quit() if __name__ == '__main__':sendEmail()# receiver = '***'# send_email2(mail_host, mail_user, mail_pass, receiver, title, content)
### add at 18-11-11 通过163邮箱每日IP推送
import socket# 获取本机计算机名称 hostname = socket.gethostname() #print(hostname) # 获取本机ip列表 #ip = socket.gethostbyname_ex(hostname) ip = socket.gethostbyname_ex(hostname) addr=ip[2]#获取当前时间 import time current_time=time.ctime()#获取计算机名 pc_name=socket.gethostname() ip_info=current_time+' '+pc_name+' '+str(addr)import smtplib from email.header import Header from email.mime.text import MIMEText # 第三方 SMTP 服务 mail_host = "smtp.163.com" # SMTP服务器 mail_user = "c4kaichen" # 用户名 mail_pass = "XXXXX" # 授权密码,非登录密码 sender = 'c4kaichen@163.com' # 发件人邮箱(最好写全, 不然会失败) receivers = '312051952@qq.com' # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 title = '每日ip' # 标题 content = ip_info#正文 def sendEmail():message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码message['From'] = "{}".format(sender)#message['To'] = ",".join(receivers)message['To'] = receiversmessage['Subject'] = titletry:smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465smtpObj.login(mail_user, mail_pass) # 登录验证smtpObj.sendmail(sender, receivers, message.as_string()) # 发送print("mail has been send successfully.")except smtplib.SMTPException as e:print(e)def send_email2(SMTP_host, from_account, from_passwd, to_account, subject, content):email_client = smtplib.SMTP(SMTP_host)email_client.login(from_account, from_passwd)# create msgmsg = MIMEText(content, 'plain', 'utf-8')msg['Subject'] = Header(subject, 'utf-8') # subjectmsg['From'] = from_accountmsg['To'] = to_accountemail_client.sendmail(from_account, to_account, msg.as_string())email_client.quit() if __name__ == '__main__':sendEmail()# receiver = '***'# send_email2(mail_host, mail_user, mail_pass, receiver, title, content)
View Code
升级版 邮件轰炸
import socket from threading import Timer# 获取本机计算机名称 hostname = socket.gethostname() #print(hostname) # 获取本机ip列表 #ip = socket.gethostbyname_ex(hostname) ip = socket.gethostbyname_ex(hostname) addr=ip[2]#获取当前时间 import time current_time=time.ctime()#获取计算机名 pc_name=socket.gethostname() ip_info=current_time+' '+pc_name+' '+str(addr)import smtplib from email.header import Header from email.mime.text import MIMEText # 第三方 SMTP 服务 mail_host = "smtp.163.com" # SMTP服务器 mail_user = "c4kaichen" # 用户名 mail_pass = "XXXXX" # 授权密码,非登录密码 sender = 'c4kaichen@163.com' # 发件人邮箱(最好写全, 不然会失败) receivers = '312051952@qq.com' # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 title = '每日ip' # 标题 content = ip_info#正文 def sendEmail():message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码message['From'] = "{}".format(sender)#message['To'] = ",".join(receivers)message['To'] = receiversmessage['Subject'] = titletry:smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465smtpObj.login(mail_user, mail_pass) # 登录验证smtpObj.sendmail(sender, receivers, message.as_string()) # 发送t=Timer(10,sendEmail)t.start()print("mail has been send successfully.")except smtplib.SMTPException as e:print(e)def send_email2(SMTP_host, from_account, from_passwd, to_account, subject, content):email_client = smtplib.SMTP(SMTP_host)email_client.login(from_account, from_passwd)# create msgmsg = MIMEText(content, 'plain', 'utf-8')msg['Subject'] = Header(subject, 'utf-8') # subjectmsg['From'] = from_accountmsg['To'] = to_accountemail_client.sendmail(from_account, to_account, msg.as_string())email_client.quit() if __name__ == '__main__':sendEmail()# receiver = '***'# send_email2(mail_host, mail_user, mail_pass, receiver, title, content)
转载于:https://www.cnblogs.com/nodchen/p/8458140.html
Python撰写mail相关推荐
- python模型训练框架_以Python撰写 AI模型框架
以Python撰写 AI模型框架 by 高焕堂 1. 前言: 在AI(人工智慧)方面,由于当今的机器学习本质是一种<大数据相关性支撑的>归纳性推理.软体框架的复用(Reuse)性愈高,对于 ...
- Python之Mail编程(电子邮件编程)
电子邮件的历史起源 1969 Leonard K. 教授发给同时的 "LO" 1971 美国国防部自主的阿帕网(Arpanet)的通讯机制 通讯地址里用@, 1987年中国的第一份 ...
- python之进程与线程
什么是操作系统 可能很多人都会说,我们平时装的windows7 windows10都是操作系统,没错,他们都是操作系统.还有没有其他的? 想想我们使用的手机,Google公司的Androi ...
- python发送邮件和附件
发送邮件的时候,需要发送人,收件人,和一台邮件服务器,这里使用python发送一个邮件,主要需要引入smtplib和email库. 下面是源码,粘贴即可用: #!/usr/bin/env python ...
- 精通python爬虫框架-精通Python爬虫框架Scrapy PDF 中文清晰版
给大家带来的一篇关于Python爬虫相关的电子书资源,介绍了关于Python.爬虫.框架.Scrapy方面的内容,本书是由人民邮电出版社出版,格式为PDF,资源大小8.6 MB,迪米特里奥斯编写,目前 ...
- 学python的好处-学习python有什么好处?python强大之处在哪?
Python是一种面向对象编程.释疑型的开发语言,另外也是一种功能齐全而逐步完善的通用性語言.对比别的编程语言,Python代码比较简单,入门很容易.这种全是学习培训python的优势,和传统式的C/ ...
- python与php8-php8的扩展arginfo生成工具及工具初体验
php8提供了非常方便的扩展函数或类参数信息的生成工具. 只需要维护一份xyz.stub.php,就可以使用工具生成 xyz_arginfo.h. 毫无疑问,这种方式,又降低了广大 phper 开发扩 ...
- python可以干嘛知乎-Python到底可以干什么?老男孩Python视频教程
如果说挑选一门编程语言进行学习,你会选择哪个?当然是Python.Python是一门简单的编程语言,适合初学者学习,也是很多人都喜欢的语言,那么Python到底可以干什么?跟着老男孩教育来看看吧. P ...
- python主要是干什么用的-Python到底可以干什么?主要应用领域
如果说挑选一门编程语言进行学习,你会选择哪个?当然是Python.Python是一门简单的编程语言,适合初学者学习,也是很多人都喜欢的语言,那么Python到底可以干什么? Python语言在学术上是 ...
最新文章
- unity获取电磁笔压感_1024级压感 原道W8S电磁笔的秘密
- B.The Tortoise and the Hare 长春
- Redis 分布式集群搭建2022版本+密码(linux环境)
- ubuntu下安装oracle
- sql script: Calculating Days
- java实现内存修改器_魔兽3内存修改器 v8
- alex的ATM学习笔记
- [渝粤教育] 西南科技大学 机械设计基础 在线考试复习资料(1)
- zigbee协议栈-端点
- http长连接与主动断开方
- 华氏温度与摄氏温度转换 java_用JAVA写一个将华氏温度转换成摄氏温度的程序
- 什么是依赖注入(DI)
- 如何判断自己IP是内网IP还是外网IP
- springboot项目日志记录访问客户端ip地址
- during怎么念_during怎么读音英语 during中文谐音
- 用计算机进行情报检测属于计算机应用中,[单选] 计算机用于情报检索属于计算机应用中的()。...
- 证件怎么扫描?手机即可扫描
- unity中通过代码控制游戏中画质
- IDEA包死活引入不进来的问题
- java歌曲上传下载功能实现,Java上传下载功能的实现详解