您好,登錄后才能下訂單哦!
實例如下所示:
#!/usr/bin/python # -*- coding: UTF-8 -*- import smtplib import email.mime.multipart import email.mime.text from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication def send_email(smtpHost, sendAddr, password, recipientAddrs, subject='', content=''): msg = email.mime.multipart.MIMEMultipart() msg['from'] = sendAddr msg['to'] = recipientAddrs msg['subject'] = subject content = content txt = email.mime.text.MIMEText(content, 'plain', 'utf-8') msg.attach(txt) # 添加附件,傳送D:/mydev/yasuo.rar文件 part = MIMEApplication(open('D:/mydev/6.rar','rb').read()) part.add_header('Content-Disposition', 'attachment', filename="yasuo.rar") msg.attach(part) smtp = smtplib.SMTP() smtp.connect(smtpHost, '25') smtp.login(sendAddr, password) smtp.sendmail(sendAddr, recipientAddrs, str(msg)) print("發送成功!") smtp.quit() try: subject = 'Python 測試郵件' content = '這是一封來自 Python 編寫的測試郵件。' send_email('smtp.163.com', '18310161797@163.com', '郵箱密碼', '526189064@qq.com', subject, content) except Exception as err: print(err)
以上這篇python3 發送任意文件郵件的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。