First of all, we need to understand the protocols for mail sending (SMTP) and mail receiving (POP3). SMTP is a set of rules used to transfer mails from source addresses to destination addresses. It controls the transfer mode of mails. SMTP protocol belongs to the TCP/IP protocol cluster, which helps each computer to find the next destination when sending or forwarding a message. The SMTP server is a mail sending server that follows the SMTP protocol and is used to send or forward emails. Post Office Protocol -Version3 (POP3) This Protocol is used to host clients to remotely manage emails on servers.

The process of sending and receiving emails

We use netease email as an example

1. Enable SMTP

The purpose of this step is to enable THE SMTP protocol and obtain the client authorization code (the authorization code needs to be set by yourself).

Second, code implementation

1. Introduce dependencies
<dependency>
   <groupId>com.sun.mail</groupId>
   <artifactId>javax.mail</artifactId>
   <version>1.6.0</version>
</dependency>
Copy the code
2. Code writing
/** * email * author:CoderZS */ public class javaMailTest {private static final String HOST ="smtp.163.com"; Private static final Integer PORT = 25; private static final String USERNAME ="[email protected]"; //163 Email Account private static final String PASSWORD ="000000"; Private static Final String EMAILFORM = private static Final String EMAILFORM ="[email protected]"; Private static JavaMailSenderImpl mailSender = createMailSender(); private static final String EMAILNAME ="Data Exception Report"; Private static Final String EMAILTOPNAME ="Abnormal Data Collection of Scheduled Task 1"; // Email name /** * email sender ** @return*/ Private Static JavaMailSenderImplcreateMailSender() {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(HOST);
        sender.setPort(PORT);
        sender.setUsername(USERNAME);
        sender.setPassword(PASSWORD);
        sender.setDefaultEncoding("Utf-8");
        Properties p = new Properties();
        p.setProperty("mail.smtp.timeout"."25000");
        p.setProperty("mail.smtp.auth"."false");
        sender.setJavaMailProperties(p);
        returnsender; } / email * * * * @ param to email recipients * @ param subject theme * @ param send HTML content * @ throws UnsupportedEncodingException * / public static void sendHtmlMail(String to, String subject, String html) { try { MimeMessage mimeMessage = mailSender.createMimeMessage(); MessageHelper = new MimeMessageHelper(mimeMessage,true."utf-8");
            messageHelper.setFrom(EMAILFORM, EMAILNAME);
            messageHelper.setTo(to);
            messageHelper.setSubject(subject);
            messageHelper.setText(html, true);
            mailSender.send(mimeMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
     sendHtmlMail("[email protected]", EMAILTOPNAME, "Abnormal data entry! CoderZS, I'll meet you at Jane's."); }}Copy the code

Email notification