简体   繁体   English

在Java Spring MVC中发送电子邮件

[英]Sending email in java spring mvc

I am having trouble sending an email in my spring webapp. 我在春季网络应用中无法发送电子邮件。 I am using spring mvc. 我正在使用Spring MVC。

I have mail configuration class: 我有邮件配置类:

@Configuration
class MailConfig {

     @Bean(name="mailSender")
    public MailSender javaMailService() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost("smtp.gmail.com");
        javaMailSender.setPort(587);
        javaMailSender.setProtocol("smtp");
        javaMailSender.setUsername("email@gmail.com");
        javaMailSender.setPassword("password");
        Properties mailProperties = new Properties();
        mailProperties.put("mail.smtp.auth", "true");
        mailProperties.put("mail.smtp.starttls.enable", "starttls");
        mailProperties.put("mail.smtp.debug", "true");
        javaMailSender.setJavaMailProperties(mailProperties);
        return javaMailSender;
    }

}

And in controller I have autowired instance of mailSender and I send emails like this: 在控制器中,我已自动连接mailSender实例,并发送如下电子邮件:

@Autowired
MailSender mailSender;

@RequestMapping(path="emailTest", method = {RequestMethod.GET})
    public void emailTest(){

        SimpleMailMessage smm = new SimpleMailMessage();

        smm.setFrom("email@gmail.com");
        smm.setTo("email@gmail.com");
        smm.setSubject("title");
        smm.setText("text");

        mailSender.send(smm);

    }

And when I try to send I get 当我尝试发送时,我得到了

HTTP Status 500 - Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: com/sun/mail/util/MessageRemovedIOException

You should also have mail.jar on the classpath. 您还应该在类路径上有mail.jar You can find reference solution here . 您可以在此处找到参考解决方案

请验证,是否添加了mail.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM