python--发送邮件
1 # 发送邮件前需要开通邮箱授权码,来替代登录密码 2 # 账号:fasongzhe@qq.com 授权码:bsvtiasolcrmbgfi 3 # 发邮件是固定格式,记住就行 4 5 import smtplib 6 7 #不带附件的邮件 8 # from email.mime.text import MIMEText 9 # email_host='smtp.qq.com' #邮箱地址 10 # email_user='fasongzhe@qq.com' #发送者账号 11 # email_pwd='bsvtiasolcrmbgfi' #发送者授权码 12 # maillist='jieshouzhe@qq.com' #收件人账号,多个时以逗号分隔 13 # msg=MIMEText('邮件内容,发送测试数据,哈哈哈!') #邮件内容 14 # msg['Subject']='邮件标题' #邮件标题 15 # msg['From']=email_user #发送者账号 16 # msg['To']=maillist #接受者账号列表 17 # smtp=smtplib.SMTP_SSL(email_host,465) #连接邮箱,465为ssl加密端口 18 # smtp.login(email_user,email_pwd) #发送者账号密码 19 # smtp.sendmail(email_user,maillist,msg.as_string()) 20 # #参数分别是发送者、接收者,第三个是把邮件内容变成字符串 21 # smtp.quit() 22 # print('email send success!') 23 24 #带附件的邮件 25 from email.mime.text import MIMEText 26 from email.mime.multipart import MIMEMultipart 27 email_host='smtp.qq.com' #邮箱地址 28 email_user='发送者@qq.com' #发送者账号 29 email_pwd='bsvtiasolcrmbgfi' #发送者授权码 30 maillist=['jieshouzhe1@qq.com','jieshouzhe2@qq.com'] #收件人账号,多个时以逗号分隔 31 msg=MIMEMultipart() #构造一个发送附件的对象 32 msg.attach(MIMEText('邮件正文,测试')) #发送文字内容 33 msg['Subject']='邮件标题' #邮件标题 34 msg['From']=email_user #发送者账号 35 msg['To']=','.join(maillist) #接受者账号列表 36 att1=MIMEText(open('a.txt',encoding='utf-8').read()) #附件 37 att1['Content-Type']='application/octet-stream' 38 att1['Content-Disposition']='attachement;filename="%s"' %'a.txt' 39 #这两句是必须写的,filename是附件名字 40 msg.attach(att1) #把附件加到邮件对象里 41 smtp=smtplib.SMTP_SSL(email_host,465) #连接邮箱,465为SSL加密端口 42 smtp.login(email_user,email_pwd) #发送者账号密码 43 smtp.sendmail(email_user,maillist,msg.as_string()) 44 smtp.quit() 45 print('email send success!')
发送邮件的类,其中判断是否为qq邮箱,这里只是做了个例子,只要是使用ssl加密的都可以这样做,修改下判断条件即可。
1 import smtplib 2 from email.mime.text import MIMEText 3 from email.mime.multipart import MIMEMultipart 4 5 class SendMail(object): 6 def __init__(self,user,pwd,recv,title,content, 7 file=None,host='smtp.qq.com',port=25): 8 self.user=user 9 self.pwd=pwd 10 self.recv=recv 11 self.title=title 12 self.content=content 13 self.file=file 14 self.host=host 15 self.port=port 16 def send_mail(self): 17 msg=MIMEMultipart() #发送内容对象 18 if self.file: 19 att = MIMEText(open(self.file, encoding='utf-8').read()) 20 att["Content-Type"] = 'application/octet-stream' 21 att["Content-Disposition"] = 'attachment; filename="%s"' % self.file 22 msg.attach(att) 23 msg.attach(MIMEText(self.content)) # 邮件正文的内容 24 msg['Subject'] = self.title # 邮件主题 25 msg['From'] = self.user # 发送者账号 26 msg['To'] = ','.join(self.recv) # 接收者账号列表 27 if self.user.endswith('qq.com'): 28 self.smtp = smtplib.SMTP_SSL(self.host, port=self.port) 29 else: 30 self.smtp = smtplib.SMTP(self.host, port=self.port) 31 self.smtp.login(self.user,self.pwd) 32 try: 33 self.smtp.sendmail(self.user,self.recv,msg.as_string()) 34 except Exception as e: 35 print('出错了。。',e) 36 else: 37 print('发送成功!') 38 self.smtp.quit() 39 40 41 if __name__ == '__main__': 42 m = SendMail( 43 user='fasongzhe@qq.com',pwd='bsvtiasolcrmbgfi',recv=['jieshouzhe1@qq.com','jieshouzhe2@qq.com'], 44 title='类发送邮件',content='走你',host='smtp.qq.com',port=465,file='a.txt' 45 ) 46 m.send_mail()