使用python獲取郵箱郵件的設定方法

2022-10-03 18:42:10 字數 3637 閱讀 6958

首先我們要在郵箱的設定中開通那個pop3

然後我們要匯入這些包

import poplib

from datetime import datetime

import jieba

import re

from email.parser import parser

from email.header import decode_header

from email.utils import parseaddr

from lxml import etree

然後進行鏈結

name_list =

email = '***@xx.com'

password = '***xxqcnvrpkx'

pop3_server = 'imap.exmail.qq.com'

# 連線到pop3伺服器:

server = poplib.pop3(pop3_server)

# 可以開啟或關閉除錯資訊:

server.set_debuglevel(1)

# 可選:列印pop3伺服器的歡迎文字:

print(server.getwelcome().decode('utf-8'))

# 身份認證:

server.user(email)

server.pass_(password)

# stat()返回郵件數量和占用空間:

print('messages: %s. size: %s' % server.stat())

# list()返回所有郵件的編號:

resp, mails, octets = server.list()

# 可以檢視返回的列表類似[b'1 82923', b'2 2184', ...]

print(mails)

# 獲取最新一封郵件, 注意索引號從1開始:

index = len(mails)

print(index,'2222')

resp,www.cppcns.com lines, octets = server.retr(284)

# lines儲存了郵件的原始文字的每一行,

# 可以獲得整個郵件的原始文字:

msg_content = b'\r\n'.join(lines).decode('utf-8')

# 稍後解析出郵件:

msg = parser().parsestr(msg_content)

# print(msg)

messageobject = parser().parsestr(msg_content)

msgdate = messageobject["date"]

print(678,msgdate)

# 可以根據郵件索引號直接從伺服器刪除郵件:

# server.dele(index)

# 關閉連線:

這裡的index是全部的郵件數量,server.retr(284),從最新的開始,retr裡面傳你要讀取的第幾個,

try:

gmt_format = '%a, %d %b %y %h:%m:%s +0800 (cst)'

new_date = datetime.strptime(msgdate, gmt_format)

except:

gmt_format = '%a, %d %b %y %h:%m:%s +0800'

new_date = datetime.strptime(msgdate, gmt_format)

name_list.append(str(new_date))

print(datetime.strptime(msgdate, gmt_format))

直接讀取出來的時間是tue, 17 sep 2019 16:21:25 +0800(cst) 這種形式,這裡是轉為為2019-09-17 16:21:25這種亞子。哈哈

繼續def print_info(msg, indent=0):

if indent =qcnvrpk= 0:

for header in ['from', 'to', 'subject']:

value = msg.get(header, '')

if value:

if header == 'subject':

value = decode_s程式設計客棧tr(value)

else:

hdr, addr = parseaddr(value)

name = decode_str(hdr)

value = u'%s ' % (name, addr)

print('7777',name)

print('%s%s: %s' % (' ' * indent, header, value))

if (msg.is_multipart()):

parts = msg.get_payload()

for n, part in enumerate(parts):

print('%spart %s' % (' ' * indent, n))

print('%s--------------------' % (' ' * indent))

print_info(part, indent + 1)

else:

content_type = msg.get_content_type()

if content_type == 'text/plain' or content_type == 'text/html':

content = msg.get_payload(decode=true)

charset = guess_charset(msg)

if charset:

content = content.decode(charset)

print('%stext: %s' % (' ' * indent, content + '...'))

else:

print('%sattachment: 程式設計客棧%s' % (' ' * indent, content_type))

這裡就是讀取郵件內容了

繼續還有兩個函式

def decode_str(s):

value, charset = decode_header(s)[0]

if charset:

value = value.decode(charset)

return value

def guess_charset(msg):

charset = msg.get_charset()

if charset is none:

content_type = msg.get('content-type', '').lower()

pos = content_type.find('charset=')

if pos >= 0:

charset = content_type[pos + 8:].strip()

return charset

print_info(msg = msg)

print(name_list)

server.quit()

就這樣了

總結本文標題: 使用python獲取郵箱郵件的設定方法

本文位址:

企業郵箱使用python發郵件

下邊是發郵件的相關 usr bin env python coding utf 8 date 2017 02 22 09 44 42 import datetime import email import smtplib import os from email.mime.text import m...

python發郵件(163郵箱)

import smtplib from email.mime.text import mimetext from email.header import header class sendemail def init self,sender smtpserver smtp.163.com usern...

python實現126郵箱傳送郵件

用python傳送126郵件,供大家參考,具體內容如下 今天想做個自動化郵件提醒的功能,最近剛好在學習python,都說python那麼強大,想試一下python能否搞定,搜一下資料,果真可以,而且又簡單通俗易懂 from email.mail.text import mimetext import...