python之smtplib发邮件
第一版: 认证发信,不支持附件
#!/usr/bin/env python # --------------------------------------- # author : Geng Jie # email : gengjie@outlook.com # # Create Time: 2016/3/13 15:21 # ---------------------------------------- import smtplib # 定义邮件服务器地址以及端口 SMTP_HOST = 'smtp.xxx.com' SMTP_PORT = '25' # 定义发件人,密码,收件人 MAIL_USER = 'xxx@xxx.com' PASSWD = 'xxxxxxx' MAIL_TO = 'xxxx@xxx.com' # 定义邮件主题,内容 MAIL_SUB = '测试邮件' MAIL_CON = ''' Hi, python: 最近好吗?好久不见. ''' message = """From: {0} To: {1} Subject: {2} {3} """.format(MAIL_USER, MAIL_TO, MAIL_SUB, MAIL_CON) try: smtpObj = smtplib.SMTP(SMTP_HOST, SMTP_PORT) smtpObj.login(MAIL_USER, PASSWD) smtpObj.sendmail(MAIL_USER, MAIL_TO, message.encode('GBK')) print('Successfully send email !') smtpObj.quit() except smtplib.SMTPException as e: print(e) print('Error: unable to send mail !')
第二版,写成模块,可调用使用
1 import smtplib 2 from email.mime.text import MIMEText 3 import socket 4 5 6 class EasySendmail: 7 ''' e = ClassMail.EasySendmail(Port=25, auth=True) 8 default smtp port : 25 9 # 默认smtp的端口是25,若需要更改则设置port=xxx 10 # 默认是认证发信,若要匿名发信则需设置auth=False 11 ''' 12 def __init__(self, port=25, auth=True): 13 self.port = port 14 self.auth = auth 15 16 @classmethod 17 def setmail(cls, host, sender, passwd, mail_to, mail_sub, content): 18 '''You must set attr: 19 Host : mailServer exp: smtp.163.com 20 Sender: mail from user 21 Passwd: user passwd 22 Mail_to: To user 23 Mail_sub: mail subject 24 content: message ''' 25 26 cls.host = host 27 cls.sender = sender 28 cls.passwd = passwd 29 cls.mail_to = ';'.join(mail_to) 30 cls.mail_sub = mail_sub 31 cls.content = content 32 33 @property 34 def sendmail(self): 35 '''sendmail: Start Connect server to send mail .''' 36 self.message = """From: {0} 37 To: {1} 38 MIME-Version: 1.0 39 Content-type: text/plain 40 Subject: {2} 41 42 {3} 43 """.format(self.sender, self.mail_to, self.mail_sub, self.content) 44 45 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 46 s.settimeout(10) 47 try: 48 s.connect((self.host, self.port)) 49 # print('Connect {0}:{1} successfuly !'.format(self.host, self.port)) 50 except Exception: 51 print('Error: Can connect {0}:{1} !'.format(self.host, self.port)) 52 s.close() 53 54 try: 55 print(self.auth) 56 smtpobj = smtplib.SMTP(self.host, self.port) 57 if self.auth is True: 58 smtpobj.login(self.sender, self.passwd) 59 smtpobj.sendmail(self.sender, self.mail_to, self.message.encode('GBK')) 60 print('Successfully send email !') 61 smtpobj.quit() 62 except smtplib.SMTPAuthenticationError as error: 63 print(error) 64 print('认证失败,请核对用户名和密码.') 65 except smtplib.SMTPException as error: 66 print(error) 67 print('Error: unable to send mail !') 68 except Exception as e: 69 print(e)
调用方法:
1 #!/usr/bin/env python 2 # --------------------------------------- 3 # author : Geng Jie 4 # email : gengjie@outlook.com 5 # 6 # Create Time: 2016/3/14 13:19 7 # ---------------------------------------- 8 9 import ClassMail 10 11 if __name__ == '__main__': 12 # 初始化实例 13 # 默认smtp的端口是25,若需要更改则设置port=xxx 14 # 默认是认证发信,若要匿名发信则需设置auth=False 15 e = ClassMail.EasySendmail() 16 17 # 定义邮件服务器 18 e.host = 'smtp.163.com' 19 # 定义发件人 20 e.sender = 'xxx@163.com' 21 # 定义发件人密码 22 e.passwd = '*******' 23 # 定义收件人,多个收件人,需用,号隔开 24 e.mail_to = ['xxxx@xxx.com'] 25 26 # 定义邮件主题 27 e.mail_sub = 'EasyMail Test' 28 # 定义邮件正文 29 e.content = """Hi, Tony: 30 31 How are you ? 32 """ 33 # 调用发信方法发送信件 34 e.sendmail
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。
http://www.cnblogs.com/topicjie/