{"id":276,"date":"2017-02-05T17:37:26","date_gmt":"2017-02-05T09:37:26","guid":{"rendered":"http:\/\/www.91tfboys.com\/?p=276"},"modified":"2019-04-02T15:34:31","modified_gmt":"2019-04-02T07:34:31","slug":"java-smtp-%e5%8f%91%e9%80%81%e9%82%ae%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/www.91tfboys.com\/?p=276","title":{"rendered":"Java SMTP \u53d1\u9001\u90ae\u4ef6"},"content":{"rendered":"<p>\/\/ \u8fd9\u662f\u6b20\u5b89\u9e23\u54e5\u4e00\u5e74\u591a\u7684\u4ee3\u7801\u3002\u4e00\u5e74\u96f6\u4e09\u4e2a\u6708\u4ee5\u524d\u5c31\u5e94\u8be5\u5b9e\u73b0\u7684\u3002\u7ed3\u679c\u62d6\u5230\u4eca\u5929\uff0c\u8fd8\u8d39\u4e86\u4e00\u4e0b\u5348\u3002<!--more--><\/p>\n<pre>package javaMailTest;\r\n\r\nimport java.io.BufferedReader;\r\nimport java.io.IOException;\r\nimport java.io.InputStream;\r\nimport java.io.InputStreamReader;\r\nimport java.io.OutputStream;\r\nimport java.io.PrintWriter;\r\nimport java.net.InetAddress;\r\nimport java.net.Socket;\r\n\r\npublic class MailSender {\r\n\tprivate String smtpServer = \"smtp.yeah.net\";\r\n\tprivate int port = 25;\r\n\t\r\n\tpublic static void main(String[] args) {\r\n\t\tMessage msg = new Message(\r\n\t\t\t\t\"ourauc@yeah.net\",\t\t\/\/ Sender\r\n\t\t\t\t\"wzhi1997@qq.com\",\r\n\t\t\t\t\"\u6210\u529f\",\r\n\t\t\t\t\"\u5df2\u51fa\uff0c\u8bf7\u53ca\u65f6\u68c0\u67e5\u66f4\u65b0\");\r\n\t\tnew MailSender().sendMail(msg);\r\n\t}\r\n\tpublic void sendMail(Message msg) {\r\n\t\tSocket socket = null;\r\n\t\ttry{\r\n\t\t\tsocket = new Socket(smtpServer, port);\r\n\t\t\tBufferedReader br = getReader(socket);\r\n\t\t\tPrintWriter pw = getWriter(socket);\r\n\t\t\tString localhost = InetAddress.getLocalHost().getHostName();\r\n\t\t\t\r\n\t\t\tString username = msg.from;\r\n\t\t\tString password = \"12345@#$%^\";\r\n\t\t\tusername = new sun.misc.BASE64Encoder().encode(username.getBytes());\r\n\t\t\tpassword = new sun.misc.BASE64Encoder().encode(password.getBytes());\r\n\t\t\t\r\n\t\t\tsendAndReceive(null, br, pw);\r\n\t\t\tsendAndReceive(\"EHLO \" + localhost, br, pw);\r\n\t\t\tsendAndReceive(\"AUTH LOGIN\", br, pw);\r\n\t\t\tsendAndReceive(username, br, pw);\r\n\t\t\tsendAndReceive(password, br, pw);\r\n\t\t\tsendAndReceive(\"MAIL FROM:&lt;\" + msg.from + \"&gt;\", br, pw);\r\n\t\t\tsendAndReceive(\"RCPT TO:&lt;\" + msg.to + \"&gt;\", br, pw);\r\n\t\t\tsendAndReceive(\"DATA\", br, pw);\r\n\t\t\tsendAndReceive(msg.data, br, pw);\r\n\t\t\tsendAndReceive(\".\", br, pw);\r\n\t\t\tsendAndReceive(\"QUIT\", br, pw);\r\n\t\t} catch (IOException e) {\r\n\t\t\te.printStackTrace();\r\n\t\t} finally {\r\n\t\t\ttry {\r\n\t\t\t\tif (socket != null)\r\n\t\t\t\t\tsocket.close();\r\n\t\t\t} catch (IOException e2) {\r\n\t\t\t\te2.printStackTrace();\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\t\r\n\tprivate void sendAndReceive(String str, BufferedReader br, PrintWriter pw) throws IOException {\r\n\t\tif (str != null) {\r\n\t\t\tSystem.out.println(\"Client&gt;\" + str);\r\n\t\t\tpw.println(str);\r\n\t\t}\r\n\t\tString response = \"\";\r\n\t\t\r\n\t\tif ((response = br.readLine()) != null) {\r\n\t\t\tSystem.out.println(\"Server&gt;\" + response);\r\n\t\t}\r\n\t\t\r\n\t\ttry {\r\n\t\t\tThread.sleep(1000);\r\n\t\t} catch (InterruptedException e) {\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t}\r\n\t\r\n\tprivate PrintWriter getWriter(Socket socket) throws IOException {\r\n\t\tOutputStream socketOut = socket.getOutputStream();\r\n\t\treturn new PrintWriter(socketOut,true);\r\n\t}\r\n\t\r\n\tprivate BufferedReader getReader(Socket socket) throws IOException {\r\n\t\tInputStream socketIn = socket.getInputStream();\r\n\t\treturn new BufferedReader(new InputStreamReader(socketIn));\r\n\t}\r\n}\r\n\r\nclass Message {\r\n\tString from;\r\n\tString to;\r\n\tString subject;\r\n\tString content;\r\n\tString data;\r\n\tpublic Message(String from, String to, String subject, String content) {\r\n\t\tthis.from = from;\r\n\t\tthis.to = to;\r\n\t\tthis.subject = subject;\r\n\t\tthis.content = content;\r\n\t\tdata = \"From:ourauc@yeah.net\\r\\nTo:wzhi1997@qq.com\\r\\nSubject:\" + subject + \"\\r\\n\\r\\n\" + content;\r\n\t}\r\n}\r\n<\/pre>\n<p>\/\/ \u6700\u540e\u8fd8\u662f\u81f4\u656c\u5b89\u9e23\u54e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ \u8fd9\u662f\u6b20\u5b89\u9e23\u54e5\u4e00\u5e74\u591a\u7684\u4ee3\u7801\u3002\u4e00\u5e74\u96f6\u4e09\u4e2a\u6708\u4ee5\u524d\u5c31\u5e94\u8be5\u5b9e\u73b0\u7684\u3002\u7ed3\u679c\u62d6\u5230\u4eca\u5929\uff0c\u8fd8\u8d39\u4e86\u4e00\u4e0b\u5348\u3002<\/p>\n","protected":false},"author":1,"featured_media":277,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[16,17],"class_list":["post-276","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding","tag-java","tag-smtp"],"_links":{"self":[{"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/posts\/276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=276"}],"version-history":[{"count":2,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/posts\/276\/revisions"}],"predecessor-version":[{"id":279,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/posts\/276\/revisions\/279"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/media\/277"}],"wp:attachment":[{"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}