この問題が古いことはわかっていますが、xml ではなくクラスにプロパティが追加された SimpleMailMessage の解決策は見つかりませんでした。アプリの起動後に例外はありません。単にメールを送信していません。メソッド CustomMailDeliveryImpl.sendMail() は Controllerller からトリガーされます。間違いが見つかりません。
@Component("CustomMailDelivery")
public class CustomMailDeliveryImpl implements CustomMailDelivery {
protected static Logger logger = Logger.getLogger(CustomMailDeliveryImpl.class);
private EmployeeService employeeService;
private String username = "* * *@gmail.com";
private String password = "* * *";
private JavaMailSenderImpl sender = new JavaMailSenderImpl();
@Autowired
public void setSender(JavaMailSenderImpl sender) {
this.sender = sender;
}
@Autowired
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
/**
* @param from - must contain address of sender,
* @param to - must contain address of target,
* @param subject - must be included with short description,
* @param body - main message of mail.
* @throws RuntimeException if any param is empty.
*/
public void sendMail(String from, String to, String subject, String body) {
// selecting settings from DB
SystemSettingsModel settingsModel = employeeService.getSettingsByName("default");
// defining settings
sender.setHost(settingsModel.getSmtpServerAddress());
sender.setPort(settingsModel.getSmtpServerPort());
sender.setUsername(username);
sender.setPassword(password);
// defining SMTP properties
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.debug", "1");
Session session = Session.getDefaultInstance(properties, null);
sender.setSession(session);
// initiating mail
SimpleMailMessage simpleMessage = new SimpleMailMessage();
try {
if (from != null && to != null) {
if (subject != null && body != null) {
simpleMessage.setFrom(from);
simpleMessage.setTo(to);
simpleMessage.setSubject(subject);
simpleMessage.setText(body);
sender.send(simpleMessage);
logger.debug("Mail has been sent successfully");
} else {
throw new MessagingException("Subject and message of mail is empty!");
}
} else {
throw new MessagingException("Address of object and target is empty!");
}
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
助けていただければ幸いです!!!!