您的位置:

利用Python实现SMTP邮件服务器发送邮件

一、SMTP邮件服务器介绍

SMTP(Simple Mail Transfer Protocol)是互联网上的邮件传输协议,SMTP邮件服务器是负责发送和接收电子邮件的服务器。在发送邮件时,需要使用SMTP邮件服务器进行身份验证,确保要发送的邮件不是垃圾邮件。SMTP邮件服务器通常需要通过用户名和密码进行身份验证,并提供基于SSL或TLS的加密通信功能。在这个前提下,我们才能够利用Python实现SMTP邮件服务器发送邮件。

二、Python SMTP库介绍

Python SMTP库是Python标准库中提供的邮件发送库,可以方便地使用SMTP邮件服务器发送邮件。Python SMTP库支持常见的邮件发送需求,包括添加收件人、添加附件、设置邮件主题和正文等。

import smtplib
from email.mime.text import MIMEText

mail_host = 'smtp.example.com'
mail_user = 'your_username'
mail_pass = 'your_password'
sender = 'your_sender_address'
receiver = 'your_receiver_address'
subject = 'your_subject'
content = 'your_content'

msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver

try:
    smtpObj = smtplib.SMTP_SSL(mail_host, 465)
    smtpObj.login(mail_user, mail_pass)
    smtpObj.sendmail(sender, receiver, msg.as_string())
    smtpObj.quit()
    print('邮件发送成功')
except Exception as e:
    print(e)
    print('邮件发送失败')

三、Python SMTP库使用示例

以下示例代码展示了如何使用Python SMTP库实现发送一份邮件。在该邮件中,我们向收件人发送一份包含主题和正文的简单邮件。

import smtplib
from email.mime.text import MIMEText

mail_host = 'smtp.example.com'
mail_user = 'your_username'
mail_pass = 'your_password'
sender = 'your_sender_address'
receiver = 'your_receiver_address'
subject = 'your_subject'
content = 'your_content'

msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver

try:
    smtpObj = smtplib.SMTP_SSL(mail_host, 465)
    smtpObj.login(mail_user, mail_pass)
    smtpObj.sendmail(sender, receiver, msg.as_string())
    smtpObj.quit()
    print('邮件发送成功')
except Exception as e:
    print(e)
    print('邮件发送失败')

四、Python SMTP库实现添加附件

除了发送纯文本邮件外,我们还可以使用Python SMTP库实现添加附件的功能。下面的示例展示了如何实现添加附件的操作。

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication

mail_host = 'smtp.example.com'
mail_user = 'your_username'
mail_pass = 'your_password'
sender = 'your_sender_address'
receiver = 'your_receiver_address'
subject = 'your_subject'
content = 'your_content'

msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver

attachment_path = '/path/to/your/attachment'
with open(attachment_path, 'rb') as fp:
    attachment = MIMEApplication(fp.read())
    attachment.add_header('Content-Disposition', 'attachment', filename='your_attachment_filename')
    msg.attach(attachment)

text = MIMEText(content)
msg.attach(text)

try:
    smtpObj = smtplib.SMTP_SSL(mail_host, 465)
    smtpObj.login(mail_user, mail_pass)
    smtpObj.sendmail(sender, receiver, msg.as_string())
    smtpObj.quit()
    print('邮件发送成功')
except Exception as e:
    print(e)
    print('邮件发送失败')

五、Python SMTP库实现HTML邮件

Python SMTP库也支持发送HTML邮件,下面是一个使用Python SMTP库实现发送HTML邮件的简单示例。

import smtplib
from email.mime.text import MIMEText

mail_host = 'smtp.example.com'
mail_user = 'your_username'
mail_pass = 'your_password'
sender = 'your_sender_address'
receiver = 'your_receiver_address'
subject = 'your_subject'
content = '

your_html_content