Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1651873
  • 博文数量: 245
  • 博客积分: 10378
  • 博客等级: 上将
  • 技术积分: 2571
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 08:19
文章分类

全部博文(245)

文章存档

2013年(4)

2012年(8)

2011年(13)

2010年(68)

2009年(152)

分类: Python/Ruby

2011-12-03 17:43:17

  1. import smtplib, mimetypes
  2. from email.mime.text import MIMEText
  3. from email.mime.multipart import MIMEMultipart
  4. from email.mime.image import MIMEImage
  5. import os

  6. class smtpclient:
  7.     port=25
  8.     stmpAddr= None
  9.     username=None
  10.     password=None
  11.     localMaill=None
  12.     mailserver=None
  13.     debugLevel=0
  14.     def init_smtp(self):
  15.         pass
  16.     def relase_smtp(self):
  17.         if self.mailserver != None :
  18.             self.mailserver.close()
  19.                             
  20.     def set_debugLevel(self,_level):
  21.         self.debugLevel = _level
  22.     def open_smtp(self,_host,_port):
  23.         self.host = _host
  24.         self.port = _port
  25.         try:
  26.             self.mailserver=smtplib.SMTP(_host,_port)
  27.             if self.debugLevel > 0:
  28.                 print("smtplib init Ok")
  29.             return True
  30.         except:
  31.             self.mailserver=None
  32.             if self.debugLevel > 0:
  33.                 print("smtplib init error")
  34.             return False
  35.     def login_mail(self,_user,_password):
  36.         if self.mailserver != None:
  37.             self.username = _user
  38.             self.password = _password
  39.             try:
  40.                 self.mailserver.login(_user, _password)
  41.                 if self.debugLevel > 0:
  42.                     print("smtplib login Ok")
  43.                 return True
  44.             except:
  45.                 if self.debugLevel > 0:
  46.                     print("smtplib login error")
  47.                 return False
  48.         else:
  49.             return False
  50.     def close_mail(self):
  51.         if self.mailserver != None:
  52.             self.mailserver.close()
  53.             if self.debugLevel > 0:
  54.                 print("close mail Ok \n")
  55.             return True
  56.         else:
  57.             return False
  58.     def send_mail(self,_from,_to,_title,_msg):
  59.         if self.mailserver != None:
  60.             try:
  61.                 #mail head
  62.                 msg = MIMEMultipart()
  63.                 msg['From'] = _from
  64.                 msg['To'] = _to
  65.                 msg['Subject'] = _title
  66.                 #mail msg
  67.                 txt = MIMEText(_msg)
  68.                 msg.attach(txt)
  69.                 self.mailserver.sendmail(_from, _to, msg.as_string())
  70.                 if self.debugLevel > 0:
  71.                     print("smtplib send Ok")
  72.                 return True
  73.             except:
  74.                 if self.debugLevel > 0:
  75.                     print("smtplib send error")
  76.                 return False
  77.         else:
  78.             return False
  79.                     
  80. mail =smtpclient()
  81. mail.set_debugLevel(1)
  82. mail.open_smtp("smtp.yeah.net", 25)
  83. mail.login_mail("xxxx", "xxxxx")
  84. mail.send_mail("xxxxx@yeah.net", "xxxx@163.com", "Demo for python", "hello wolrd\n")
  85. mail.close_mail()
阅读(2412) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~