SMTP realizes mail sending

Mailbox configuration

  1. Open the settings and enable the IMAP/SMTP service
  2. Enable POP3/SMTP service
  3. Get the authorization code and save it
  4. Introduce Pom dependencies
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4</version>
</dependency>


Supported servers

sina.com:
POP3 server address: pop3.sina.com.cn (port: 110)
SMTP server address: smtp.sina.com.cn (port: 25)
 
sinaVIP:
POP3 server: pop3.vip.sina.com (port: 110)
SMTP server: smtp.vip.sina.com (port: 25)
 
sohu.com:
POP3 server address: pop3.sohu.com (port: 110)
SMTP server address: smtp.sohu.com (port: 25)
 
126 mailbox:
POP3 server address: pop.126.com (port: 110)
SMTP server address: smtp.126.com (port: 25)
 
139 mailbox:
POP3 server address: POP.139.com (port: 110)
SMTP server address: SMTP.139.com (port: 25)
 
163.com:
POP3 server address: pop.163.com (port: 110)
SMTP server address: smtp.163.com (port: 25)
 
QQ mailbox
POP3 server address: pop.qq.com (port: 110)
SMTP server address: smtp.qq.com (port: 25)
 
QQ enterprise mailbox
POP3 server address: pop.exmail.qq.com (SSL enabled port: 995)
SMTP server address: smtp.exmail.qq.com (SSL enabled port: 587/465)
 
yahoo.com:
POP3 server address: pop.mail.yahoo.com
SMTP server address: smtp.mail.yahoo.com
 
yahoo.com.cn:
POP3 server address: pop.mail.yahoo.com.cn (port: 995)
SMTP server address: smtp.mail.yahoo.com.cn (port: 587
 
HotMail
POP3 server address: pop3.live.com (port: 995)
SMTP server address: smtp.live.com (port: 587)
 
gmail (google.com)
POP3 server address: pop.gmail.com (SSL enabled port: 995)
SMTP server address: smtp.gmail.com (SSL enabled port: 587)
 
263.net:
POP3 server address: pop3.263.net (port: 110)
SMTP server address: smtp.263.net (port: 25)
 
263.net.cn:
POP3 server address: pop.263.net.cn (port: 110)
SMTP server address: smtp.263.net.cn (port: 25)
 
x263.net:
POP3 server address: pop.x263.net (port: 110)
SMTP server address: smtp.x263.net (port: 25)
 
21cn.com:
POP3 server address: pop.21cn.com (port: 110)
SMTP server address: smtp.21cn.com (port: 25)
 
Foxmail:
POP3 server address: POP.foxmail.com (port: 110)
SMTP server address: SMTP.foxmail.com (port: 25)
 
china.com:
POP3 server address: pop.china.com (port: 110)
SMTP server address: smtp.china.com (port: 25)
 
tom.com:
POP3 server address: pop.tom.com (port: 110)
SMTP server address: smtp.tom.com (port: 25)
 
etang.com:
POP3 server address: pop.etang.com
SMTP server address: smtp.etang.com

Simple E-mail sending

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public static void main(String[] args) {<!-- -->
        // recipient email
        String to = "[email protected]";

        // sender email
        String from = "[email protected]";

        // Specify the sender of the mail
        String host = "smtp.126.com";

        // Get system properties
        Properties properties = System. getProperties();

        // set mail server
        properties.setProperty("mail.smtp.host", host);

        properties.put("mail.smtp.auth", "true");
        // Get the default session object
        Session session = Session.getDefaultInstance(properties,new Authenticator(){<!-- -->
            public PasswordAuthentication getPasswordAuthentication()
            {<!-- -->
                return new PasswordAuthentication("[email protected]", "JFYBLV111111YYCGE"); //Sender email user name, authorization code
            }
        });

        try{<!-- -->
            // create default MimeMessage object
            MimeMessage message = new MimeMessage(session);

            // Set From: header header field
            message.setFrom(new InternetAddress(from));

            // Set To: header field
            // send multiple emails
// InternetAddress internetAddress = new InternetAddress(to);
// InternetAddress internetAddress2 = new InternetAddress(to);
// Address[] addresses = new Address[]{internetAddress, internetAddress2};
// message.addRecipients(Message.RecipientType.TO,
// addresses);
            // Send to a single mailbox
            message.addRecipients(Message.RecipientType.TO,
                    to);

            // Set Subject: header field
            message.setSubject("Hello, world");

            // set message body
            message.setText("This is a text, I hope you can have a good mood every day!");

            // Send a message
            Transport. send(message);
            System.out.println("Send successfully");
        } catch (MessagingException mex) {<!-- -->
            mex. printStackTrace();
        }
    }

E-mail sending with HTML

Instead of normal text, HTML text is inserted in the message body

public static void main(String[] args) {<!-- -->
        // recipient email
        String to = "[email protected]";

        // sender email
        String from = "[email protected]";

        // Specify the sender of the mail
        String host = "smtp.126.com";

        // Get system properties
        Properties properties = System. getProperties();

        // set mail server
        properties.setProperty("mail.smtp.host", host);

        properties.put("mail.smtp.auth", "true");
        // Get the default session object
        Session session = Session.getDefaultInstance(properties,new Authenticator(){<!-- -->
            public PasswordAuthentication getPasswordAuthentication()
            {<!-- -->
                return new PasswordAuthentication("[email protected]", "JFYBLV111111YCGE"); //Sender email user name, authorization code
            }
        });

        try{<!-- -->
            // create default MimeMessage object
            MimeMessage message = new MimeMessage(session);

            // Set From: header header field
            message.setFrom(new InternetAddress(from));

            // Set To: header field
            // send multiple emails
// InternetAddress internetAddress = new InternetAddress(to);
// InternetAddress internetAddress2 = new InternetAddress(to);
// Address[] addresses = new Address[]{internetAddress, internetAddress2};
// message.addRecipients(Message.RecipientType.TO,
// addresses);
            // Send to a single mailbox
            message.addRecipients(Message.RecipientType.TO,
                    to);

            // Set Subject: header field
            message.setSubject("This is an HTML test email");

            // Send HTML message, insert HTML tags. charset=UTF-8 is used to solve Chinese garbled characters
            message.setContent("<h1 style="color: rebeccapurple;">I hope you have a good mood every day!</h1><br>\
" +
                    " <a href="www.baidu.com">Click me to jump\?\?</a>\
" +
                    " <div>Please reply, thank you</div>", "text/html; charset=UTF-8");
            // Send a message
            Transport. send(message);
            System.out.println("send successfully");
        } catch (MessagingException mex) {<!-- -->
            mex. printStackTrace();
        }