{"id":20065,"date":"2025-05-11T14:48:29","date_gmt":"2025-05-11T06:48:29","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=20065"},"modified":"2025-05-11T14:51:15","modified_gmt":"2025-05-11T06:51:15","slug":"python-%e5%ae%9e%e7%8e%b0-smtp-%e9%82%ae%e7%ae%b1%e5%8f%91%e9%80%81","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=20065","title":{"rendered":"Python \u5b9e\u73b0 SMTP \u90ae\u7bb1\u53d1\u9001"},"content":{"rendered":"<p>\u6ca1\u6709\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u7b80\u5355\u5b9e\u73b0\u7684\u4e00\u4e2a\u6837\u4f8b<\/p>\n<p>smtpclient.py<\/p>\n<pre><code class=\"language-python\">import smtplib\nfrom email.mime.text import MIMEText\nfrom email.mime.multipart import MIMEMultipart\nfrom email.header import Header\n\nclass SmtpClient:\n    smtpServer = None\n    smtpPort = None\n    senderName = None\n    senderEmail = None\n    senderPassword = None\n    receiverEmail = None\n    encryption = True\n\n    @staticmethod\n    def setEmailConfig(smtpServer: str, smtpPort: int, senderName: str, senderEmail: str, senderPassword: str, receiverEmail: str, encryption: str = &#039;SSL&#039;):\n        &quot;&quot;&quot;\u8bbe\u7f6e\u53d1\u4ef6\u4fe1\u606f\n\n        Args:\n            smtpServer (str): SMTP\u670d\u52a1\u5668\u5730\u5740\n            smtpPort (int): SMTP\u670d\u52a1\u5668\u7aef\u53e3\n            senderName (str): \u53d1\u4ef6\u4eba\u59d3\u540d\n            senderEmail (str): \u53d1\u4ef6\u4eba\u90ae\u7bb1\n            senderPassword (str): \u53d1\u4ef6\u4eba\u90ae\u7bb1\u5bc6\u7801\n            receiverEmail (str): \u6536\u4ef6\u4eba\u90ae\u7bb1\n            encryption (str): \u52a0\u5bc6\u65b9\u5f0f\uff08SSL \u6216 STARTTLS\uff09\n        &quot;&quot;&quot;\n        SmtpClient.smtpServer = smtpServer\n        SmtpClient.smtpPort = smtpPort\n        SmtpClient.senderName = Header(senderName, &#039;utf-8&#039;).encode()\n        SmtpClient.senderEmail = senderEmail\n        SmtpClient.senderPassword = senderPassword\n        SmtpClient.receiverEmail = receiverEmail\n        SmtpClient.encryption = encryption\n\n    @staticmethod\n    def sendEmail(subject, body) -&gt; tuple[bool, Exception]:\n        &quot;&quot;&quot;\u53d1\u9001\u90ae\u4ef6\n\n        Args:\n            subject (str): \u90ae\u4ef6\u4e3b\u9898\n            body (str): \u90ae\u4ef6\u5185\u5bb9\n\n        Returns:\n            tuple[bool, Exception]: \u53d1\u9001\u6210\u529f\u8fd4\u56de (True, None)\uff0c\u53d1\u9001\u5931\u8d25\u8fd4\u56de (False, Exception)\n        &quot;&quot;&quot;\n        if False == all([SmtpClient, SmtpClient.smtpPort, SmtpClient.senderName, SmtpClient.senderEmail, SmtpClient.senderPassword, SmtpClient.receiverEmail]):\n            return False, &#039;\u8bf7\u5148\u8bbe\u7f6e\u53d1\u4ef6\u4fe1\u606f&#039;\n\n        emailMsg = MIMEMultipart()\n        emailMsg[&#039;From&#039;] = f&#039;{SmtpClient.senderName} &lt;{SmtpClient.senderEmail}&gt;&#039;\n        emailMsg[&#039;To&#039;] = SmtpClient.receiverEmail\n        emailMsg[&#039;Subject&#039;] = subject\n        emailMsg.attach(MIMEText(body, &#039;plain&#039;))\n\n        try:\n            if SmtpClient.encryption == &#039;SSL&#039;:\n                server = smtplib.SMTP_SSL(SmtpClient.smtpServer, SmtpClient.smtpPort)\n            else:\n                server = smtplib.SMTP(SmtpClient.smtpServer, SmtpClient.smtpPort)\n                server.starttls()\n            server.login(SmtpClient.senderEmail, SmtpClient.senderPassword)\n            server.sendmail(SmtpClient.senderEmail, SmtpClient.receiverEmail, emailMsg.as_string())\n            server.quit()\n            return True, None\n        except Exception as e:\n            return False, e\n\ndef test():\n    SmtpClient.setEmailConfig(&#039;\u90ae\u7bb1\u670d\u52a1\u5668&#039;, \u90ae\u7bb1\u7aef\u53e3, &#039;\u53d1\u4ef6\u4eba&#039;, &#039;\u90ae\u7bb1&#039;, &#039;\u5bc6\u7801&#039;, &#039;\u6536\u4ef6\u4eba&#039;)\n    status, err = SmtpClient.sendEmail(&#039;\u90ae\u4ef6\u6807\u9898&#039;, &#039;\u90ae\u4ef6\u6b63\u6587&#039;)\n    if status:\n        print(&#039;\u53d1\u9001\u6210\u529f&#039;)\n    else:\n        print(&#039;\u53d1\u9001\u5931\u8d25&#039;)\n        print(err)\n\nif __name__ == &#039;__main__&#039;:\n    test()<\/code><\/pre>\n<p>\u6211\u8fd9\u91cc\u4f7f\u7528 QQ \u90ae\u7bb1\u7684 SMTP<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2025\/05\/image-1746945892888.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 1115px; --smush-placeholder-aspect-ratio: 1115\/1223;\" \/><\/p>\n<p>\u8bbe\u7f6e\u597d\u90ae\u7bb1\u4fe1\u606f\uff0c\u8fd0\u884c\u6d4b\u8bd5<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2025\/05\/image-1746945979073.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 1116px; --smush-placeholder-aspect-ratio: 1116\/501;\" \/><\/p>\n<p>\u6536\u5230\u90ae\u4ef6<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2025\/05\/image-1746946083802.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 897px; --smush-placeholder-aspect-ratio: 897\/560;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6ca1\u6709\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff0c\u7b80\u5355\u5b9e\u73b0\u7684\u4e00\u4e2a\u6837\u4f8b smtpclient.py import smtplib from em [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[1,592],"tags":[],"class_list":["post-20065","post","type-post","status-publish","format-standard","hentry","category-all","category-python"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/20065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=20065"}],"version-history":[{"count":4,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/20065\/revisions"}],"predecessor-version":[{"id":20072,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/20065\/revisions\/20072"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}