本文實例為大家分享了python實現發送郵件功能的具體代碼,供大家參考,具體內容如下
依賴:
Python代碼實現發送郵件,使用的模塊是smtplib、MIMEText,實現代碼之前需要導入包:
import smtplibfrom email.mime.text import MIMEText
使用163郵件發送郵件,具體代碼實現如下:
import smtplibfrom email.mime.text import MIMEText'''發送郵件函數,默認使用163smtp:param mail_host: 郵箱服務器,16郵箱host: smtp.163.com:param port: 端口號,163郵箱的默認端口是 25:param username: 郵箱賬號 xx@163.com:param passwd: 郵箱密碼(不是郵箱的登錄密碼,是郵箱的授權碼):param recv: 郵箱接收人地址,多個賬號以逗號隔開:param title: 郵件標題:param content: 郵件內容:return:'''def send_mail(username, passwd, recv, title, content, mail_host='smtp.163.com', port=25): msg = MIMEText(content) # 郵件內容 msg['Subject'] = title # 郵件主題 msg['From'] = username # 發送者賬號 msg['To'] = recv # 接收者賬號列表 smtp = smtplib.SMTP(mail_host, port=port) # 連接郵箱,傳入郵箱地址,和端口號,smtp的端口號是25 smtp.login(username, passwd) # 登錄發送者的郵箱賬號,密碼 # 參數分別是 發送者,接收者,第三個是把上面的發送郵件的 內容變成字符串 smtp.sendmail(username, recv, msg.as_string()) smtp.quit() # 發送完畢后退出smtp print('email send success.')if __name__ == '__main__': email_user = 'xxxx@163.com' # 發送者賬號 email_pwd = 'xxxxx' # 發送者密碼,授權碼 maillist = 'xxxx@qq.com' content = '這里是郵件內容' send_mail(email_user, email_pwd, maillist, title, content)163郵箱的授權碼獲取方法如下:
1. 登錄163郵箱,點擊設置-POP3/SMTP/IMAP,如下:

2. 開啟SMTP服務且可以查詢SMTP的host地址:

3. 點擊客戶端授權密碼,可以重置授權碼:

使用QQ郵件發送郵件,具體代碼實現如下:
import smtplibfrom email.mime.text import MIMEText'''發送郵件函數,默認使用163smtp:param mail_host: 郵箱服務器,qq郵箱host: smtp.qq.com:param port: 端口號,qq郵箱的默認端口是: 456:param username: 郵箱賬號 xx@163.com:param passwd: 郵箱密碼(不是郵箱的登錄密碼,是郵箱的授權碼):param recv: 郵箱接收人地址,多個賬號以逗號隔開:param title: 郵件標題:param content: 郵件內容:return:'''#qq郵箱發送郵件def send_mail(username, passwd, recv, title, content, mail_host='smtp.qq.com', port=456): msg = MIMEText(content) # 郵件內容 msg['Subject'] = title # 郵件主題 msg['From'] = username # 發送者賬號 msg['To'] = recv # 接收者賬號列表 smtp = smtplib.SMTP_SSL(mail_host, port=port) # 連接郵箱,傳入郵箱地址,和端口號,smtp的端口號是25 smtp.login(username, passwd) # 登錄發送者的郵箱賬號,密碼 # 參數分別是 發送者,接收者,第三個是把上面的發送郵件的 內容變成字符串 smtp.sendmail(username, recv, msg.as_string()) smtp.quit() # 發送完畢后退出smtp print('email send success.')if __name__ == '__main__': email_user = 'xxx@qq.com' # 發送者賬號 email_pwd = 'WOSHINIGE123' # 發送者密碼,授權碼 maillist = 'xxxx@qq.com' content = '這里是郵件內容' send_mail(email_user, email_pwd, maillist, title, content)多個 收件人且帶附件:
# import smtplib# from email.mime.text import MIMEText# '''# 發送郵件函數,默認使用163smtp# :param mail_host: 郵箱服務器,qq郵箱host: smtp.163.com# :param port: 端口號,qq郵箱的默認端口是: 25# :param username: 郵箱賬號 xx@163.com# :param passwd: 郵箱密碼(不是郵箱的登錄密碼,是郵箱的授權碼)# :param recv: 郵箱接收人地址,多個賬號以逗號隔開# :param title: 郵件標題# :param content: 郵件內容# :return:# '''import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipart#郵箱服務器地址email_host = 'smtp.163.com'#發送者email_user = 'xxxx@163.com'#授權碼email_pwd = 'WOSHINIGE123'#多個收件人,使用分號分隔maillist ='xxxx@qq.com;aaaa@qq.com'#對多個收件人進行分割,以;為標準,返回結果是list['xxxx@qq.com','aaaa@qq.com']email_info = maillist.split(';')#構造一個發附件的郵件內容對象new_msg = MIMEMultipart()#郵件正文內容new_msg.attach(MIMEText('test email.....'))#郵件主題new_msg['Subject'] = 'test email'#郵件發送者new_msg['From'] = email_user#郵件接收者,多個收件人的賬號使用,連接,傳入類型是strnew_msg['To'] = ','.join(email_info)#打開a.txt讀取文本內容att = MIMEText(open('a.txt').read())att["Content-Type"] = 'application/octet-stream'# 發送附件就得這么寫,固定格式,filename指定附件的名字att["Content-Disposition"] = 'attachment; filename="haha.txt"'new_msg.attach(att)# 連接郵箱,傳入郵箱地址,和端口號,smtp的端口號是25smtp = smtplib.SMTP(email_host, port=25)# 發送者的郵箱賬號,密碼,先登錄smtp.login(email_user, email_pwd)smtp.sendmail(email_user, email_info, new_msg.as_string())smtp.quit()print('email send success.')多個收件人時賬號之間使用分號;分割,進行smtp.sendmail(傳入的多個收件人類型是list);new_msg['TO'] = recv,接收的類型是str
使用類實現郵件的發送,即可發送多人也可發送附件:
import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartclass 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, encoding='utf-8').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 # 發送者賬號 #將多個賬號'aaa.@qq.com;bbbb@163.com' 以分號分割,分割為list self.recv = self.recv.split(';') if isinstance(self.recv, list): msg['To'] = ','.join(self.recv) else: msg['To'] = self.recv # 接收者賬號列表 if self.username.endswith('qq.com'): #如果發送者是qq郵箱 self.smtp = smtplib.SMTP_SSL(self.email_host, port=self.port) else: 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('發送成功!') self.smtp.quit()if __name__ == '__main__': m = SendMail( username='zzzzz@163.com', passwd='xxxxxxx',file='a.txt', recv='xxxxxx@qq.com;xxxxxx@qq.com', ) m.send_ma以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答