本文介绍了使用Python如何实现邮件发送的功能,实例介绍了 整个邮件发送处理过程
使用Python用代码的方式实现邮件发送,可以集成到脚本执行过程中,很方便的实现一些邮件通知功能。
代码实例:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#测试python发送邮件
import os
import sys
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.base import MIMEBase
from email import encoders
#参数定义
sender_host = 'smtp.126.com' //其他邮箱比如 qq,163或者新浪邮箱 都需要先确认是否开启了smtp服务
sender_user = 'xxxx@126.com' //邮箱用户名
sender_pwd = 'xxxxxxx' //邮箱密码
sender_name = 'xxxx@126.com' //发送人
recv = 'xxxxxxx@qq.com' //接收人
try:
smtp = smtplib.SMTP();
smtp.connect(sender_host);
smtp.login(sender_user, sender_pwd);
msg = MIMEMultipart('alternative');
msg['Subject'] = 'test mail';
msg['From'] = sender_name;
msg['To'] = recv;
msg.attach(MIMEText("test content mail check"));
#msg.attach(att); //附件添加
smtp.sendmail(sender_name, recv, msg.as_string());
finally:
smtp.quit();
发表评论
取消回复