python实现发送文本邮件

Posted bluezms

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python实现发送文本邮件相关的知识,希望对你有一定的参考价值。

简单实现了python发送文本邮件

 

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 # @Time    : 2018/4/25 17:09
 5 # @Author  : zms
 6 # @Site    : 
 7 # @File    : SendEmail.py
 8 # @Software: PyCharm Community Edition
 9 
10 import time
11 from email.mime.text import MIMEText
12 import smtplib
13 
14 
15 class SendEmail(object):
16     def __init__(self):
17         self.port = 25
18         self.fromemail = [email protected]
19         self.emailpasswd = ******
20 
21     def sendemail(self, subject, msg, fromemail=None, emailpasswd=None, toemail=None):
22         ‘‘‘实现发送邮件功能函数‘‘‘
23         if fromemail == None | emailpasswd == None:
24             _user = self.fromemail
25             _pwd = self.emailpasswd
26         else:
27             _user = fromemail
28             _pwd = emailpasswd
29         _to = toemail
30         nowtime = time.strftime(%Y-%m-%d %H:%M:%S)
31 
32         msg = MIMEText(msg)
33         msg["Subject"] = subject
34         msg["From"] = _user
35         msg["To"] = _to
36 
37         try:
38             # s = smtplib.SMTP_SSL(‘****.****.cn‘, 25)
39             s = smtplib.SMTP(‘****.****.cn, self.port)
40             s.login(_user, _pwd)
41             s.sendmail(_user, _to, msg.as_string())
42             s.quit()
43             print "[%s]INFO:%s Email send Success!" % (nowtime, _to)
44         except smtplib.SMTPException, e:
45             print "[%s]ERROR:%s Email send Falied,%s" % ((nowtime, e), _to)
46 
47 
48 if __name__ == __main__:
49     email = SendEmail()
50     email.sendemail(test, test, [email protected], ******, [email protected])

 

以上是关于python实现发送文本邮件的主要内容,如果未能解决你的问题,请参考以下文章

我应该如何使用 Outlook 发送代码片段?

python实现发送文本邮件

Python发送邮件

Python发送邮件

干货 | 解放双手,用Python实现自动发送邮件

基于Python实现邮件发送