This example shares the specific code of java using Socket to implement the SMTP protocol for sending emails for your reference. The specific content is as follows
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 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("test mail"); mail.setContent("<h1>Hello</h1><br/><img src=/"https://www.baidu.com/img/baidu_jgylogo3.gif?v=39549282.gif/" />"); mail.setShowLog(true); mail.send(); System.out.println("Program End"); } /** Email Subject**/ private String subject; /** Send from this address**/ private String fromMail; /** Username**/ private String userName; /** Login Password**/ private String password; /** SMTP Server address**/ private String smtpServer; /** SMTP server port (default: 25) **/ private int smtpPort = 25; /** All addresses sent to toMail**/ private List<String> toMail; /** Mail content**/ private String content; /** Whether to display the log**/ 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 cannot be empty"); } if (userName == null) { throw new RuntimeException("userName cannot be empty"); } if (password == null) { throw new RuntimeException("password cannot be empty"); } if (fromMail == null) { throw new RuntimeException("fromMail cannot be empty"); } if (toMail == null || toMail.isEmpty()) { throw new RuntimeException("toMail cannot be empty"); } if (content == null || toMail.isEmpty()) { throw new RuntimeException("content cannot be empty"); } 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("Connect to" + smtpServer + ":" + smtpPort + "Failed", 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("Send the email completes, close the Socket error occurred: " + 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("Response information error: " + line); } else if (num >= 400) { throw new RuntimeException("Send email failed: " + line); } if (showResponse) { System.out.println(line); } } catch (IOException e) { System.out.println("Failed to get response"); } } }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.