{"id":607,"date":"2025-03-01T17:58:00","date_gmt":"2025-03-01T09:58:00","guid":{"rendered":"https:\/\/www.zhzxs.xyz\/?p=607"},"modified":"2025-03-01T17:58:02","modified_gmt":"2025-03-01T09:58:02","slug":"1740823018","status":"publish","type":"post","link":"https:\/\/www.zhzxs.site\/index.php\/2025\/03\/01\/1740823018\/","title":{"rendered":"Ubuntu22.04\u4f7f\u7528python\u7ed9\u81ea\u5df1\u53d1\u9001\u90ae\u4ef6"},"content":{"rendered":"\n<p>\u5728 Ubuntu 22.04 \u4e0a\u4f7f\u7528 Python \u811a\u672c\u53d1\u9001\u90ae\u4ef6\u5230\u4f60\u7684 126 \u90ae\u7bb1\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>smtplib<\/code> \u5e93\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u811a\u672c\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 Python \u901a\u8fc7 SMTP \u534f\u8bae\u53d1\u9001\u90ae\u4ef6\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h3>\n\n\n\n<p>\u4f60\u9700\u8981\u786e\u4fdd Python \u7684 <code>smtplib<\/code> \u5e93\u53ef\u7528\u3002\u5b83\u662f Python \u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002\u4e3a\u4e86\u53d1\u9001\u90ae\u4ef6\uff0c\u4f60\u8fd8\u9700\u8981 <code>email<\/code> \u5e93\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u4f1a\u7528\u5230\u5b83\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Python \u811a\u672c\u793a\u4f8b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import smtplib\nfrom email.mime.text import MIMEText\nfrom email.mime.multipart import MIMEMultipart\n\n# \u8bbe\u7f6e\u90ae\u4ef6\u5185\u5bb9\nsender_email = \"your_email@126.com\"  # \u4f60\u7684 163 \u90ae\u7bb1\nreceiver_email = \"doctorxiaoming@126.com\"  # \u6536\u4ef6\u4eba\u90ae\u7bb1\npassword = \"your_email_password\"  # \u4f60\u7684 163 \u90ae\u7bb1\u5bc6\u7801\uff08\u6ce8\u610f\uff1a\u4e0d\u8981\u5c06\u5bc6\u7801\u66b4\u9732\u5728\u516c\u5f00\u7684\u5730\u65b9\uff09\n\nsubject = \"\u4f60\u597d\"\nbody = \"\u4f60\u597d\uff0c\u8fd9\u662f\u6765\u81ea Python \u7684\u6d4b\u8bd5\u90ae\u4ef6\u3002\"\n\n# \u521b\u5efa\u4e00\u4e2a\u90ae\u4ef6\u5bf9\u8c61\nmsg = MIMEMultipart()\nmsg&#91;'From'] = sender_email\nmsg&#91;'To'] = receiver_email\nmsg&#91;'Subject'] = subject\nmsg.attach(MIMEText(body, 'plain'))\n\n# \u8bbe\u7f6e SMTP \u670d\u52a1\u5668\u5e76\u53d1\u9001\u90ae\u4ef6\ntry:\n    server = smtplib.SMTP('smtp.163.com', 25)  # 163 \u90ae\u7bb1\u7684 SMTP \u670d\u52a1\u5668\n    server.starttls()  # \u542f\u52a8 TLS \u52a0\u5bc6\n    server.login(sender_email, password)  # \u767b\u5f55\u90ae\u7bb1\n    text = msg.as_string()  # \u5c06\u90ae\u4ef6\u5bf9\u8c61\u8f6c\u4e3a\u5b57\u7b26\u4e32\n    server.sendmail(sender_email, receiver_email, text)  # \u53d1\u9001\u90ae\u4ef6\n    print(\"\u90ae\u4ef6\u53d1\u9001\u6210\u529f\uff01\")\nexcept Exception as e:\n    print(f\"\u53d1\u9001\u90ae\u4ef6\u5931\u8d25: {e}\")\nfinally:\n    server.quit()  # \u5173\u95ed\u670d\u52a1\u5668\u8fde\u63a5\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u5173\u952e\u6b65\u9aa4\u89e3\u91ca\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u521b\u5efa\u90ae\u4ef6\u5185\u5bb9<\/strong>\uff1a\u6211\u4eec\u4f7f\u7528 <code>MIMEText<\/code> \u548c <code>MIMEMultipart<\/code> \u6765\u6784\u5efa\u90ae\u4ef6\u7684\u5185\u5bb9\u3002<code>MIMEText<\/code> \u7528\u4e8e\u90ae\u4ef6\u7684\u6587\u672c\u90e8\u5206\uff0c<code>MIMEMultipart<\/code> \u7528\u4e8e\u5c06\u591a\u4e2a\u90e8\u5206\u7ec4\u5408\u5728\u4e00\u8d77\u3002<\/li>\n\n\n\n<li><strong>\u8fde\u63a5 SMTP \u670d\u52a1\u5668<\/strong>\uff1a\u6211\u4eec\u901a\u8fc7 <code>smtplib.SMTP()<\/code> \u8fde\u63a5 126 \u90ae\u7bb1\u7684 SMTP \u670d\u52a1\u5668\u3002\u5bf9\u4e8e 126 \u90ae\u7bb1\uff0cSMTP \u670d\u52a1\u5668\u5730\u5740\u662f <code>smtp.126.com<\/code>\uff0c\u7aef\u53e3\u662f 25\u3002<\/li>\n\n\n\n<li><strong>\u52a0\u5bc6\u548c\u767b\u5f55<\/strong>\uff1a\u901a\u8fc7 <code>starttls()<\/code> \u542f\u7528 TLS \u52a0\u5bc6\uff0c\u518d\u7528 <code>login()<\/code> \u65b9\u6cd5\u767b\u5f55\u4f60\u7684\u90ae\u7bb1\u8d26\u6237\u3002<\/li>\n\n\n\n<li><strong>\u53d1\u9001\u90ae\u4ef6<\/strong>\uff1a\u901a\u8fc7 <code>sendmail()<\/code> \u65b9\u6cd5\u53d1\u9001\u90ae\u4ef6\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u6ce8\u610f\u4e8b\u9879\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f60\u9700\u8981\u5c06 <code>sender_email<\/code> \u548c <code>password<\/code> \u66ff\u6362\u6210\u4f60\u81ea\u5df1\u7684 163 \u90ae\u7bb1\u5730\u5740\u548c\u5bc6\u7801\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u4f60\u542f\u7528\u4e86\u90ae\u7bb1\u7684\u4e24\u6b65\u9a8c\u8bc1\uff0c\u53ef\u80fd\u9700\u8981\u521b\u5efa\u5e76\u4f7f\u7528\u4e00\u4e2a\u4e13\u7528\u7684\u5e94\u7528\u5bc6\u7801\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u4f7f\u7528\u4f60\u7684\u90ae\u7bb1\u5bc6\u7801\u3002<\/li>\n\n\n\n<li>\u90ae\u4ef6\u53d1\u9001\u6210\u529f\u540e\uff0c\u4f60\u4f1a\u770b\u5230 &#8220;\u90ae\u4ef6\u53d1\u9001\u6210\u529f\uff01&#8221;\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u6d4b\u8bd5\u811a\u672c<\/h3>\n\n\n\n<p>\u8fd0\u884c\u811a\u672c\u540e\uff0c\u68c0\u67e5\u4f60\u7684 126 \u90ae\u7bb1\u662f\u5426\u6536\u5230\u4e86 &#8220;\u4f60\u597d&#8221; \u7684\u90ae\u4ef6\u3002\u5982\u679c\u9047\u5230\u4efb\u4f55\u95ee\u9898\uff0c\u68c0\u67e5\u9519\u8bef\u4fe1\u606f\u5e76\u786e\u4fdd SMTP \u914d\u7f6e\u548c\u7f51\u7edc\u8fde\u63a5\u6ca1\u6709\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Ubuntu 22.04 \u4e0a\u4f7f\u7528 Python \u811a\u672c\u53d1\u9001\u90ae\u4ef6\u5230\u4f60\u7684 126 \u90ae\u7bb1\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 smtpli [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":163,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[121,93,122],"class_list":["post-607","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-2","tag-mail","tag-python","tag-122"],"_links":{"self":[{"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/posts\/607","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/comments?post=607"}],"version-history":[{"count":1,"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/posts\/607\/revisions"}],"predecessor-version":[{"id":608,"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/posts\/607\/revisions\/608"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/media\/163"}],"wp:attachment":[{"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/media?parent=607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/categories?post=607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhzxs.site\/index.php\/wp-json\/wp\/v2\/tags?post=607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}