我在我的spring webapp中發送郵件時遇到問題。我正在使用spring mvc。在java spring發送電子郵件mvc
我有郵件配置類:
@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;
}
}
而在控制器我已經自動裝配的mailSender
實例,我發送電子郵件這樣的:
@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);
}
當我嘗試發送我得到
HTTP Status 500 - Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: com/sun/mail/util/MessageRemovedIOException
我想添加[javax.mail](https://mvnrepository.com/artifact/com.sun.mail/javax.mail/1.6.0)dependency會解決你的問題。 –
@SergeyProkofiev謝謝,它確實,但現在我得到了'SMTPSendFailedException:530 5.7.0必須首先發出一個STARTTLS命令。 a69sm529238wme.29 - gsmtp' – wdc