Mail notification achieve exception alarm

First, we need to know about mailing (SMTP) and receiving e-mail (POP3) protocol:      the SMTP (the Simple MailTransfer Protocol) that is Simple Mail Transfer Protocol, which is a set of rules for transferring from the source address to the destination address of the message, which it way to transfer control of the message. SMTP protocol is TCP / IP protocol suite, each computer group to help it find the next destination or transit when sending mail. SMTP server is the SMTP protocol to follow outgoing mail server used to send e-mail sent or transit.              POP3 (Post Office Protocol -Version3) This protocol is mainly used to host clients remotely manage e-mail on the server.

The process of sending and receiving mail

Pictures .png

Here we use an example NetEase mailbox

1. The opening of the SMTP protocol

Pictures .png

Pictures .png

The main purpose of this step is to open the SMTP protocol and obtain client authorization code (authorization code needs its own set similar to their own mailbox password)

Second, code implementation

1. dependence introduction
<dependency>
   <groupId>com.sun.mail</groupId>
   <artifactId>javax.mail</artifactId>
   <version>1.6.0</version>
</dependency>
复制代码
2. coding
/**
 * 邮件
 * 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邮箱账号
    private static final String PASSWORD = "000000";//163邮箱---授权码
    private static final String EMAILFORM = "[email protected]";  //发送邮件的用户
    private static JavaMailSenderImpl mailSender = createMailSender();


    private static final String EMAILNAME = "数据异常报告";  //收到邮件显示对方邮件名称 总体名称
    private static final String EMAILTOPNAME = "定时任务1数据采集异常";  //邮件名称

    /**
     * 邮件发送器
     *
     * @return 配置好的工具
     */
    private static JavaMailSenderImpl createMailSender() {
        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);
        return sender;
    }

    /**
     * 发送邮件
     * @param to      邮件接收人
     * @param subject 主题
     * @param html    发送内容
     * @throws UnsupportedEncodingException 异常
     */
    public static void sendHtmlMail(String to, String subject, String html) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();

            // 设置utf-8或GBK编码,否则邮件会有乱码
            MimeMessageHelper 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, "数据入库异常! CoderZS 我在简书等你");
    }

}
复制代码

E-mail notification

E-mail notification

Guess you like

Origin juejin.im/post/5d8882dff265da03ad148f24