python发送邮件
时间:2009-01-20 来源:hkebao
import smtplib #调用了现成的包
import string
import os,sys
class netUtil:
# sendMail(errorSubject,errorBody)
def sendMail(self,_mailsubject,_mailbody):
mailserver = 'smtp.126.com'
mailuser = 'hkebao'
mailpassword = '19861104'
mailfrom = '[email protected]'
mailto = ['[email protected]', '[email protected]']
mailsubject = _mailsubject
mailbody = _mailbody
mailbody = string.join(('From: %s' % mailfrom,
'To: %s' % mailto,
'Subject: %s' % mailsubject,
'',
mailbody),'\r\n')
# send mail
try:
smtp = smtplib.SMTP()
# set debug level
smtp.set_debuglevel(0)
smtp.connect(mailserver)
smtp.login(mailuser, mailpassword)
smtp.sendmail(mailfrom,mailto,mailbody)
smtp.quit()
return True
except:
return False
如果想要调用的话就可以直接这样来写就可以了:
if __name__ == '__main__':
ob=netUtil()
ob.sendMail("hello", "Test is First")
以后我想:如果一旦有问题发生了的话就可以发送邮件的形式进行处理判断的哦!
import string
import os,sys
class netUtil:
# sendMail(errorSubject,errorBody)
def sendMail(self,_mailsubject,_mailbody):
mailserver = 'smtp.126.com'
mailuser = 'hkebao'
mailpassword = '19861104'
mailfrom = '[email protected]'
mailto = ['[email protected]', '[email protected]']
mailsubject = _mailsubject
mailbody = _mailbody
mailbody = string.join(('From: %s' % mailfrom,
'To: %s' % mailto,
'Subject: %s' % mailsubject,
'',
mailbody),'\r\n')
# send mail
try:
smtp = smtplib.SMTP()
# set debug level
smtp.set_debuglevel(0)
smtp.connect(mailserver)
smtp.login(mailuser, mailpassword)
smtp.sendmail(mailfrom,mailto,mailbody)
smtp.quit()
return True
except:
return False
如果想要调用的话就可以直接这样来写就可以了:
if __name__ == '__main__':
ob=netUtil()
ob.sendMail("hello", "Test is First")
以后我想:如果一旦有问题发生了的话就可以发送邮件的形式进行处理判断的哦!
相关阅读 更多 +