您好,登錄后才能下訂單哦!
Python 實(shí)現(xiàn)使用office365郵箱的方法?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
MailTools.py
#!/usr/bin/env python3 # coding: utf-8 import smtplib # 加載smtplib模塊 from email.mime.text import MIMEText from email.utils import formataddr from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication import time class SendMail(object): def __init__(self,sender,title,content): self.sender = sender #發(fā)送地址 self.title = title # 標(biāo)題 self.content = content # 發(fā)送內(nèi)容 self.sys_sender = 'xx@office365.com' # 系統(tǒng)賬戶 self.sys_pwd = '123456' # 系統(tǒng)賬戶密碼 def send(self,file_list): """ 發(fā)送郵件 :param file_list: 附件文件列表 :return: bool """ try: # 創(chuàng)建一個(gè)帶附件的實(shí)例 msg = MIMEMultipart() # 發(fā)件人格式 msg['From'] = formataddr(["", self.sys_sender]) # 收件人格式 msg['To'] = formataddr(["", self.sender]) # 郵件主題 msg['Subject'] = self.title # 郵件正文內(nèi)容 msg.attach(MIMEText(self.content, 'plain', 'utf-8')) # 多個(gè)附件 for file_name in file_list: print("file_name",file_name) # 構(gòu)造附件 xlsxpart = MIMEApplication(open(file_name, 'rb').read()) # filename表示郵件中顯示的附件名 xlsxpart.add_header('Content-Disposition','attachment',filename = '%s'%file_name) msg.attach(xlsxpart) # SMTP服務(wù)器 server = smtplib.SMTP("smtp.office365.com", 587,timeout=10) server.ehlo() server.starttls() # 登錄賬戶 server.login(self.sys_sender, self.sys_pwd) # 發(fā)送郵件 server.sendmail(self.sys_sender, [self.sender, ], msg.as_string()) # 退出賬戶 server.quit() return True except Exception as e: print(e) return False if __name__ == '__main__': # 發(fā)送地址 sender = "12345678@qq.com" # 標(biāo)題 title = "測(cè)試告警" # 開(kāi)始時(shí)間 start_time = time.strftime('%Y-%m-%d %H:%M:%S') ip = "xx.xx.xx.xx" # 發(fā)送內(nèi)容 content = "{} ip: {} 掉線".format(start_time,ip) # 附件列表 file_list = [] ret = SendMail(sender, title, content).send(file_list) print(ret,type(ret))
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。