Search in sources :

Example 1 with Mail

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));
}
Also used : Mail(org.motechproject.email.domain.Mail) MimeMessage(javax.mail.internet.MimeMessage) Properties(java.util.Properties) Session(javax.mail.Session) Test(org.junit.Test)

Example 2 with Mail

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);
    }
}
Also used : EmailRecord(org.motechproject.email.domain.EmailRecord) Mail(org.motechproject.email.domain.Mail) EmailSendException(org.motechproject.email.exception.EmailSendException) MailException(org.springframework.mail.MailException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Mail (org.motechproject.email.domain.Mail)2 Properties (java.util.Properties)1 Session (javax.mail.Session)1 MimeMessage (javax.mail.internet.MimeMessage)1 Test (org.junit.Test)1 EmailRecord (org.motechproject.email.domain.EmailRecord)1 EmailSendException (org.motechproject.email.exception.EmailSendException)1 MailException (org.springframework.mail.MailException)1 Transactional (org.springframework.transaction.annotation.Transactional)1