基于python的XML读取和邮件发送方法,适用于linux、T100和windows系统
使用Python编程语言,我们可以轻松地读取XML文件并发送邮件。在linux、T100和windows系统上,我们可以使用以下代码段来实现这个功能:
import xml.etree.ElementTree as ET
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def read_xml_and_send_email():
# 读取XML文件
tree = ET.parse('file.xml')
root = tree.getroot()
# 获取邮件相关信息
sender = root.find('sender').text
receiver = root.find('receiver').text
subject = root.find('subject').text
body = root.find('body').text
# 构建邮件内容
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = receiver
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
# 发送邮件
smtp_obj = smtplib.SMTP('smtp.gmail.com', 587)
smtp_obj.starttls()
smtp_obj.login(sender, 'password')
smtp_obj.send_message(msg)
smtp_obj.quit()
read_xml_and_send_email()
可以根据自己的需求在"file.xml"文件中定义发送邮件的相关信息。执行以上代码,即可读取XML文件并发送邮件。
下载地址
用户评论