美文网首页
springboot2.X集成email

springboot2.X集成email

作者: 小吖么小一郎 | 来源:发表于2019-07-05 15:36 被阅读0次

    加入依赖

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-mail</artifactId>
            </dependency>
    

    新建一个枚举Messages

    package com.example.demo.email;
    public enum Messages {
        STOP(-1,"暂停服务","stop")
        ,SUCCESS(2000,"成功","success")
        ,SYSTEM_ERROR(9999,"系统错误","system error")
        ,AUTH_FAIL(4001,"无访问权限","no access")
        ,USERNAME_PASSWORD_FAIL(4002, "用户名或密码错误", "username or password error")
        ,USERNAME_EMPTY(4003, "用户名不能为空", "Username is null")
        ,PASSWORD_EMPTY(4004, "密码不能为空", "password is null")
        ,VERIFICATION_CODE_ERROR(4005,"验证码错误", "Verification code error")
        ,EMAIL_EMPTY(4006,"邮箱不能为空","Email is null")
        ,EMAIL_INVALID(4007,"邮箱不合法","email invalid")
        ,VERIFICATION_CODE_EMPTY(4008, "验证码不能为空", "verification code is null")
        ,USERNAME_EXISTS(4009, "用户名已存在", "username exists")
        ,EMAIL_EXISTS(4010,"邮箱已经存在", "email exists")
        ,EMAIL_NOT_EXISTS(4011,"邮箱不存在", "email not exists")
        ,INVITE_CODE_EXISTS(4012,"推荐码不存在", "invite code not exists")
        ,AVAILABLE_ASSETS_INSUFFICIENT(4013,"可用资产不足", "available assets insufficient")
        ,PROHIBIT_LOGIN(4014,"禁止登录", "prohibit login")
        ,CONCURRENT_EXECUTOR_ERROR(5000,"当前访问人数较多,请稍后再试","")
        ;
    
    
        private int code;
        private String de;
        private String zh;
        private String en;
    
        Messages(int code, String zh, String en) {
            this.de = zh;
            this.code = code;
            this.zh = zh;
            this.en = en;
        }
    
        public enum Lang {
            de,zh,en;
        }
    
        public int getCode() {
            return code;
        }
    
        public String getDe() {
            return de;
        }
    
        public String getZh() {
            return zh;
        }
    
        public String getEn() {
            return en;
        }
    }
    

    新建一个MessageListener类

    package com.example.demo.email;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.event.EventListener;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Component;
    
    import javax.mail.internet.MimeMessage;
    
    @Component
    public class MessageListener {
        @Autowired
        private JavaMailSender mailSender;
    
        @EventListener
        @Async
        public void simpleMailMessagEvent(SimpleMailMessagEvent event) throws Exception {
            mailSender.send((SimpleMailMessage)event.getSource());
        }
    
        @EventListener
        @Async
        public void mimeMailMessagEvent(MimeMailMessagEvent event) throws Exception {
            mailSender.send((MimeMessage)event.getSource());
        }
    }
    

    新建MailMessageService类

    package com.example.demo.email;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.ApplicationContext;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.stereotype.Service;
    
    @Service
    public class MailMessageService {
        @Value("${spring.mail.username}")
        private String username;
        @Autowired
        private ApplicationContext applicationContext;
    
        /**
         * 发送简单邮件
         * @param subject 主题
         * @param text 内容
         * @param to 收件人
         */
        public void sendSimple(String subject, String text, String... to){
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(username);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(text);
            applicationContext.publishEvent(new SimpleMailMessagEvent(message));
        }
    }
    

    新建一个接口Constants

    package com.example.demo.email;
    
    import java.math.BigDecimal;
    
    public interface Constants {
        String X_AUTH_TOKEN = "X-AUTH-TOKEN";
        // token过期时间单位分钟
        long TOKEN_EXPIRATION_TIMEOUT = 120L;
        // 邮箱注册验证码前缀
        String REGISTER_MAIL_CODE_PREFIX = "REGISTER_MAIL_CODE_";
        // 邮箱登录验证码前缀
        String LOGIN_MAIL_CODE_PREFIX = "LOGIN_MAIL_CODE_";
        // 国际化参数
        String LANGUAGE = "lang";
        // token存储前缀
        String TOKEN_PREFIX = "token_";
        // 用户与token关系
        String USER_TOKEN_PREFIX = "user_token_";
        // 小于这个值则忽略不计算
        BigDecimal IGNORE_EXEC = new BigDecimal(0.000001);
    
        // redis和token关系
        String LOGIN_TOKEN="LOGIN_TOKEN_";
        // MD5和salt关系
        String MD5_SALT="hehongqian";
    }
    

    新建一个MimeMailMessagEvent类

    package com.example.demo.email;
    
    import org.springframework.context.ApplicationEvent;
    import org.springframework.mail.javamail.MimeMailMessage;
    
    public class MimeMailMessagEvent extends ApplicationEvent {
        private static final long serialVersionUID = -6424588529678254971L;
    
        public MimeMailMessagEvent(MimeMailMessage message) {
            super(message);
        }
    }
    

    新建一个SimpleMailMessageEvent类

    package com.example.demo.email;
    import org.springframework.context.ApplicationEvent;
    import org.springframework.mail.SimpleMailMessage;
    
    public class SimpleMailMessagEvent extends ApplicationEvent {
        private static final long serialVersionUID = -6424588529678254971L;
    
        public SimpleMailMessagEvent(SimpleMailMessage message) {
            super(message);
        }
    }
    

    新建一个MailMessageController类

    package com.example.demo.controller;
    
    
    /*
     * @Author: i_heh
     * @CreateDate: 2019/7/1
     * @CreateTime: 11:01
     */
    
    import com.example.demo.email.MailMessageService;
    import org.flowable.engine.ProcessEngine;
    import org.flowable.engine.ProcessEngineConfiguration;
    import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/mail")
    public class MailMessageController {
    
        @Autowired
        private MailMessageService mailMessageService;
        // 测试邮件
        private static String topic = "测试邮件";
        // 内容
        private static String text = "今天天气真好!!!!!!!!!!!!!!!!!!";
        // 收件人
        private static String to = "i_hehongqian@sina.com";
    
        // 发送文字邮件
        @RequestMapping("/send")
        public String sendMail(){
            mailMessageService.sendSimple(topic,text,to);
            return "发送成功";
        }
        // 发送带附件邮件
        @RequestMapping("/sendFile")
        public String sendFileMail(){
            return null;
        }
    }
    

    在properties文件加入

    spring.mail.host=smtp.sina.com
    spring.mail.username=i_hehongqian@sina.com
    spring.mail.password=
    spring.mail.port=465
    spring.mail.default-encoding=UTF-8
    spring.mail.properties.mail.smtp.auth=true
    spring.mail.properties.mail.smtp.starttls.enable=true
    spring.mail.properties.mail.smtp.starttls.required=true
    spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
    

    spring.mail.password中的密码是需要登录到自己的邮箱,在设置-》账户里面开启POP3/SMTP服务,然后就会生成一个密码


    微信图片_20190705153457.png

    相关文章

      网友评论

          本文标题:springboot2.X集成email

          本文链接:https://www.haomeiwen.com/subject/nyrehctx.html