Example usage for javax.mail.internet MimeMessage saveChanges

List of usage examples for javax.mail.internet MimeMessage saveChanges

Introduction

In this page you can find the example usage for javax.mail.internet MimeMessage saveChanges.

Prototype

@Override
public void saveChanges() throws MessagingException 

Source Link

Document

Updates the appropriate header fields of this message to be consistent with the message's contents.

Usage

From source file:com.zimbra.cs.mime.MimeTest.java

@Test
public void fixBase64LineWrapping() throws Exception {
    String textPlain = "Line 1 This is base64 encoded text message part. It does not have line folding. \r\n"
            + "Line 2 This is base64 encoded text message part. It does not have line folding. \r\n"
            + "Line 3 This is base64 encoded text message part. It does not have line folding. \r\n";
    String textHtml = "<html>\r\n" + "<body>\r\n"
            + "Line 1 This is base64 encoded html message part. It does not have line folding.  </ br>"
            + "Line 2 This is base64 encoded html message part. It does not have line folding. </ br>\r\n"
            + "Line 3 This is base64 encoded html message part. It does not have line folding. \r\n"
            + "</ br>\r\n" + "</body>\r\n" + "</html>";
    InputStream is = getClass().getResourceAsStream("base64mime.txt");
    MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), is);
    mm.saveChanges();
    String dataBeforeFix = IOUtils.toString(mm.getInputStream());

    Mime.fixBase64MimePartLineFolding(mm);
    mm.saveChanges();//from  w  ww.  j  ava2s.c o  m
    String dataAfterFix = IOUtils.toString(mm.getInputStream());
    MPartInfo mpiText = Mime.getTextBody(Mime.getParts(mm), false);
    MPartInfo mpiHtml = Mime.getTextBody(Mime.getParts(mm), true);

    Assert.assertFalse("Line folding should take place.", dataAfterFix.equals(dataBeforeFix));

    Assert.assertTrue("Text Part Content-Transfer-Encoding header should be preserved",
            mpiText.getMimePart().getHeader("Content-Transfer-Encoding", ":").equals("base64"));
    Assert.assertTrue("HTML Part Content-Transfer-Encoding header should be preserved",
            mpiHtml.getMimePart().getHeader("Content-Transfer-Encoding", ":").equals("base64"));

    Assert.assertTrue("Text Part Content-Disposition header should be preserved",
            mpiText.mDisposition.equals("inline"));
    Assert.assertTrue("HTML Part Content-Disposition header should be preserved",
            mpiHtml.mDisposition.equals("inline"));

    Assert.assertTrue("Text data should not be modified",
            TestUtil.bytesEqual(textPlain.getBytes(), mpiText.getMimePart().getInputStream()));
    Assert.assertTrue("Html data should not be modified",
            TestUtil.bytesEqual(textHtml.getBytes(), mpiHtml.getMimePart().getInputStream()));
}

From source file:mitm.application.djigzo.james.mailets.DKIMSign.java

@Override
public void serviceMail(Mail mail) {
    try {/*from ww w  .  j a v a2 s  . co  m*/
        MimeMessage message = mail.getMessage();

        if (convertTo7Bit) {
            /*
             * Before signing, the message should be converted to 7bit to make sure
             * the signature 'survives' the internet.
             */
            boolean converted = MailUtils.convertTo7Bit(message);

            if (converted) {
                message.saveChanges();
            }
        }

        PrivateKey privateKey = getPrivateKey(mail);

        if (privateKey != null) {
            DKIMSigner signer = new DKIMSigner(getSignatureTemplate(), privateKey);

            signer.setDKIMHeader(dkimHeader);

            String signature = signer.sign(message);

            /*
             * the signature field cannot be folded after signing in the simple mode
             * because that would break the signature
             */
            if (foldSignature) {
                signature = MimeUtility.fold(dkimHeader.length(), signature);
            }

            HeaderUtils.prependHeaderLine(message, signature);

            /*
             * Validate the message to make sure that James can write the message. 
             * 
             * Note: Because normally only a header is added, the message-id is not changed. The
             * exception to this is when the message is converted from 8bit to 7bit (see above).
             * When the message is converted from 7bit to 8bit the message-id is changed.
             */
            MailUtils.validateMessage(message);
        } else {
            logger.warn("Unable to sign the message because the private key is missing.");
        }
    } catch (MessagingException e) {
        logger.error("DKIM signing failed.", e);
    } catch (IOException e) {
        logger.error("DKIM signing failed.", e);
    } catch (FailException e) {
        logger.error("DKIM signing failed.", e);
    }
}

From source file:mitm.common.mail.MailUtilsTest.java

@Test
public void testUnknownContentTypeMultipartAddPart() throws IOException, MessagingException {
    MimeMessage message = loadMessage("unknown-content-type-multipart.eml");

    MimeMultipart multipart = (MimeMultipart) message.getContent();

    BodyPart newPart = new MimeBodyPart();
    newPart.setContent("new part", "text/plain");

    multipart.addBodyPart(newPart);//  w w  w . j  ava 2 s  . c  o m

    message.saveChanges();

    MailUtils.validateMessage(message);

    File file = new File("test/tmp/testunknowncontenttypemultipartaddpart.eml");

    MailUtils.writeMessage(message, file);
}

From source file:mitm.application.djigzo.james.matchers.HasValidPasswordTest.java

@Test
public void testNullDatePasswordSet() throws MessagingException, EncryptorException {
    HasValidPassword matcher = new HasValidPassword();

    MockMatcherConfig matcherConfig = new MockMatcherConfig("test", "matchOnError=false", mailetContext);

    matcher.init(matcherConfig);/*ww  w. java2 s .c o  m*/

    Mail mail = new MockMail();

    MimeMessage message = new MimeMessage(MailSession.getDefaultSession());

    message.setContent("test", "text/plain");
    message.setFrom(new InternetAddress("123456@example.com"));

    message.saveChanges();

    mail.setMessage(message);

    Collection<MailAddress> recipients = new LinkedList<MailAddress>();

    recipients.add(new MailAddress("test6@example.com"));

    mail.setRecipients(recipients);

    Collection<?> result = matcher.match(mail);

    assertEquals(1, result.size());
}

From source file:mitm.application.djigzo.james.matchers.HasValidPasswordTest.java

@Test
public void testNegativeValidityInterval() throws MessagingException, EncryptorException {
    HasValidPassword matcher = new HasValidPassword();

    MockMatcherConfig matcherConfig = new MockMatcherConfig("test", "matchOnError=false", mailetContext);

    matcher.init(matcherConfig);/*from   w ww. jav  a  2 s. c o m*/

    Mail mail = new MockMail();

    MimeMessage message = new MimeMessage(MailSession.getDefaultSession());

    message.setContent("test", "text/plain");
    message.setFrom(new InternetAddress("123456@example.com"));

    message.saveChanges();

    mail.setMessage(message);

    Collection<MailAddress> recipients = new LinkedList<MailAddress>();

    recipients.add(new MailAddress("test7@example.com"));

    mail.setRecipients(recipients);

    Collection<?> result = matcher.match(mail);

    assertEquals(1, result.size());
}

From source file:mitm.application.djigzo.james.matchers.HasValidPasswordTest.java

@Test
public void testZeroValidityInterval() throws MessagingException, EncryptorException {
    HasValidPassword matcher = new HasValidPassword();

    MockMatcherConfig matcherConfig = new MockMatcherConfig("test", "matchOnError=false", mailetContext);

    matcher.init(matcherConfig);/*from  ww  w  .j  a v  a 2s  .co  m*/

    Mail mail = new MockMail();

    MimeMessage message = new MimeMessage(MailSession.getDefaultSession());

    message.setContent("test", "text/plain");
    message.setFrom(new InternetAddress("123456@example.com"));

    message.saveChanges();

    mail.setMessage(message);

    Collection<MailAddress> recipients = new LinkedList<MailAddress>();

    recipients.add(new MailAddress("test1@example.com"));

    mail.setRecipients(recipients);

    Collection<?> result = matcher.match(mail);

    assertEquals(0, result.size());
}

From source file:mitm.application.djigzo.james.matchers.HasValidPasswordTest.java

@Test
public void testExpired() throws MessagingException, EncryptorException {
    HasValidPassword matcher = new HasValidPassword();

    MockMatcherConfig matcherConfig = new MockMatcherConfig("test", "matchOnError=false", mailetContext);

    matcher.init(matcherConfig);//w w  w. j  ava 2s .  co m

    Mail mail = new MockMail();

    MimeMessage message = new MimeMessage(MailSession.getDefaultSession());

    message.setContent("test", "text/plain");
    message.setFrom(new InternetAddress("123456@example.com"));

    message.saveChanges();

    mail.setMessage(message);

    Collection<MailAddress> recipients = new LinkedList<MailAddress>();

    recipients.add(new MailAddress("test3@example.com"));

    mail.setRecipients(recipients);

    Collection<?> result = matcher.match(mail);

    assertEquals(0, result.size());
}

From source file:mitm.application.djigzo.james.matchers.HasValidPasswordTest.java

@Test
public void testUnknownUser() throws MessagingException, EncryptorException {
    HasValidPassword matcher = new HasValidPassword();

    MockMatcherConfig matcherConfig = new MockMatcherConfig("test", "matchOnError=false", mailetContext);

    matcher.init(matcherConfig);//from   w w w.  j a  va 2s  . c o  m

    Mail mail = new MockMail();

    MimeMessage message = new MimeMessage(MailSession.getDefaultSession());

    message.setContent("test", "text/plain");
    message.setFrom(new InternetAddress("123456@example.com"));

    message.saveChanges();

    mail.setMessage(message);

    Collection<MailAddress> recipients = new LinkedList<MailAddress>();

    recipients.add(new MailAddress("unknown@example.com"));

    mail.setRecipients(recipients);

    Collection<?> result = matcher.match(mail);

    assertEquals(0, result.size());
}

From source file:mitm.application.djigzo.james.matchers.HasValidPasswordTest.java

@Test
public void testNullPassword() throws MessagingException, EncryptorException {
    HasValidPassword matcher = new HasValidPassword();

    MockMatcherConfig matcherConfig = new MockMatcherConfig("test", "matchOnError=false", mailetContext);

    matcher.init(matcherConfig);//from  www  .j a  va 2s. c om

    Mail mail = new MockMail();

    MimeMessage message = new MimeMessage(MailSession.getDefaultSession());

    message.setContent("test", "text/plain");
    message.setFrom(new InternetAddress("123456@example.com"));

    message.saveChanges();

    mail.setMessage(message);

    Collection<MailAddress> recipients = new LinkedList<MailAddress>();

    recipients.add(new MailAddress("test4@example.com"));

    mail.setRecipients(recipients);

    Collection<?> result = matcher.match(mail);

    assertEquals(0, result.size());
}

From source file:mitm.common.mail.MailUtilsTest.java

@Test
public void testSaveNewMessage() throws IOException, MessagingException {
    File outputFile = File.createTempFile("mailUtilsTest", ".eml");

    outputFile.deleteOnExit();//from  w  ww. j ava2  s.  co m

    MimeMessage message = new MimeMessage(MailSession.getDefaultSession());

    message.addFrom(new InternetAddress[] { new InternetAddress("test@example.com") });
    message.addRecipients(RecipientType.TO, "recipient@example.com");
    message.setContent("test body", "text/plain");
    message.saveChanges();

    MailUtils.writeMessage(message, new FileOutputStream(outputFile));

    MimeMessage loadedMessage = MailUtils.loadMessage(outputFile);

    String recipients = ArrayUtils.toString(loadedMessage.getRecipients(RecipientType.TO));

    assertEquals("{recipient@example.com}", recipients);

    String from = ArrayUtils.toString(loadedMessage.getFrom());

    assertEquals("{test@example.com}", from);
}