1. 程式人生 > >使用QQ郵箱發送郵件並添加附件

使用QQ郵箱發送郵件並添加附件

mail smtplib exception 添加 ica rom ring lib except

def send_email(str):
fromaddr = ‘[email protected]‘ # 發送方郵箱
password = ‘xxxxxxxxx‘ # 填入發送方郵箱的授權碼
toaddrs = [str] # 收件人郵箱

content = ‘最新生成內容‘
textApart = MIMEText(content)

zipFile = file_dir + ‘/aa_file.zip‘
zipApart = MIMEApplication(open(zipFile, ‘rb‘).read())
zipApart.add_header(‘Content-Disposition‘, ‘attachment‘, filename=‘QianBi.zip‘)

m = MIMEMultipart()
m.attach(textApart)
m.attach(zipApart)
m[‘Subject‘] = ‘千幣分析‘

try:
s = smtplib.SMTP_SSL("smtp.qq.com", 465) # 郵件服務器及端口號
s.login(fromaddr, password)
s.sendmail(fromaddr, toaddrs, m.as_string())
print(‘success‘)
s.quit()
except smtplib.SMTPException as e:
print(‘error:‘, e)

if __name__ == ‘__main__‘:
send_email()

使用QQ郵箱發送郵件並添加附件