📅  最后修改于: 2023-12-03 14:47:29.705000             🧑  作者: Mango
smtplib是Python中的一个内置模块,用于发送电子邮件。本文将介绍如何使用smtplib发送带有日历的电子邮件。
以下是使用smtplib发送日历电子邮件的步骤:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
message = MIMEMultipart()
message['Subject'] = 'Python日历电子邮件'
message['From'] = 'sender@example.com'
message['To'] = 'receiver@example.com'
text = '这是一封带有日历的电子邮件。'
message.attach(MIMEText(text))
with open('calendar.ics', 'rb') as f:
calendar_data = f.read()
calendar = MIMEApplication(calendar_data, 'ics')
calendar.add_header('Content-Disposition', 'attachment', filename='calendar.ics')
message.attach(calendar)
server = smtplib.SMTP('smtp.example.com', 587)
server.starttls()
server.login('username', 'password')
server.send_message(message)
server.quit()
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
message = MIMEMultipart()
message['Subject'] = 'Python日历电子邮件'
message['From'] = 'sender@example.com'
message['To'] = 'receiver@example.com'
text = '这是一封带有日历的电子邮件。'
message.attach(MIMEText(text))
with open('calendar.ics', 'rb') as f:
calendar_data = f.read()
calendar = MIMEApplication(calendar_data, 'ics')
calendar.add_header('Content-Disposition', 'attachment', filename='calendar.ics')
message.attach(calendar)
server = smtplib.SMTP('smtp.example.com', 587)
server.starttls()
server.login('username', 'password')
server.send_message(message)
server.quit()
请确保将示例中的'sender@example.com'、'receiver@example.com'、'smtp.example.com'、'username'和'password'替换为相应的值。此外,请确保在相同目录下存在一个名为'calendar.ics'的日历文件。
以上是使用smtplib发送日历电子邮件的Python代码示例。希望对你有所帮助!