python SMTP发送邮件的详细介绍(附代码)


当前第2页 返回上一页

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

import smtplib

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

from email.mime.base import MIMEBase

 

class SendEmail(object):

    '''

    发送邮件模块封装,属性均从config.ini文件获得

    '''

    def __init__(self, smtpServer, mailPort, mailSender, mailPwd, mailtoList, mailSubject): 

        self.mail_smtpserver = smtpServer

        self.mail_port = mailPort

        self.mail_sender = mailSender

        self.mail_pwd = mailPwd

        # 接收邮件列表

        self.mail_receiverList = mailtoList

        self.mail_subject = mailSubject

        # self.mail_content = mailContent

 

    def sendFile(self, reportFile):

        '''

        发送各种类型的附件

        '''

        # 构建根容器

        msg = MIMEMultipart()

         

        # 邮件正文部分body,1、可以用HTML自己自定义body内容;2、读取其他文件的内容为body

        # body = "您好,<p>这里是使用Python登录邮箱,并发送附件的测试<\p>"

        with open(reportFile,'r',encoding='UTF-8') as f:

            body = f.read()

             

        # _charset 是指Content_type的类型

        msg.attach(MIMEText(_text=body, _subtype='html', _charset='utf-8')) 

 

        # 邮件主题、发送人、收件人、内容

        msg['Subject'] = self.mail_subject  # u'自动化测试报告'

        msg['from'] = self.mail_sender

        msg['to'] = self.mail_pwd

 

        # 添加附件

        attachment = MIMEText(_text=open(reportFile, 'rb').read(), _subtype='base64',_charset= 'utf-8')

        attachment['Content-Type'] = 'application/octet-stream'

        attachment['Content-Disposition'] = 'attachment;filename = "result.html"'

        msg.attach(attachment)

 

        try:

            smtp = smtplib.SMTP_SSL(host=self.mail_smtpserver, port=self.mail_port)  # 继承自SMTP

        except:

            smtp = smtplib.SMTP()

            smtp.connect(self.mail_smtpserver, self.mail_port)

 

        # smtp.set_debuglevel(1)

        # 创建安全连接,加密SMTP

        smtp.starttls()     # Puts the connection to the SMTP server into TLS mode.

        # 用户名和密码

        smtp.login(user=self.mail_sender, password=self.mail_pwd)

 

        # 函数:sendmail(self, from_addr, to_addrs, msg, mail_options=[],rcpt_options=[]):

        smtp.sendmail(self.mail_sender, self.mail_receiverList, msg.as_string())

        smtp.quit()

 

# 调试代码

if __name__ == "__main__":

    mail_smtpserver = 'smtp.qq.com'

    mail_port = 587

    mail_sender = '@qq.com'

    mail_pwd = '' 

    mail_receiverList = ['@qq.com', '@163.com']

    mail_subject = u'自动化测试报告'

    s = SendEmail(mail_smtpserver, mail_port, mail_sender, mail_pwd, mail_receiverList, mail_subject)

    s.sendFile('F:\Python_project\PythonLearnning_2018\send_email\sendEmail_Test.html.tar.gz')

    print('--- test end --- ')

以上就是python SMTP发送邮件的详细介绍(附代码)的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

Python判断一个集合是否为另一个集合的子集方法

Python如何让字典保持有序(代码)

Python中╲t是什么

vs可以写Python

Python文件的后缀名是什么

Python是什么

Python之中正则表达式详解(实例分析)

Python什么意思

Python用input输入列表的方法

如何移除Python列表项的值Python remove()方法最有效

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...