This article introduces the Java implementation code of a simple E-mail sender program. It is shared with you for your reference. The specific content is as follows
In this code, there are several points of attention to emphasize :
1. Use Socket to get a connection with the SMTP mail server, pay attention to the host name of the SMTP server;
2. When using the data command, if the subject is written, there must be a blank line between the body of the email and the subject, that is, "Enter + Line Break", which is /r/n in the code;
3. It is also necessary to BASE64 encoding of the sender's email username and password before passing it to the SMTP server;
4. The program still has a warning when compiling. This is because the sun.misc.BASE64Encoder class exists in the rt.jar package. Since the JDK will be updated and upgraded, some classes in the package may change and be unavailable, so the compiler will issue a warning.
In addition, after writing these codes, some problems were found :
1. The mail servers of smtp.qq.com and smtp.sina.com do not know why they cannot be used. That is to say, when the sender's email address is qq or sina, this program is no longer useful and the status response code cannot be understood. In my tests, only smtp.163.com can be used. It is obvious that these SMTP servers were found on the official website, so why can't they be used? It's so strange. Anyone who knows hopes to tell me, thank you!
2. In the sendEmail() method in the SimpleMailSender class below, some duplicate codes are confusing, but there is no way, I still don't understand it...
3. Major discovery: I am surprised that the QQ email is receiving mails at a speed that may be dozens of times faster than 163 emails and sina emails. In addition, when using the nslookup command to query the host name of smtp.qq.com, it found that it has many SMTP servers, at least 5 more than 163 3. Tencent is really powerful;
4. Although I can maliciously send emails to a certain email address after writing this program, I found that when I send dozens of emails to another fixed email address in succession with a sina email address, the sina email address will be rejected if I want to send an email. Be careful.
The code is as follows:
// Email class E_Mail { String from; String to; String subject; String content; String userName; String pwd; public E_Mail(String from, String to, String subject, String content, String userName, String pwd) { this.from = from; this.to = to; this.subject = subject; this.content = content; this.userName = this.toBASE64(userName); this.pwd = this.toBASE64(pwd); } /** * Transcode username and password in E_Mail class*/ private String toBASE64(String str) { return (new sun.misc.BASE64Encoder().encode(str.getBytes())); }}// Simple mail sender class to implement the sending function public class SimpleMailSender { private String smtpServer; private int port = 25; private Socket socket; BufferedReader br; PrintWriter pw; /** * Determine SMTP mail server based on the sender's email address*/ private void initServer(String from) { if(from.contains("@163")) { this.smtpServer = "smtp.163.com"; }else if(from.contains("@126")) { this.smtpServer = "smtp.126.com"; }else if(from.contains("@sina")) { this.smtpServer = "smtp.sina.com"; }else if(from.contains("@qq")) { this.smtpServer = "smtp.qq.com"; } } public void sendEmail(E_Mail email) { try { this.initServer(email.from); this.socket = new Socket(smtpServer, port); this.br = this.getReader(socket); this.pw = this.getWriter(socket); // Start assembling the command sequence for sending emails send_Receive(null); // Receive the message that the connection to the SMTP server successfully send_Receive("ehlo hao"); send_Receive("auth login"); send_Receive(email.userName); send_Receive(email.pwd); send_Receive("mail from:<" + email.from + ">"); send_Receive("rcpt to:<" + email.to + ">"); send_Receive("data"); // Email content pw.println("from:" + email.from); pw.println("to:" + email.to); // Be sure to empty a line between the subject and the text, that is, add "/r/n" pw.println("subject:" + email.subject + "/r/n"); // Print the email content on the console System.out.println("from:" + email.from); System.out.println("to:" + email.to); System.out.println("subject:" + email.subject + "/r/n"); System.out.println(email.content); // Email body pw.println(email.content); // Remember to end the text with "." send_Receive("."); send_Receive("quit"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (socket != null) socket.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * Every time a command is sent, "/r/n" must be added after the command, * The corresponding status code of the smtp mail server will be printed out at the same time* @param command */ private void send_Receive(String command) throws IOException{ if(command != null) { // Send commands to the SMTP mail server, remember to add "/r/n" pw.print(command + "/r/n"); pw.flush(); System.out.println("User>> " + command); } char [] response = new char[1024]; br.read(response); System.out.println(response); } /** * Get the output stream of Socket*/ private PrintWriter getWriter(Socket socket) throws IOException { OutputStream socketOut = socket.getOutputStream(); return new PrintWriter(socketOut, true); } /** * Get the input stream of Socket*/ private BufferedReader getReader(Socket socket) throws IOException { InputStream socketIn = socket.getInputStream(); return new BufferedReader(new InputStreamReader(socketIn)); } // Test public static void main(String[] args) { new MailSenderGUI(); }}// Mail Sender Program Interface class MailSenderGUI extends JFrame implements ActionListener { private JLabel userNameLabel; private JTextField userNameField; private JLabel pwdLabel; private JPasswordField pwdField; private JLabel fromLabel; private JTextField fromField; private JLabel toLabel; private JTextField toField; private JLabel subjectLabel; private JLabel contentLabel; private JTextArea contentArea; private JButton sendBtn; private JButton cancelBtn; private E_Mail email; private SimpleMailSender mailSender; public MailSenderGUI() { this.init(); this.mailSender = new SimpleMailSender(); } private void init() { this.fromLabel = new JLabel("Sender Email Address: "); this.fromField = new JTextField(25); this.userNameLabel = new JLabel("Username: "); this.userNameField = new JTextField(25); this.pwdLabel = new JLabel("Password: "); this.pwdField = new JPasswordField(25); this.toLabel = new JLabel("Recipient Email Address:"); this.toField = new JTextField(25); this.subjectLabel = new JLabel("Email Subject:"); this.subjectField = new JTextField(20); this.contentLabel = new JLabel("Email Body:"); this.contentArea = new JTextArea(15, 20); this.setTitle("Ant-->Simple Mail Sender"); this.setBounds(200, 30, 500, 500); this.setLayout(new BorderLayout()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.sendBtn = new JButton("Send"); this.cancelBtn = new JButton("Reset"); this.sendBtn.addActionListener(this); this.cancelBtn.addActionListener(this); JPanel upPanel = new JPanel(new GridLayout(6, 2, 5, 5)); upPanel.add(fromLabel); upPanel.add(fromField); upPanel.add(userNameLabel); upPanel.add(pwdLabel); upPanel.add(pwdField); upPanel.add(toLabel); upPanel.add(toField); upPanel.add(toField); upPanel.add(subjectLabel); upPanel.add(subjectField); upPanel.add(contentLabel); this.add(upPanel, BorderLayout.NORTH); this.add(contentArea, BorderLayout.CENTER); JPanel downPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); downPanel.add(sendBtn, BorderLayout.SOUTH); downPanel.add(cancelBtn, BorderLayout.SOUTH); this.add(downPanel, BorderLayout.SOUTH); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == this.sendBtn) { this.email = new E_Mail( this.fromField.getText(), this.toField.getText(), this.subjectField.getText(), this.contentArea.getText(), this.userNameField.getText(), new String(this.pwdField.getPassword()) ); this.mailSender.sendEmail(this.email); } else if (e.getSource() == this.cancelBtn) { this.fromField.setText(null); this.toField.setText(null); this.subjectField.setText(null); this.contentArea.setText(null); } }}The above is all the code for writing simple E-mail sending programs in Java, I hope it will be helpful to everyone's learning.