美文网首页springboot
【框架集成6】email集成

【框架集成6】email集成

作者: yedp | 来源:发表于2019-11-22 09:47 被阅读0次

    pom.xml添加依赖

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

    添加aplication.yml

    spring:
      mail:
        host: smtp.126.com
        username: **@126.com
        password: ***
    

    服务

    
    import com.ydp.ez.user.service.IEmailService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.stereotype.Service;
    
    /**
     * @Author: yedp
     * @Date: 2019/11/22 9:24
     * @Description:邮件服务
     */
    @Service
    public class EmailService implements IEmailService {
        @Autowired
        private JavaMailSender javaMailSender;
        @Value("${spring.mail.username}")
        private String sendUser;
    
        @Override
        public void sendEmail(String receiveEmail, String subject, String content) {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(sendUser);
            message.setTo(receiveEmail);
            message.setSubject(subject);
            message.setText(content);
            javaMailSender.send(message);
        }
    }
    
    

    测试

    
    @SpringBootTest()
    public class EmailServiceTest {
        @Autowired
        IEmailService emailService;
    
        @Test
        public void sendMail() {
            try {
                String receiver = "**@qq.com";
                emailService.sendEmail(receiver, "你好", "测试");
            }catch (Exception e){
                System.out.println(e);
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:【框架集成6】email集成

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