python发送网易邮件
无附件 from smtplib import SMTP from email.header import Header from email.mime.text import MIMEText def main(): sender = ‘1355***‘ receivers = ‘1047**‘ message = MIMEText(‘用python发送邮件的实例代码‘,‘plain‘,‘utf-8‘) message[‘Subject‘] = Header(‘实例代码邮件‘, ‘utf-8‘) message[‘From‘]=sender message[‘To‘]=receivers smtpter = SMTP(‘smtp.163.com‘,25) smtpter.set_debuglevel(1) smtpter.login(sender,‘******‘) smtpter.sendmail(sender,receivers,message.as_string()) smtpter.quit() print(‘邮件发送完成‘) if __name__ == ‘__main__‘: main()
有附件
from smtplib import SMTP from email.header import Header from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart def main(): sender = ‘135***‘ receivers = ‘1047***‘ # 创建一个带附件的邮件消息对象 message = MIMEMultipart() message[‘From‘]=sender message[‘To‘]=receivers #主题如果是中文要五个字以上 message[‘Subject‘] = Header(‘本月数据文件‘, ‘utf-8‘) # 创建文本内容 text_content = MIMEText(‘附件中有本月的数据请查收‘,‘plain‘,‘utf-8‘) # 将文本内容添加到邮件消息对象中 message.attach(text_content) #打开附件内容 with open(‘helloworld.py‘) as f: #读取附件 txt = MIMEText(f.read(),‘base64‘,‘utf-8‘) #以文本附件传送 txt[‘Content-Type‘]=‘text/plain‘ txt[‘Content-Disposition‘]=‘attachment;filename=helloworld.py‘ #添加入邮件消息对象 message.attach(txt) smtpter = SMTP(‘smtp.163.com‘,25) smtpter.set_debuglevel(1) smtpter.login(sender,‘******‘) smtpter.sendmail(sender,receivers,message.as_string()) smtpter.quit() print(‘邮件发送完成‘) if __name__ == ‘__main__‘: main()
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16