版本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 data num=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+=1 if num==count: print('