python发送Email笔记

发送文本(HTML链接)的Email

  • 使用到的包和插件
from email.mime.text import MIMEText
from email.header import Header
import smtplib
  • 配置邮件服务器地址
	#163邮箱测试sender_host = "smtp.163.com"
  • 邮件登陆配置
	#邮件用户名sender_user = "****@163.com"#邮件密码sender_pass = "*******"#设置收件人receiver = ["****@163.com"]
  • 配置发送内容
    text = '

Python 邮件发送测试...

这是一个链接

'
#配置邮件对象,类型为text/html(超链接内容)msg_root = MIMEText(text,"html","utf-8")#配置邮件对象,类型为text/plain(正文内容)#text1 = "Hello Word 我是Python"#msg_root = MIMEText(text1,"plain",urf-8)#配置邮件主题名称msg_root["Subject"] = Header("主题","utf-8")#配置发送邮件中发送者的名称msg_root["From"] = Header("发送者名称","utf-8")#配置邮件中接收人名称msg_root["To"] = Header("接受者名称","utf-8")
  • 配置连接163 SMTP服务
    smObj = smtplib.SMTP()#链接163服务端口号为25conn = smObj.connect(sender_host,25)print("链接服务connect:",conn)
#连接163服务成功日志
链接服务connect: (220, b'163.com Anti-spam GT for Coremail System (163com[20141201])')
  • 登陆163账户
	#登陆邮箱log = smObj.login(sender_user,sender_pass)print("登陆login:",log)
#登陆成功日志
登陆login: (235, b'Authentication successful')
  • 开始发送邮件
	smObj.sendmail(sender_user,receiver,msg_root.as_string())

整体代码

from email.mime.text import MIMEText
from email.header import Header
import smtplibdef email_163():#设置邮件服务器sender_host = "smtp.163.com"#邮件用户名sender_user = "***@163.com"#邮件密码sender_pass = "******"#设置收件人(可以同时发送多个用户邮箱)receiver= ["***@163.com","****@163.com"]#书写内容text = '

Python 邮件发送测试...

这是一个链接

'
#设置邮件对象,类型为text/html(超链接)msg_root = MIMEText(text,"html","utf-8")#设置邮件对象,类型为text/plain(正文内容)#text = "Hello Word 我是Python"#msg_root = MIMEText(text,"plain",urf-8)#发送者名称msg_root["From"] = Header(sender_user,"utf-8")#接收人名称msg_root["To"] = Header(receiver,"utf-8")#设置主题名称msg_root["Subject"] = Header("主题","utf-8")try:#配置SMTP服务smObj = smtplib.SMTP()#链接163服务conn = smObj.connect(sender_host,25)print("链接服务成功connect:",conn)except smtplib.SMTPConnectError:print("链接服务失败")print(smtplib.SMTPConnectError)try:#登陆邮箱log = smObj.login(sender_user,sender_pass)print("登陆login:",log)except smtplib.SMTPAuthenticationError:print("用户名密码错误")print(smtplib.SMTPAuthenticationError)else:#发送邮件try:smObj.sendmail(sender_user,receiver,msg_root.as_string())print("发送成功")except smtplib.SMTPRecipientsRefused:print("发送失败")finally:smObj.quit()if __name__ == '__main__':email_163()

发送带附件的Email

发送带有附件的邮件时需要使用MIMEMultipart,还有发送文本和html的MIMWText,还有一个是发送图片的MIMEImage,其中MIMEMultipart有三种不同的基本类型格式:mixed、alternative、related

MIMEMultipart详情描述博客链接

  • 配置邮件服务器地址
	#163邮箱测试sender_host = "smtp.163.com"
  • 邮件登陆配置
	#邮件用户名sender_user = "****@163.com"#邮件密码sender_pass = "*******"#设置收件人receiver = ["****@163.com"]
  • 配置发送内容
	#创建一个带有附件的实例attachment = MIMEMultipart()#配置邮件主题名称attachment ["Subject"] = Header("主题","utf-8")#配置发送邮件中发送者的名称attachment ["From"] = Header("发送者名称","utf-8")#配置邮件中接收人名称attachment ["To"] = Header("接受者名称","utf-8")#加入文本描述信息text = "这是一个带有附件的邮件信息测试"attachment .attach(MIMEText(text,"plain","utf-8"))#读取文件信息att = MIMEText(open(r"E:\xyh\schtasks.txt","rb").read(),"base64","utf-8")print("读取的文件",att.as_string())#配置类型att["Content-Type"] = "application/octet-stream"#配置处理内容att["content-Disposition"] = "attachment;filename='schtasks.txt'"#将附件添加到创建的实例中attachment .attach(att)

整体代码

from email.mime.text import MIMEText
from email.header import Header
import smtplibdef email_163():#设置邮件服务器sender_host = "smtp.163.com"#邮件用户名sender_user = "***@163.com"#邮件密码sender_pass = "******"#设置收件人(可以同时发送多个用户邮箱)receiver= ["***@163.com","****@163.com"]#创建一个带有附件的实例attachment = MIMEMultipart()#配置邮件主题名称attachment ["Subject"] = Header("主题","utf-8")#配置发送邮件中发送者的名称attachment ["From"] = Header("发送者名称","utf-8")#配置邮件中接收人名称attachment ["To"] = Header("接受者名称","utf-8")#加入文本描述信息text = "这是一个带有附件的邮件信息测试"attachment .attach(MIMEText(text,"plain","utf-8"))#读取文件信息(开始添加附件信息)att = MIMEText(open(r"E:\xyh\schtasks.txt","rb").read(),"base64","utf-8")print("读取的文件",att.as_string())#配置类型att["Content-Type"] = "application/octet-stream"#配置处理内容att["content-Disposition"] = "attachment;filename='schtasks.txt'"#将附件添加到创建的实例中attachment .attach(att)try:#配置SMTP服务smObj = smtplib.SMTP()#链接163服务conn = smObj.connect(sender_host,25)print("链接服务成功connect:",conn)except smtplib.SMTPConnectError:print("链接服务失败")print(smtplib.SMTPConnectError)try:#登陆邮箱log = smObj.login(sender_user,sender_pass)print("登陆login:",log)except smtplib.SMTPAuthenticationError:print("用户名密码错误")print(smtplib.SMTPAuthenticationError)else:#发送邮件try:smObj.sendmail(sender_user,receiver,attachment .as_string())print("发送成功")except smtplib.SMTPRecipientsRefused:print("发送失败")finally:smObj.quit()if __name__ == '__main__':email_163()


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部