您的位置:

Python SMTP邮件发送教程

一、发件人和收件人

在Python中发送邮件需要指定发件人和收件人。发件人可以是一个字符串,表示发件人的名称和邮箱地址,也可以是一个由名称和邮箱地址组成的元组。收件人可以是一个字符串,表示收件人的邮箱地址,也可以是由收件人姓名和邮箱地址组成的元组。以下是一个示例:

import smtplib
from email.mime.text import MIMEText
sender = '发件人名称 '
receiver = '收件人名称 
   '
message = MIMEText('邮件正文')
message['From'] = sender
message['To'] = receiver
message['Subject'] = '邮件主题'
try:
    smtpObj = smtplib.SMTP('smtp.example.com')
    smtpObj.sendmail(sender, receiver, message.as_string())         
    print("邮件发送成功")
except smtplib.SMTPException:
    print("邮件发送失败")

   
  

二、登录邮箱

在使用SMTP发送邮件之前,需要先登录邮箱。通常,需要提供用户名和密码用于身份验证。以下是一个示例:

import smtplib
from email.mime.text import MIMEText
username = 'your_email@example.com'
password = 'your_email_password'
sender = '发件人名称 '
receiver = '收件人名称 
   '
message = MIMEText('邮件正文')
message['From'] = sender
message['To'] = receiver
message['Subject'] = '邮件主题'
try:
    smtpObj = smtplib.SMTP('smtp.example.com')
    smtpObj.login(username, password)
    smtpObj.sendmail(sender, receiver, message.as_string())         
    print("邮件发送成功")
except smtplib.SMTPException:
    print("邮件发送失败")

   
  

三、添加附件

发送邮件时还可以添加附件,可以是文本文件、图片、音频或视频等任何文件。需要使用email.mime.multipart和email.mime.base模块的相关类实现。以下是一个例子:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email import encoders
username = 'your_email@example.com'
password = 'your_email_password'
sender = '发件人名称 '
receiver = '收件人名称 
   '
message = MIMEMultipart('related')
message['From'] = sender
message['To'] = receiver
message['Subject'] = '邮件主题'
# 添加文本
text = MIMEText('邮件正文')
message.attach(text)
# 添加图片
with open('image.jpg', 'rb') as f:
    img_data = f.read()
img = MIMEImage(img_data)
img.add_header('Content-ID', '
    ')
message.attach(img)
# 添加音频
with open('audio.mp3', 'rb') as f:
    audio_data = f.read()
audio = MIMEAudio(audio_data)
audio.add_header('Content-ID', '
     ')
message.attach(audio)
# 添加附件
with open('document.pdf', 'rb') as f:
    document_data = f.read()
document = MIMEBase('application', 'octet-stream', filename='document.pdf')
document.set_payload(document_data)
encoders.encode_base64(document)
document.add_header('Content-Disposition', 'attachment', filename='document.pdf')
message.attach(document)
try:
    smtpObj = smtplib.SMTP('smtp.example.com')
    smtpObj.login(username, password)
    smtpObj.sendmail(sender, receiver, message.as_string())         
    print("邮件发送成功")
except smtplib.SMTPException:
    print("邮件发送失败")

     
    
   
  

四、SSL加密连接

为了增强邮件发送的安全性,可以使用SSL加密连接来进行邮件发送。接收邮件的邮箱服务器必须支持SSL协议,否则会发送失败。以下是一个示例:

import smtplib, ssl
from email.mime.text import MIMEText
port = 465  # SMTP端口
smtp_server = "smtp.example.com"
sender = '发件人名称 '
receiver = '收件人名称 
   '
message = MIMEText('邮件正文')
message['From'] = sender
message['To'] = receiver
message['Subject'] = '邮件主题'
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender, password)
    server.sendmail(sender, receiver, message.as_string())
    print("邮件发送成功")

   
  

五、Google邮箱发送邮件

在使用Gmail发送邮件时,需要先开启IMAP/SMTP协议,并生成一个应用专用密码进行身份验证。以下是一个示例:

import smtplib, ssl
from email.mime.text import MIMEText
port = 465  # SMTP端口
smtp_server = "smtp.gmail.com"
sender = 'your_email@gmail.com'
receiver = 'receiver_email@example.com'
message = MIMEText('邮件正文')
message['From'] = sender
message['To'] = receiver
message['Subject'] = '邮件主题'
password = 'your_app_password'
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender, password)
    server.sendmail(sender, receiver, message.as_string())
    print("邮件发送成功")