问答详情
源自:2-4 文本邮件

启动之后,一直报空指针异常

sendMessageTest(com.neo.helloWord.service.ServiceTest)  Time elapsed: 0.026 sec  <<< ERROR!

java.lang.NullPointerException: null

at com.neo.helloWord.hello.MailService.sendSimpleMail(MailService.java:34)

at com.neo.helloWord.service.ServiceTest.sendMessageTest(ServiceTest.java:26)


2019-04-03 16:06:14.461  INFO 5236 --- [       Thread-2] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@26275bef: startup date [Wed Apr 03 16:06:10 CST 2019]; root of context hierarchy


Results :


Tests in error: 

  ServiceTest.sendMessageTest:26 ? NullPointer


提问者:lhw901013 2019-04-03 16:09

个回答

  • qq_虚伪的宋痞子_0
    2019-06-03 14:08:21

    请问这个问题解决了吗

    java.lang.NullPointerException

    报的是这个错误,下面是代码内容

    try{
        Hello h = new Hello();
        h.SimpleEmail("shizengs@aliyun.com", "This is Subject", "This is Content");
    }catch (Exception e){
        System.out.println(e);
    }


    @RestController
    public class Hello {
        @Value("${spring.mail.username}")
        private String from;
    
        @Autowired
        private JavaMailSender mailSender;
    
        @RequestMapping(value="/sendEmail", method = RequestMethod.GET)
        public void SimpleEmail(String to, String subject, String content){
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setTo(to);
            simpleMailMessage.setSubject(subject);
            simpleMailMessage.setText(content);
            simpleMailMessage.setFrom(from);
            mailSender.send(simpleMailMessage);
        }
    
    }


  • qq_一曲离殇丶笑看人世繁华_3
    2019-05-09 07:36:42

    我也遇到了,请问你是怎么解决的


  • 罗汉爷
    2019-04-20 23:44:36

    spring boot实现邮箱验证码注册


    https://blog.csdn.net/IndexMan/article/details/89409512