use of org.motechproject.email.domain.Mail in project motech by motech.
the class MotechMimeMessagePreparatorTest method shouldPrepareMimeMessage.
@Test
public void shouldPrepareMimeMessage() throws MessagingException, IOException {
String fromAddress = "from@emaildomain.com";
String toAddress = "to@emaildomain.com";
String text = "mail body";
String subject = "Mail subject";
Mail mail = new Mail(fromAddress, toAddress, subject, text);
MotechMimeMessagePreparator preparator = new MotechMimeMessagePreparator(mail);
Session session = Session.getDefaultInstance(new Properties());
MimeMessage mimeMessage = new MimeMessage(session);
preparator.prepare(mimeMessage);
assertThat(mimeMessage.getFrom()[0].toString(), IsEqual.equalTo(fromAddress));
assertThat(mimeMessage.getAllRecipients()[0].toString(), IsEqual.equalTo(toAddress));
assertThat(mimeMessage.getDataHandler().getContent().toString(), IsEqual.equalTo(text));
assertThat(mimeMessage.getSubject(), IsEqual.equalTo(subject));
}
use of org.motechproject.email.domain.Mail in project motech by motech.
the class EmailSenderServiceImpl method send.
@Override
@Transactional
public void send(String fromAddress, String toAddress, String subject, String message) throws EmailSendException {
Mail mail = new Mail(fromAddress, toAddress, subject, message);
LOGGER.info(String.format("Sending message [%s] from [%s] to [%s] with subject [%s].", mail.getMessage(), mail.getFromAddress(), mail.getToAddress(), mail.getSubject()));
try {
mailSender.send(getMimeMessagePreparator(mail));
log(new EmailRecord(mail.getFromAddress(), mail.getToAddress(), mail.getSubject(), mail.getMessage(), now(), DeliveryStatus.SENT));
} catch (MailException e) {
log(new EmailRecord(mail.getFromAddress(), mail.getToAddress(), mail.getSubject(), mail.getMessage(), now(), DeliveryStatus.ERROR));
throw new EmailSendException("Unable to send an email to " + mail.getToAddress(), e);
}
}
Aggregations