Python 發郵件例子

Python 發郵件例子python


例子

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2019-04-23 16:12:33
# @Author : BenLam
# @Link : https://www.cnblogs.com/BenLam/

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart


#發送郵箱服務器
smtpserver='smtp.mxhichina.com'

#發送郵箱用戶/密碼
user='tester@qq.com'
password='123456'

#發送人
sender='tester@qq.com'
#收件人
receiver='tester@qq.com'
#主題
subject='test E-mail'
msgRoot=MIMEMultipart('test')

#正文
content='測試郵件地址!'
cont=MIMEText(content,'plain','utf-8')

#附件
file=open('C:\\bug.txt','rb').read()
mark=MIMEText(file,'base64','utf-8')
mark["Content-Type"]='application/octet-stream'
mark["Content-Disposition"]='attachment;filename="bug.txt"'

msgRoot['Subject'] = subject
msgRoot.attach(cont)
msgRoot.attach(mark)

#開始發送郵件
smtp=smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()

結果:服務器

相關文章
相關標籤/搜索