java使用Socket實(shí)現(xiàn)SMTP協(xié)議發(fā)送郵件
本文實(shí)例為大家分享了java 利用Socket實(shí)現(xiàn)SMTP協(xié)議發(fā)送郵件的具體代碼,供大家參考,具體內(nèi)容如下
package mail; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Reader; import java.net.Socket; import java.util.ArrayList; import java.util.List; import org.apache.commons.codec.binary.Base64; public class Mail { public static void main(String[] args) throws IOException { Mail mail = new Mail(); mail.setSmtpServer("smtp.qq.com"); mail.setFromMail("1344364****@qq.com"); mail.addToMail("105648****@qq.com"); mail.addToMail("long*****@sina.com"); mail.setUserName("134364****"); mail.setPassword("*************"); mail.setSubject("測(cè)試郵件"); mail.setContent("<h1>你好</h1><br/><img src=\"https://www.baidu.com/img/baidu_jgylogo3.gif?v=39549282.gif\" />"); mail.setShowLog(true); mail.send(); System.out.println("程序結(jié)束"); } /** 郵件主題 **/ private String subject; /** 從此地址發(fā)出 **/ private String fromMail; /** 用戶名 **/ private String userName; /** 登錄密碼 **/ private String password; /** SMTP 服務(wù)器地址 **/ private String smtpServer; /** SMTP 服務(wù)器端口(默認(rèn):25) **/ private int smtpPort = 25; /** 發(fā)送到 toMail 中的所有地址 **/ private List<String> toMail; /** 郵件內(nèi)容 **/ private String content; /** 是否顯示日志 **/ private boolean showLog; public void addToMail(String mail) { if (toMail == null) toMail = new ArrayList<String>(); toMail.add(mail); } public void send() { if (smtpServer == null) { throw new RuntimeException("smtpServer 不能為空"); } if (userName == null) { throw new RuntimeException("userName 不能為空"); } if (password == null) { throw new RuntimeException("password 不能為空"); } if (fromMail == null) { throw new RuntimeException("fromMail 不能為空"); } if (toMail == null || toMail.isEmpty()) { throw new RuntimeException("toMail 不能為空"); } if (content == null || toMail.isEmpty()) { throw new RuntimeException("content 不能為空"); } Socket socket = null; InputStream in = null; OutputStream out = null; try { socket = new Socket(smtpServer, smtpPort); socket.setSoTimeout(3000); in = socket.getInputStream(); out = socket.getOutputStream(); } catch (IOException e) { throw new RuntimeException("連接到 " + smtpServer + ":" + smtpPort + " 失敗", e); } BufferedReaderProxy reader = new BufferedReaderProxy(new InputStreamReader(in), showLog); PrintWriterProxy writer = new PrintWriterProxy(out, showLog); reader.showResponse(); writer.println("HELO " + smtpServer); reader.showResponse(); writer.println("AUTH LOGIN"); reader.showResponse(); writer.println(new String(Base64.encodeBase64(userName.getBytes()))); reader.showResponse(); writer.println(new String(Base64.encodeBase64(password.getBytes()))); reader.showResponse(); writer.println("MAIL FROM:" + fromMail); reader.showResponse(); for (String mail : toMail) { writer.println("RCPT TO:" + mail); reader.showResponse(); } writer.println("DATA"); writer.println("Content-Type:text/html"); if (subject != null) { writer.println("Subject:" + subject); } writer.println("From:" + fromMail); writer.print("To:"); for (String mail : toMail) { writer.print(mail + "; "); } writer.println(); writer.println(); writer.println(content); writer.println("."); reader.showResponse(); writer.println("QUIT"); reader.showResponse(); try { socket.close(); } catch (IOException e) { System.err.println("發(fā)送郵件完成,關(guān)閉 Socket 出錯(cuò):" + e.getMessage()); } } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getFromMail() { return fromMail; } public void setFromMail(String fromMail) { this.fromMail = fromMail; } public String getSmtpServer() { return smtpServer; } public void setSmtpServer(String smtpServer) { this.smtpServer = smtpServer; } public int getSmtpPort() { return smtpPort; } public void setSmtpPort(int smtpPort) { this.smtpPort = smtpPort; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public List<String> getToMail() { return toMail; } public void setToMail(List<String> toMail) { this.toMail = toMail; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean getShowLog() { return showLog; } public void setShowLog(boolean showLog) { this.showLog = showLog; } static class PrintWriterProxy extends PrintWriter { private boolean showRequest; public PrintWriterProxy(OutputStream out, boolean showRequest) { super(out, true); this.showRequest = showRequest; } @Override public void println() { if (showRequest) System.out.println(); super.println(); } public void print(String s) { if (showRequest) System.out.print(s); super.print(s); } } static class BufferedReaderProxy extends BufferedReader { private boolean showResponse = true; public BufferedReaderProxy(Reader in, boolean showResponse) { super(in); this.showResponse = showResponse; } public void showResponse() { try { String line = readLine(); String number = line.substring(0, 3); int num = -1; try { num = Integer.parseInt(number); } catch (Exception e) { } if (num == -1) { throw new RuntimeException("響應(yīng)信息錯(cuò)誤 : " + line); } else if (num >= 400) { throw new RuntimeException("發(fā)送郵件失敗 : " + line); } if (showResponse) { System.out.println(line); } } catch (IOException e) { System.out.println("獲取響應(yīng)失敗"); } } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java NIO三大組件與ByteBuffer深入理解及使用
這篇文章主要介紹了Java NIO三大組件與ByteBuffer,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01java使用MulticastSocket實(shí)現(xiàn)基于廣播的多人聊天室
這篇文章主要為大家詳細(xì)介紹了java使用MulticastSocket實(shí)現(xiàn)基于廣播的多人聊天室,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Mybatis的parameterType造成線程阻塞問(wèn)題分析
這篇文章主要詳細(xì)分析了Mybatis的parameterType造成線程阻塞問(wèn)題,文中有詳細(xì)的解決方法,及相關(guān)的代碼示例,具有一定的參考價(jià)值,感興趣的朋友可以借鑒閱讀2023-06-06maven為MANIFEST.MF文件添加內(nèi)容的方法
這篇文章主要介紹了maven為MANIFEST.MF文件添加內(nèi)容的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12IntelliJ?IDEA設(shè)置JVM運(yùn)行參數(shù)的圖文介紹
這篇文章主要介紹了IntelliJ?IDEA設(shè)置JVM運(yùn)行參數(shù)的方法,包括配置方式及優(yōu)先級(jí),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04