import poplib import ssl ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE USER = "barant@barant.com" PASS = "imaptest123" def step(name, fn): try: result = fn() print(f"OK {name}: {result}") return result except Exception as e: print(f"FAIL {name}: {e}") raise m = poplib.POP3_SSL("127.0.0.1", 995, context=ctx) print("welcome:", m.getwelcome()) step("USER", lambda: m.user(USER)) step("PASS", lambda: m.pass_(PASS)) count, size = step("STAT", lambda: m.stat()) print(f" {count} messages, {size} octets") resp, items, octets = step("LIST", lambda: m.list()) print(f" {len(items)} items, sample: {items[0] if items else None}") if count > 0: resp, lines, octets = step("RETR 1", lambda: m.retr(1)) print(f" {len(lines)} lines, first: {lines[0] if lines else None}") resp, uidls, octets = step("UIDL (all)", lambda: m.uidl()) print(f" sample: {uidls[0] if uidls else None}") resp = step("UIDL 1", lambda: m.uidl(1)) print(f" {resp}") resp, lines, octets = step("TOP 1 0", lambda: m.top(1, 0)) print(f" {len(lines)} lines (should be 0 body lines)") step("DELE 1", lambda: m.dele(1)) count2, size2 = step("STAT (after DELE)", lambda: m.stat()) print(f" {count2} messages (should be {count - 1})") step("RSET", lambda: m.rset()) count3, size3 = step("STAT (after RSET)", lambda: m.stat()) print(f" {count3} messages (should be back to {count})") step("DELE 1 (again, will commit)", lambda: m.dele(1)) step("QUIT", lambda: m.quit()) print("ALL DONE") # Reconnect to confirm the DELE really committed at QUIT. m2 = poplib.POP3_SSL("127.0.0.1", 995, context=ctx) m2.user(USER) m2.pass_(PASS) count4, size4 = m2.stat() print(f"after reconnect: {count4} messages (should be {count - 1})") m2.quit()