Gavrilo Adamovic
Gavrilo Adamovic

Reputation: 2795

Sending email in java spring mvc

I am having trouble sending an email in my spring webapp. I am using 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 protected]");
        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:

@Autowired
MailSender mailSender;

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

        SimpleMailMessage smm = new SimpleMailMessage();

        smm.setFrom("[email protected]");
        smm.setTo("[email protected]");
        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

Upvotes: 0

Views: 1442

Answers (2)

CuriousMind
CuriousMind

Reputation: 3173

You should also have mail.jar on the classpath. You can find reference solution here.

Upvotes: 2

user3906011
user3906011

Reputation:

Please verify, have you added mail.jar.

Upvotes: 1

Related Questions