I have a Spring @Service to asynchronously send emails. The part that seems to be a bit off to me is some code that is specific to our QA environment. We want to send and receive all emails through a test email account so that it does not impact live users if we work with real data imported to QA. I check for the environment and some settings in .properties files and adjust the outgoing email accordingly.
Is there a better way to handle this?
@Async
public void send(String email, String subject, String body) {
if (email == null) {
log.error("Not sending; null input: email");
return;
}
try {
MimeMessage message = this.mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setFrom(mailSender.getUsername(), getFrom());
helper.setReplyTo(mailSender.getUsername());
helper.setSubject(subject);
//HERE IS THE PART THAT'S IFFY:
if ("QA".equalsIgnoreCase(EmailManager.getEnvironmentName())) {
if (isTestMode()) {
if (!email.equals(getTestEmailAddress())) {
helper.setTo(getTestEmailAddress());
body += "<br/>This email was originally intended for " + email +
" but was redirected to " + getTestEmailAddress() + " for testing.";
helper.setText(body, true); //HTML on
}
}
}
else {
helper.setTo(email);
helper.setText(body, true); //HTML on
}
if (message == null) {
log.error("Email message to send is null. Email={}, Subject={}", email, subject);
log.error("Body:");
log.error(body);
return;
}
this.mailSender.send(message);
}
catch (MailException | UnsupportedEncodingException | MessagingException e) {
log.error(e.toString());
e.printStackTrace();
}
}
private static String getFrom() {
Locale defaultLocale = Locale.getDefault();
ResourceBundle bundle = ResourceBundle.getBundle("messages", defaultLocale);
return bundle.getString("label.project.name");
}
protected static String getTestEmailAddress() {
return ResourceBundle.getBundle(AbstractEnvironment.ENVIRONMENT)
.getString("testEmailAddress");
}
protected static Boolean isTestMode() {
return Boolean.valueOf(ResourceBundle.getBundle(AbstractEnvironment.ENVIRONMENT)
.getString("redirectAllEmailsToTestEmailAddress"));
}
@Autowired
private JavaMailSenderImpl mailSender;
//setter needed for Junit
public void setMailSender(JavaMailSenderImpl mailSender) {
this.mailSender = mailSender;
}