Simulation before sending the message, you need to open the SMTP service
Operation steps: open the mailbox, Settings - Account - start the service
1.python send mail function built-in
(1) text
Import smtplib from email.mime.text Import MimeText from email.header Import Header SENDER = ' sender mailbox qq.com @ ' password = ' authorization code ' Receiver = 'reception by mail qq.com @ ' # Send the message subject subject = ' the Python Test in email ' # written in HTML type message body MSG = MimeText ( ' <HTML> <h1 of> Hello! </ h1 of> </ HTML> ' , ' HTML ' , 'utf-8') MSG [ ' the Subject ' ] = Header (Subject, ' UTF-. 8 ' ) # E-mail SMTP = smtplib.SMTP () smtp.connect ( " smtp.qq.com " ) # connection mail service smtp.login (sender, password) # login smtp.sendmail (SENDER, Receiver, msg.as_string ()) # specified sender, recipient, body smtp.quit ()
(2) with attachment
Import smtplib from email.mime.text Import MimeText from email.mime.multipart Import MimeMultipart SENDER = ' Sender qq.com @ ' password = ' authorization code ' Receiver = 'received by qq.com @ ' # mail subject Subject = ' in Email send Test python ' # attachments transmitted with Open ( ' D: /Test1/test.txt ' , ' RB ' ) AS F: send_att =reached, f.read () ATT = MimeText (send_att, ' text ' , ' UTF-. 8 ' ) ATT [ " the Content-the Type " ] = ' file application / Steam-OCTET ' ATT [ " the Content-Disposition " ] = ' Attachment; filename test.txt = ' # specified display file name of the attachment MSG = MimeMultipart () MSG [ ' the Subject ' ] = Subject msg.attach (ATT) # E-mail SMTP = smtplib.SMTP () smtp.connect (" Smtp.qq.com " ) # connection mail service smtp.login (SENDER, password) # login smtp.sendmail (SENDER, Receiver, msg.as_string ()) # specified sender, recipient, body smtp.quit ( )
2.yagmail send a message
python third-party libraries, you need to install the library
(1) line installation yagmail (if installed, no need to install)
python -m pip install yagmail
Successful installation:
(2) yagmail send a message
Import yagmail SENDER = ' Sender qq.com @ ' pwd = ' authorization code ' receiver1 = ' recipient [email protected] ' receiver2 = ' recipient [email protected] ' host1 = ' smtp.qq.com ' # mailbox server connected YAG = yagmail.SMTP (SENDER = User, password pwd =, = Host host1) # message body Contents = [ ' Please Check your Information. ' ] # E-mail (to multiple receivers, sending multiple attachments) yag.send(sender, [receiver1, receiver2], contents, [“D:/Test1/result.html”, “D:/Test1/GUI.png”])
3. Automatic Test Report will be sent to the recipient
To Baidu search, for example:
Import the unittest Import Time Import yagmail from HTMLTestRunner Import HTMLTestRunner # the test report is sent as an attachment to the specified mailbox SENDER = ' Sender qq.com @ ' pwd = ' authorization code ' receiver1 = ' recipient [email protected] ' receiver2 = ' recipient [email protected] ' host1 = ' smtp.qq.com ' DEF the send_mail (Report): YAG = yagmail.SMTP (SENDER = User, password pwd =, = Host host1) Subject= ' Theme, automated test report ' Contents = ' text, please see the attachment. ' Yag.send (SENDER, [receiver1, receiver2], Contents, [Report]) Print ( ' In Email has Send OUT! ' ) IF the __name__ == ' __main__ ' : # define test cases webauto directory in the current directory test_dir = ' ./webauto ' SUIT = unittest.defaultTestLoader.discover (test_dir, pattern = ' test2_baidu.py ' ) # generated report in HTML format now__time the time.strftime = ( "Y- M-% D%%%% H_ of M_% S " ) # acquires the current date and time html_report = ' ./test_report/ ' + now__time + ' and result.html ' FP = Open (html_report, ' WB ' ) # call HTMLTestRunner, run the test case Runner = HTMLTestRunner (fp = Stream, title = " Baidu search test report " , the Description = " Operating environment: Windows 10, Chrome browser " ) runner.run (SUIT) fp.close () send_mail (html_report ) # send a test report
Test case: (D: \ Test1 \ webauto \ test2_baidu.py)
import unittest from time import sleep from selenium import webdriver class TestBaidu(unittest.TestCase): """百度搜索测试""" @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() cls.base_url = "http://www.baidu.com" def baidu_search(self, search_key): self.driver.get(self.base_url) self.driver.find_element_by_id("kw").send_keys(search_key) self.driver.find_element_by_id("su").click() sleep(2) def test_search_key_selenium(self): """搜索关键字:selenium""" search_key = "selenium" self.baidu_search(search_key) self.assertEqual(self.driver.title, search_key + "_百度搜索") def test_search_key_unittest(self): """搜索关键字:unittest""" search_key = "unittest" self.baidu_search(search_key) self.assertEqual(self.driver.title, search_key + "_百度搜索") @classmethod def tearDownClass(cls): cls.driver.quit() if __name__ == '__main': unittest.main()