欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python基础-SMTP发送邮件

程序员文章站 2024-03-18 14:50:10
...

环境准备

本人注册了163注册了2个小号,便于邮件的相互发送和接收测试

开启 POP3/SMTP/IMAP

  1. 具体在网易邮箱的设置->POP3/SMTP/IMAP
  2. 复制下对于的SMTP 服务器地址,例如163是smtp.163.com,待会代码需要使用

Python基础-SMTP发送邮件

发送邮件

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Python基础 SMTP发送邮件

from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr

import smtplib

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))

# 输入寄件人邮件地址和口令
from_addr = "寄件人@163.com"
password = "寄件人的密码"

# 输入收件人地址
to_addr = "收件人@163.com"

# plain 表示纯文本
msg = MIMEText("hello, 法迪大人,我在学习Python...", "plain", "utf-8")
# 填写完整点,不然会被判断为垃圾邮件
msg['From'] = _format_addr('法迪的测试小号 <%s>' % from_addr)
msg['To'] = _format_addr('苏法迪大人 <%s>' % to_addr)
msg['Subject'] = Header('来自SMTP的问候……', 'utf-8').encode()

# 输入SMTP 服务器地址
smtp_server = 'smtp.163.com'

import smtplib

# SMTP协议默认端口是25
server = smtplib.SMTP(smtp_server, 25)
# 打印出和SMTP服务器交互的所有信息
server.set_debuglevel(1)
# 输入登陆信息
server.login(from_addr, password)
# 发送邮件
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()

运行过程

D:\PythonProject>python main.py
send: 'ehlo [192.168.31.162]\r\n'
reply: b'250-mail\r\n'
reply: b'250-PIPELINING\r\n'
reply: b'250-AUTH LOGIN PLAIN\r\n'
reply: b'250-AUTH=LOGIN PLAIN\r\n'
reply: b'250-coremail 1Uxr2xKj7kG0xkI17xfsdsfGrU7I0s8FYsdssf2U3Uj8Cz28x1UUUUU7Ic2I0Y2UFERUV-UCa0xDrUUUUj\r\n'
reply: b'250-STASGDRTTLS\r\n'
reply: b'250 8BIDGFGTMIME\r\n'
reply: retcode (250); Msg: b'mail\nPIPELINING\nAUTH LOGIN PLAIN\nAUTH=LOGIN PLAIN\ncoremail 1Uxr2xKj7kG0xkI17xGrU7I0s8FY2U3Ujdssdsd8Cz28x1UUUDSFGUU7Ic2I0Y2UFERUV-UCa0xDrUUUUj\nSTARDSFGTTLS\n8BITMIME'
send: 'AUTH PLAIN AHN1cHl0aG9uZGVtb0AxNjMuY29sdstAHFDSGhendzeDEyMw==\r\n'
reply: b'235 Authentication successful\r\n'
reply: retcode (235); Msg: b'Authentication successful'
send: 'mail FROM:<aaa@qq.com>\r\n'
reply: b'250 Mail OK\r\n'
reply: retcode (250); Msg: b'Mail OK'
send: 'rcpt TO:<aaa@qq.com>\r\n'
reply: b'250 Mail OK\r\n'
reply: retcode (250); Msg: b'Mail OK'
send: 'data\r\n'
reply: b'354 End data with <CR><LF>.<CR><LF>\r\n'
reply: retcode (354); Msg: b'End data with <CR><LF>.<CR><LF>'
data: (354, b'End data with <CR><LF>.<CR><LF>')
send: b'Content-Type: text/plain; charset="utf-8"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: base64\r\nFrom: =?utf-8?b?5rOV6L+q55qE5rWL6K+V5bCP5Y+3?= <aaa@qq.com>\r\nTo: =?utf-8?b?6IuP5rOV6L+q5aSn5Lq6?= <aaa@qq.com>\r\nSubject: =?utf-8?b?5p2l6IeqU01UUOeahOmXDFGruWAmeKApuKApg==?=\r\n\r\naGVsbG8sIOazlei/quWkp+S6uu+8jOaIkeWcqSDFGOWtpuS5oFB5dGhvbi4uLg==\r\n.\r\n'
reply: b'250 Mail OK queued as smtp8,DMCowABHFkeR+T5ahlMwFA--.14106S2 1514076562\r\n'
reply: retcode (250); Msg: b'Mail OK queued as smtp8,DMCowABHFkeR+T5ahlMwFA--.14106S2 1514076562'
data: (250, b'Mail OK queued as smtp8,DMCowABHFkeR+T5ahlMwFA--.14106S2 1514076562')
send: 'quit\r\n'
reply: b'221 Bye\r\n'
reply: retcode (221); Msg: b'Bye'

收到邮件

Python基础-SMTP发送邮件