首页 > 代码库 > python发送邮件

python发送邮件

一、python发送邮件使用smtplib模块,是一个标准包,直接import导入使用即可,代码如下: 

import smtplib
from email.mime.text import MIMEText
email_host = ‘smtp.163.com‘ #邮件服务器地址
email_user = ‘szz_mail@163.com‘ # 发送者账号
email_pwd = ‘woshinige123‘ # 发送者密码,是,授权码(在客户端设置)
maillist =‘511402865@qq.com,1169879121@qq.com‘#收件人邮箱,多个账号的话,用逗号隔开
me = email_user
msg = MIMEText(‘zhangying测试邮件‘) # 邮件内容
msg[‘Subject‘] = ‘zhangying测试邮件‘ # 邮件主题
msg[‘From‘] = me # 发送者账号
msg[‘To‘] = maillist # 接收者账号列表
smtp = smtplib.SMTP(email_host,port=25) # 连接邮箱,传入邮箱地址,和端口号,smtp的端口号是25
smtp.login(email_user, email_pwd) # 发送者的邮箱账号,密码
smtp.sendmail(me, maillist, msg.as_string())
# 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
smtp.quit() # 发送完毕后退出smtp
print (‘email send success.‘)

 

 

二、发送带附件的邮件

import smtplib#smtplib这个模块是管发邮件
from email.mime.text import MIMEText#构造邮件内容
from email.mime.multipart import MIMEMultipart#发带附件的邮件用的
email_host = ‘smtp.163.com‘ #邮箱服务器地址
email_user = ‘szz_mail@163.com‘ # 发送者账号
email_pwd = ‘woshinige123‘# 发送者密码是邮箱的授权码,不是登录的密码
maillist =‘511402865@qq.com,niuhanyang@163.com‘#收件人邮箱,多个账号的话,用逗号隔开
new_msg = MIMEMultipart()#构建了一个能发附件的邮件对象
new_msg.attach(MIMEText(‘这是Python测试发邮件的邮件,不要回复‘))# 邮件内容
new_msg[‘Subject‘] = ‘zhangying‘ # 邮件主题
new_msg[‘From‘] = email_user # 发送者账号
new_msg[‘To‘] = maillist # 接收者账号列表
att = MIMEText(open(‘a.txt‘).read())
att["Content-Type"] = ‘application/octet-stream‘
att["Content-Disposition"] = ‘attachment; filename="haha.txt"‘
new_msg.attach(att)
#以上四行就是附件邮件特有的
smtp = smtplib.SMTP(email_host,port=25) # 连接邮箱,传入邮箱地址,和端口号,smtp的端口号是25
smtp.login(email_user, email_pwd) # 发送者的邮箱账号,密码
smtp.sendmail(email_user, maillist, new_msg.as_string())# 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
smtp.quit() # 发送完毕后退出smtp
print (‘email send success.‘)

 

 

三、封装成一个类

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class SendMail(object):
def __init__(self,username,passwd,recv,title,content,
file=None,
email_host=‘smtp.163.com‘,port=25):
self.username = username
self.passwd = passwd
self.recv = recv
self.title = title
self.content = content
self.file = file
self.email_host = email_host
self.port = port
def send_mail(self):
msg = MIMEMultipart()
#发送内容的对象
if self.file:#处理附件的
att = MIMEText(open(self.file).read())
att["Content-Type"] = ‘application/octet-stream‘
att["Content-Disposition"] = ‘attachment; filename="%s"‘%self.file
msg.attach(att)
msg.attach(MIMEText(self.content))#邮件正文的内容
msg[‘Subject‘] = self.title # 邮件主题
msg[‘From‘] = self.username # 发送者账号
msg[‘To‘] = self.recv # 接收者账号列表
self.smtp = smtplib.SMTP(self.email_host,port=self.port)
#发送邮件服务器的对象
self.smtp.login(self.username,self.passwd)
try:
self.smtp.sendmail(self.username,self.recv,msg.as_string())
except Exception as e:
print(‘出错了。。‘,e)
else:
print(‘发送成功!‘)
def __del__(self):
self.smtp.quit()

if __name__ == ‘__main__‘:#执行当前python文件的时候执行,被别人导入的时候不执行
m = SendMail(
username=‘szz_mail@163.com‘,passwd=‘woshinige123‘,recv=‘niuhanyang@163.com‘,
title=‘新的发送邮件‘,content=‘哈哈哈啊哈哈哈哈‘,file=‘a.txt‘
)
m.send_mail()

 




python发送邮件