mail 发送邮件

来源:互联网 发布:添加URL端口 编辑:程序博客网 时间:2024/06/04 01:29
#!/usr/bin/env python
#-*-coding:utf-8-*-


import smtplib  
import os
from email.mime.text import MIMEText 
from email.mime.multipart import MIMEMultipart 
from email.mime.base import MIMEBase 
from email import encoders 


# 定义主题
subject = ' %s : subject'  % "aa"
# 定义正文
text = "%s : text"  % "bb"


msg = MIMEMultipart('related') 
# 正文  
msg['Subject'] = subject  
body = MIMEText(text, 'plain', 'utf-8')
msg.attach( body )


# 构造附件
f = 'fun.so'
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(os.getcwd()+'/fun.so','rb').read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename=%s' % f ) 
msg.attach(part)


# 定义邮件服务器
e_smtpserver = 'smtp.httc.com.cn'
# 定义发送人
e_account = 'changxuesong@httc.com.cn'
# 定义发送邮箱密码
e_password = 'guiji198'
# 定义接收人
tolist = 'changxuesong@httc.com.cn'


# 发送
try:
    smtp = smtplib.SMTP()
    smtp.connect(e_smtpserver) 
    smtp.login(e_account, e_password)
    smtp.sendmail(e_account, tolist, msg.as_string())  
except Exception, e:
    print( " send mail failed: %s", e ) 
finally:
    smtp.quit()
0 0
原创粉丝点击