标签:str message mime end python info 常用 serve server
import smtplib from smtplib import SMTP_SSL from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header host_server = ‘smtp.163.com‘ send_sina = ‘xx@163.com‘ pwd = ‘xxx‘ send_sina_mail = ‘xx@163.com‘ receiver = ‘xx@qq.com‘ mail_title = ‘python办公自动化的邮件‘ mail_content = ‘你好,这是使用python登陆sina邮箱发送邮件的测试‘ msg = MIMEMultipart() # 邮件主体 msg["Subject"] = Header(mail_title, ‘utf-8‘) msg["From"] = send_sina_mail msg[‘To‘] = Header("测试邮箱", ‘utf-8‘) msg.attach(MIMEText(mail_content, ‘plain‘, ‘utf-8‘)) # 邮件正文内容 smtp = SMTP_SSL(host_server) # ssl登陆 smtp.login(send_sina, pwd) smtp.sendmail(send_sina_mail, receiver, msg.as_string())
网页版邮件
import smtplib from smtplib import SMTP_SSL from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header host_server = ‘smtp.163.com‘ send_sina = ‘aa@163.com‘ pwd = ‘pass‘ send_sina_mail = ‘aa@163.com‘ receiver = ‘bb@qq.com‘ mail_title = ‘python办公自动化的邮件‘ mail_content = ‘你好,<p>这是使用python登陆sina邮箱发送邮件的测试</p>‘ ‘<a href="https://www.python.org">Python</a>‘ # 邮件正文内容 msg = MIMEMultipart() # 邮件主体 msg["Subject"] = Header(mail_title, ‘utf-8‘) msg["From"] = send_sina_mail msg[‘To‘] = Header("测试邮箱", ‘utf-8‘) msg.attach(MIMEText(mail_content, ‘html‘, ‘utf-8‘)) # 邮件正文内容 try: smtp = SMTP_SSL(host_server) # ssl登陆 smtp.set_debuglevel(1) # 开启debug smtp.ehlo(host_server) smtp.login(send_sina, pwd) smtp.sendmail(send_sina_mail, receiver, msg.as_string()) smtp.quit() print("邮件发送成功") except smtplib.SMTPException: print("邮件无法发送")
发送带附件的邮件
import smtplib from smtplib import SMTP_SSL from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication from email.header import Header host_server = ‘smtp.163.com‘ send_sina = ‘aa@163.com‘ pwd = ‘pass‘ send_sina_mail = ‘aa@163.com‘ receiver = ‘bb@qq.com‘ mail_title = ‘python办公自动化的邮件‘ mail_content = ‘你好,<p>这是使用python登陆sina邮箱发送邮件的测试</p>‘ ‘<a href="https://www.python.org">Python</a>‘ # 邮件正文内容 msg = MIMEMultipart() # 邮件主体 msg["Subject"] = Header(mail_title, ‘utf-8‘) msg["From"] = send_sina_mail msg[‘To‘] = Header("测试邮箱", ‘utf-8‘) msg.attach(MIMEText(mail_content, ‘html‘, ‘utf-8‘)) # 邮件正文内容 attachment = MIMEApplication(open("d:/peisong.xls", "rb").read()) attachment.add_header(‘Content-Disposition‘, ‘attachment‘, filename=‘peisong.xlsx‘) msg.attach(attachment) try: smtp = SMTP_SSL(host_server) # ssl登陆 smtp.set_debuglevel(1) # 开启debug smtp.ehlo(host_server) smtp.login(send_sina, pwd) smtp.sendmail(send_sina_mail, receiver, msg.as_string()) smtp.quit() print("邮件发送成功") except smtplib.SMTPException: print("邮件无法发送")
收取邮件
import zmail server = zmail.server("aa@163.com", "pass") mail = server.get_latest() # zmail.show(mail) print(mail[‘subject‘]) print(mail[‘id‘]) print(mail[‘from‘]) print(mail[‘to‘]) print(mail[‘content_text‘]) print(mail[‘content_html‘]) # 查看附件 zmail.save_attachment(mail, target_path=None, overwrite=True)
邮件监控
import zmail from tkinter import * import tkinter.messagebox server = zmail.server(‘aa@163.com‘, ‘pass‘) mail = server.get_latest() new_id = mail[‘id‘] file_read = open(‘id.txt‘, ‘r‘) old_id = file_read.readline() file_write = open(‘id.txt‘, ‘w‘) file_write.write(str(new_id)) file_write.close() if old_id != str(new_id): main_window = Tk() main_window.withdraw() tkinter.messagebox.showinfo("你有邮件了", ‘老铁,你的邮件来了,瞅一眼吧\n,邮件标题:%s‘ % mail[‘subject‘])
标签:str message mime end python info 常用 serve server
原文地址:https://www.cnblogs.com/reblue520/p/12490004.html