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加密的均可以這樣作,修改下判斷條件便可。app
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()