Search in sources :

Example 1 with Attachment

use of org.exoplatform.services.mail.Attachment in project kernel by exoplatform.

the class TestMailService method testSendMessage.

public void testSendMessage() throws Exception {
    Message message = new Message();
    message.setFrom(generateRandomEmailSender());
    message.setTo(generateRandomEmailRecipient());
    message.setCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
    message.setBCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
    message.setSubject(MAIL_SUBJECT);
    message.setBody(MAIL_CONTENTS);
    message.setMimeType(TEXT_HTML);
    Attachment attachment = new Attachment();
    attachment.setInputStream(new ByteArrayInputStream(ATTACHMENT.getBytes()));
    attachment.setMimeType(TEXT_PLAIN);
    message.addAttachment(attachment);
    assertEquals("SMTP server should be now empty", 0, mailServer.getMessages().size());
    assertFalse(isEmailMessageSent(MAIL_SUBJECT));
    service.sendMessage(message);
    Thread.sleep(ONE_SECOND);
    assertEquals("SMTP server should have one message", 5, mailServer.getMessages().size());
    assertTrue(isEmailMessageSent(MAIL_SUBJECT));
}
Also used : Message(org.exoplatform.services.mail.Message) WiserMessage(org.subethamail.wiser.WiserMessage) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) Attachment(org.exoplatform.services.mail.Attachment)

Example 2 with Attachment

use of org.exoplatform.services.mail.Attachment in project kernel by exoplatform.

the class TestMailService method testSendMessageAsynchExceptionCause.

/**
 * Here we test asynchronous email sending of {@link Message}.
 * We check if we can get real cause of exception, if that occurs during
 * message sending process.
 */
public void testSendMessageAsynchExceptionCause() throws Exception {
    Attachment attachment = new Attachment();
    attachment.setInputStream(new ByteArrayInputStream(ATTACHMENT.getBytes()));
    attachment.setMimeType(TEXT_PLAIN);
    Message message = new Message();
    message.setFrom("!@#$%^&*()");
    message.setTo(generateRandomEmailRecipient());
    message.setCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
    message.setBCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
    message.setSubject(MAIL_SUBJECT);
    message.setBody(MAIL_CONTENTS);
    message.setMimeType(TEXT_HTML);
    message.addAttachment(attachment);
    Future<Boolean> future = service.sendMessageInFuture(message);
    try {
        future.get();
        fail();
    } catch (ExecutionException ee) {
        assertEquals("We tried to send mail with malformed sender field," + " so we expect an AdressException to be the real cause of ExecutionException", ee.getCause().getClass(), AddressException.class);
    }
}
Also used : Message(org.exoplatform.services.mail.Message) WiserMessage(org.subethamail.wiser.WiserMessage) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) AddressException(javax.mail.internet.AddressException) Attachment(org.exoplatform.services.mail.Attachment) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with Attachment

use of org.exoplatform.services.mail.Attachment in project kernel by exoplatform.

the class MailServiceImpl method sendMessage.

/**
 * {@inheritDoc}
 */
public void sendMessage(Message message) throws Exception {
    MimeMessage mimeMessage = new MimeMessage(getMailSession());
    String FROM = message.getFrom();
    String TO = message.getTo();
    String CC = message.getCC();
    String BCC = message.getBCC();
    String subject = message.getSubject();
    String mimeType = message.getMimeType();
    String body = message.getBody();
    List<Attachment> attachment = message.getAttachment();
    // set From to the message
    if (FROM != null && !FROM.equals("")) {
        InternetAddress sentFrom = new InternetAddress(FROM);
        if (sentFrom.getPersonal() != null) {
            sentFrom.setPersonal(sentFrom.getPersonal(), "UTF-8");
        }
        mimeMessage.setFrom(sentFrom);
    }
    // set To to the message
    InternetAddress[] sendTo = new InternetAddress[getArrs(TO).length];
    for (int i = 0; i < getArrs(TO).length; i++) {
        sendTo[i] = new InternetAddress(getArrs(TO)[i]);
        if (sendTo[i].getPersonal() != null) {
            sendTo[i].setPersonal(sendTo[i].getPersonal(), "UTF-8");
        }
    }
    mimeMessage.setRecipients(javax.mail.Message.RecipientType.TO, sendTo);
    // set CC to the message
    if ((getArrs(CC) != null) && (getArrs(CC).length > 0)) {
        InternetAddress[] copyTo = new InternetAddress[getArrs(CC).length];
        for (int i = 0; i < getArrs(CC).length; i++) {
            copyTo[i] = new InternetAddress(getArrs(CC)[i]);
            if (copyTo[i].getPersonal() != null) {
                copyTo[i].setPersonal(copyTo[i].getPersonal(), "UTF-8");
            }
        }
        mimeMessage.setRecipients(javax.mail.Message.RecipientType.CC, copyTo);
    }
    // set BCC to the message
    if ((getArrs(BCC) != null) && (getArrs(BCC).length > 0)) {
        InternetAddress[] bccTo = new InternetAddress[getArrs(BCC).length];
        for (int i = 0; i < getArrs(BCC).length; i++) {
            bccTo[i] = new InternetAddress(getArrs(BCC)[i]);
            if (bccTo[i].getPersonal() != null) {
                bccTo[i].setPersonal(bccTo[i].getPersonal(), "UTF-8");
            }
        }
        mimeMessage.setRecipients(javax.mail.Message.RecipientType.BCC, bccTo);
    }
    // set Subject to the message
    mimeMessage.setSubject(subject);
    mimeMessage.setSubject(message.getSubject(), "UTF-8");
    mimeMessage.setSentDate(new Date());
    MimeMultipart multipPartRoot = new MimeMultipart("mixed");
    MimeMultipart multipPartContent = new MimeMultipart("alternative");
    if (attachment != null && attachment.size() != 0) {
        MimeBodyPart contentPartRoot = new MimeBodyPart();
        if (mimeType != null && mimeType.indexOf("text/plain") > -1)
            contentPartRoot.setContent(body, "text/plain; charset=utf-8");
        else
            contentPartRoot.setContent(body, "text/html; charset=utf-8");
        MimeBodyPart mimeBodyPart1 = new MimeBodyPart();
        mimeBodyPart1.setContent(body, mimeType);
        multipPartContent.addBodyPart(mimeBodyPart1);
        multipPartRoot.addBodyPart(contentPartRoot);
        for (Attachment att : attachment) {
            InputStream is = att.getInputStream();
            MimeBodyPart mimeBodyPart = new MimeBodyPart();
            ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource(is, att.getMimeType());
            mimeBodyPart.setDataHandler(new DataHandler(byteArrayDataSource));
            mimeBodyPart.setDisposition(Part.ATTACHMENT);
            if (att.getName() != null)
                mimeBodyPart.setFileName(MimeUtility.encodeText(att.getName(), "utf-8", null));
            multipPartRoot.addBodyPart(mimeBodyPart);
        }
        mimeMessage.setContent(multipPartRoot);
    } else {
        if (mimeType != null && mimeType.indexOf("text/plain") > -1)
            mimeMessage.setContent(body, "text/plain; charset=utf-8");
        else
            mimeMessage.setContent(body, "text/html; charset=utf-8");
    }
    sendMessage(mimeMessage);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) InputStream(java.io.InputStream) Attachment(org.exoplatform.services.mail.Attachment) DataHandler(javax.activation.DataHandler) Date(java.util.Date) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 4 with Attachment

use of org.exoplatform.services.mail.Attachment in project kernel by exoplatform.

the class TestMailService method testSendMessageInFuture.

/**
 * Here we test asynchronous email sending of {@link Message}.
 * We check concurrent execution of {@link FutureTask}
 */
public void testSendMessageInFuture() throws Exception {
    Message message;
    Attachment attachment = new Attachment();
    attachment.setInputStream(new ByteArrayInputStream(ATTACHMENT.getBytes()));
    attachment.setMimeType(TEXT_PLAIN);
    @SuppressWarnings("unchecked") Future<Boolean>[] futures = new Future[THREAD_NUMBER];
    assertEquals("SMTP server should be now empty", 0, mailServer.getMessages().size());
    for (int i = 0; i < THREAD_NUMBER; i++) {
        assertFalse(isEmailMessageSent(MAIL_SUBJECT + i));
        message = new Message();
        message.setFrom(generateRandomEmailSender());
        message.setTo(generateRandomEmailRecipient());
        message.setCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
        message.setBCC(generateRandomEmailRecipient() + "," + generateRandomEmailRecipient());
        message.setSubject(MAIL_SUBJECT + i);
        message.setBody(MAIL_CONTENTS + i);
        message.setMimeType(TEXT_HTML);
        message.addAttachment(attachment);
        futures[i] = service.sendMessageInFuture(message);
    }
    for (int i = 0; i < THREAD_NUMBER; i++) {
        assertTrue(futures[i].get());
        assertTrue(isEmailMessageSent(MAIL_SUBJECT + i));
    }
    // we assume that one thread sends one email
    assertEquals("SMTP server should have" + (5 * THREAD_NUMBER) + " message (asynchronously sent)", 5 * THREAD_NUMBER, mailServer.getMessages().size());
}
Also used : Message(org.exoplatform.services.mail.Message) WiserMessage(org.subethamail.wiser.WiserMessage) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) Future(java.util.concurrent.Future) Attachment(org.exoplatform.services.mail.Attachment)

Aggregations

MimeMessage (javax.mail.internet.MimeMessage)4 Attachment (org.exoplatform.services.mail.Attachment)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Message (org.exoplatform.services.mail.Message)3 WiserMessage (org.subethamail.wiser.WiserMessage)3 InputStream (java.io.InputStream)1 Date (java.util.Date)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 DataHandler (javax.activation.DataHandler)1 AddressException (javax.mail.internet.AddressException)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1 MimeMultipart (javax.mail.internet.MimeMultipart)1 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)1