1个回答
展开全部
引用头文件
#!/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
创建“连接邮件服务器”函数
def smtp_connect( smtp_server, account, password, target ):
global smtp
global e_smtpserver
global e_account
global e_password
global e_target
global msg
e_smtpserver = smtp_server
e_account = account
e_password = password
e_target = target
try:
smtp = smtplib.SMTP()
smtp.connect(e_smtpserver)
smtp.login(e_account, e_password)
except Exception, e:
print 'Mail connect failed: %s' % e
exit( -1 )
msg = MIMEMultipart('related')
创建“写邮件内容”函数
def smtp_set_content( subject, main_body ):
global msg
msg['Subject'] = subject
body = MIMEText( main_body, 'plain', 'utf-8' )
msg.attach( body )
创建“添加附件”函数
def smtp_set_attachment( file_path ):
global msg
part = MIMEBase( 'application', 'octet-stream' )
part.set_payload( open(file_path,'rb').read() )
encoders.encode_base64(part)
part.add_header( 'Content-Disposition', 'attachment; filename=%s' % file_path.split('/')[-1] )
msg.attach(part)
创建“发送邮件”函数
def smtp_send():
global smtp
global e_smtpserver
global e_account
global e_password
global e_target
global msg
try:
smtp.sendmail( e_account, e_target, msg.as_string() )
except Exception, e:
print 'Mail send Failed: %s' % e
exit( -1 )
使用示例
if __name__ == '__main__':
smtp_connect( 'smtp.xxxx.com.cn', 'xxxx@xxxx.com.cn', 'password',
'xxxx@xxxx.com.cn' )
smtp_set_content( 'Test subject', 'Test_content' )
smtp_set_attachment( os.getcwd() + '/mail.py' )
smtp_send()
#!/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
创建“连接邮件服务器”函数
def smtp_connect( smtp_server, account, password, target ):
global smtp
global e_smtpserver
global e_account
global e_password
global e_target
global msg
e_smtpserver = smtp_server
e_account = account
e_password = password
e_target = target
try:
smtp = smtplib.SMTP()
smtp.connect(e_smtpserver)
smtp.login(e_account, e_password)
except Exception, e:
print 'Mail connect failed: %s' % e
exit( -1 )
msg = MIMEMultipart('related')
创建“写邮件内容”函数
def smtp_set_content( subject, main_body ):
global msg
msg['Subject'] = subject
body = MIMEText( main_body, 'plain', 'utf-8' )
msg.attach( body )
创建“添加附件”函数
def smtp_set_attachment( file_path ):
global msg
part = MIMEBase( 'application', 'octet-stream' )
part.set_payload( open(file_path,'rb').read() )
encoders.encode_base64(part)
part.add_header( 'Content-Disposition', 'attachment; filename=%s' % file_path.split('/')[-1] )
msg.attach(part)
创建“发送邮件”函数
def smtp_send():
global smtp
global e_smtpserver
global e_account
global e_password
global e_target
global msg
try:
smtp.sendmail( e_account, e_target, msg.as_string() )
except Exception, e:
print 'Mail send Failed: %s' % e
exit( -1 )
使用示例
if __name__ == '__main__':
smtp_connect( 'smtp.xxxx.com.cn', 'xxxx@xxxx.com.cn', 'password',
'xxxx@xxxx.com.cn' )
smtp_set_content( 'Test subject', 'Test_content' )
smtp_set_attachment( os.getcwd() + '/mail.py' )
smtp_send()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询