python对邮件的常用操作收邮件发邮件

枫铃3年前 (2021-09-30)Python256
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("邮件无法发送")

发送带附件的邮件

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
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("邮件无法发送")

收取邮件

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
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'])

相关文章

python字典遍历的4种方法

1.遍历key值 &g...

Python编程4道练习题

实例一:数...

非常有用的15个Linux 服务器监控命令

如果你想知道你的服务器正在做干什么,你就需要了解一些基本的命令,一旦你精通了这些命令,那你就是一个 专业的 Linu...

python面试题一个字符串是否由重复的子字符串组成

一,给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。 给定的字符串只含有小写英文字母,并且长度不超...

315道Python常见面试题

315道Python常见面试题

第一部分,Python基础篇 为什么学习Python? 通过什么途径学习的Python? Python...

对Python参数类型详解以及学习中遇到的坑

由于之前遇到过几次有关于...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。