List of usage examples for javax.mail.internet MimeMessage saveChanges
@Override public void saveChanges() throws MessagingException
From source file:org.apache.james.core.MimeMessageUtil.java
/** * // ww w.j a v a 2 s.com * @param message * @param headerOs * @param bodyOs * @param ignoreList * @throws MessagingException * @throws IOException * @throws UnsupportedDataTypeException */ public static void writeToInternal(MimeMessage message, OutputStream headerOs, OutputStream bodyOs, String[] ignoreList) throws MessagingException, IOException { if (message.getMessageID() == null) { message.saveChanges(); } writeHeadersTo(message, headerOs, ignoreList); // Write the body to the output stream writeMessageBodyTo(message, bodyOs); }
From source file:org.apache.james.smtpserver.SetMimeHeaderHandler.java
/** * Adds header to the message/*from w ww . j av a2 s. c o m*/ * * @see org.apache.james.smtpserver.JamesMessageHook#onMessage(org.apache.james.protocols.smtp.SMTPSession, * org.apache.mailet.Mail) */ public HookResult onMessage(SMTPSession session, Mail mail) { try { MimeMessage message = mail.getMessage(); // Set the header name and value (supplied at init time). if (headerName != null) { message.setHeader(headerName, headerValue); message.saveChanges(); } } catch (javax.mail.MessagingException me) { session.getLogger().error(me.getMessage()); } return new HookResult(HookReturnCode.DECLINED); }
From source file:org.apache.james.transport.mailets.managesieve.ManageSieveMailetTestCase.java
@Test public final void testCapabilityExtraArguments() throws Exception { MimeMessage message = prepareMimeMessage("CAPABILITY"); Mail mail = createUnauthenticatedMail(message); message.setSubject("CAPABILITY extra"); message.saveChanges(); mailet.service(mail);/*from ww w . j a va 2 s .com*/ ensureResponse("Re: CAPABILITY extra", "NO \"Too many arguments: extra\""); }
From source file:org.apache.james.transport.mailets.managesieve.ManageSieveMailetTestCase.java
private MimeMessage prepareMimeMessage(String subject) throws MessagingException { MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); message.setSubject(subject);// w w w .j av a2s . c o m message.setSender(new InternetAddress(USER)); message.setRecipient(RecipientType.TO, new InternetAddress(SIEVE_LOCALHOST)); message.saveChanges(); return message; }
From source file:org.apache.james.transport.mailets.managesieve.ManageSieveMailetTestCase.java
private MimeMessage prepareMessageWithAttachment(String scriptContent, String subject) throws MessagingException, IOException { MimeMessage message = prepareMimeMessage(subject); MimeMultipart multipart = new MimeMultipart(); MimeBodyPart scriptPart = new MimeBodyPart(); scriptPart.setDataHandler(//w ww .j av a 2 s . c o m new DataHandler(new ByteArrayDataSource(scriptContent, "application/sieve; charset=UTF-8"))); scriptPart.setDisposition(MimeBodyPart.ATTACHMENT); // setting a DataHandler with no mailcap definition is not // supported by the specs. Javamail activation still work, // but Geronimo activation translate it to text/plain. // Let's manually force the header. scriptPart.setHeader("Content-Type", "application/sieve; charset=UTF-8"); scriptPart.setFileName(SCRIPT_NAME); multipart.addBodyPart(scriptPart); message.setContent(multipart); message.saveChanges(); return message; }
From source file:org.apache.james.transport.mailets.SMIMEDecrypt.java
/** * @see org.apache.mailet.Mailet#service(org.apache.mailet.Mail) *//*from w w w. ja v a2 s . c o m*/ @SuppressWarnings("unchecked") public void service(Mail mail) throws MessagingException { MimeMessage message = mail.getMessage(); Part strippedMessage = null; log("Starting message decryption.."); if (message.isMimeType("application/x-pkcs7-mime") || message.isMimeType("application/pkcs7-mime")) { try { SMIMEEnveloped env = new SMIMEEnveloped(message); RecipientInformationStore informationStore = env.getRecipientInfos(); Collection<RecipientInformation> recipients = informationStore.getRecipients(); for (RecipientInformation info : recipients) { RecipientId id = info.getRID(); if (id.match(certificateHolder)) { try { JceKeyTransEnvelopedRecipient recipient = new JceKeyTransEnvelopedRecipient( keyHolder.getPrivateKey()); // strippedMessage contains the decrypted message. strippedMessage = SMIMEUtil.toMimeBodyPart(info.getContent(recipient)); log("Encrypted message decrypted"); } catch (Exception e) { throw new MessagingException("Error during the decryption of the message", e); } } else { log("Found an encrypted message but it isn't encrypted for the supplied key"); } } } catch (CMSException e) { throw new MessagingException("Error during the decryption of the message", e); } } // if the decryption has been successful.. if (strippedMessage != null) { // I put the private key's public certificate as a mailattribute. // I create a list of certificate because I want to minic the // behavior of the SMIMEVerifySignature mailet. In that way // it is possible to reuse the same matchers to analyze // the result of the operation. ArrayList<X509Certificate> list = new ArrayList<X509Certificate>(1); list.add(keyHolder.getCertificate()); mail.setAttribute(mailAttribute, list); // I start the message stripping. try { MimeMessage newMessage = new MimeMessage(message); newMessage.setText(text(strippedMessage), Charsets.UTF_8.name()); if (!strippedMessage.isMimeType("multipart/*")) { newMessage.setDisposition(null); } newMessage.saveChanges(); mail.setMessage(newMessage); } catch (IOException e) { log("Error during the strip of the encrypted message"); throw new MessagingException("Error during the stripping of the encrypted message", e); } } }
From source file:org.apache.james.transport.mailets.StripAttachmentTest.java
@Test public void testSimpleAttachment() throws MessagingException, IOException { Mailet mailet = initMailet();/*from w ww . j a v a 2s . c om*/ MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); MimeMultipart mm = new MimeMultipart(); MimeBodyPart mp = new MimeBodyPart(); mp.setText("simple text"); mm.addBodyPart(mp); String body = "\u0023\u00A4\u00E3\u00E0\u00E9"; MimeBodyPart mp2 = new MimeBodyPart(new ByteArrayInputStream( ("Content-Transfer-Encoding: 8bit\r\nContent-Type: application/octet-stream; charset=utf-8\r\n\r\n" + body).getBytes("UTF-8"))); mp2.setDisposition("attachment"); mp2.setFileName("10.tmp"); mm.addBodyPart(mp2); String body2 = "\u0014\u00A3\u00E1\u00E2\u00E4"; MimeBodyPart mp3 = new MimeBodyPart(new ByteArrayInputStream( ("Content-Transfer-Encoding: 8bit\r\nContent-Type: application/octet-stream; charset=utf-8\r\n\r\n" + body2).getBytes("UTF-8"))); mp3.setDisposition("attachment"); mp3.setFileName("temp.zip"); mm.addBodyPart(mp3); message.setSubject("test"); message.setContent(mm); message.saveChanges(); Mail mail = FakeMail.builder().mimeMessage(message).build(); mailet.service(mail); ByteArrayOutputStream rawMessage = new ByteArrayOutputStream(); mail.getMessage().writeTo(rawMessage, new String[] { "Bcc", "Content-Length", "Message-ID" }); @SuppressWarnings("unchecked") Collection<String> c = (Collection<String>) mail .getAttribute(StripAttachment.SAVED_ATTACHMENTS_ATTRIBUTE_KEY); Assert.assertNotNull(c); Assert.assertEquals(1, c.size()); String name = c.iterator().next(); File f = new File("./" + name); try { InputStream is = new FileInputStream(f); String savedFile = toString(is); is.close(); Assert.assertEquals(body, savedFile); } finally { FileUtils.deleteQuietly(f); } }
From source file:org.apache.james.transport.mailets.StripAttachmentTest.java
@Test public void testSimpleAttachment2() throws MessagingException, IOException { Mailet mailet = new StripAttachment(); FakeMailetConfig mci = new FakeMailetConfig("Test", FakeMailContext.defaultContext()); mci.setProperty("directory", "./"); mci.setProperty("remove", "all"); mci.setProperty("notpattern", "^(winmail\\.dat$)"); mailet.init(mci);/*from w w w . j av a 2 s . com*/ MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); MimeMultipart mm = new MimeMultipart(); MimeBodyPart mp = new MimeBodyPart(); mp.setText("simple text"); mm.addBodyPart(mp); String body = "\u0023\u00A4\u00E3\u00E0\u00E9"; MimeBodyPart mp2 = new MimeBodyPart( new ByteArrayInputStream(("Content-Transfer-Encoding: 8bit\r\n\r\n" + body).getBytes("UTF-8"))); mp2.setDisposition("attachment"); mp2.setFileName("temp.tmp"); mm.addBodyPart(mp2); String body2 = "\u0014\u00A3\u00E1\u00E2\u00E4"; MimeBodyPart mp3 = new MimeBodyPart( new ByteArrayInputStream(("Content-Transfer-Encoding: 8bit\r\n\r\n" + body2).getBytes("UTF-8"))); mp3.setDisposition("attachment"); mp3.setFileName("winmail.dat"); mm.addBodyPart(mp3); message.setSubject("test"); message.setContent(mm); message.saveChanges(); Mail mail = FakeMail.builder().mimeMessage(message).build(); mailet.service(mail); ByteArrayOutputStream rawMessage = new ByteArrayOutputStream(); mail.getMessage().writeTo(rawMessage, new String[] { "Bcc", "Content-Length", "Message-ID" }); // String res = rawMessage.toString(); @SuppressWarnings("unchecked") Collection<String> c = (Collection<String>) mail .getAttribute(StripAttachment.SAVED_ATTACHMENTS_ATTRIBUTE_KEY); Assert.assertNotNull(c); Assert.assertEquals(1, c.size()); String name = c.iterator().next(); File f = new File("./" + name); try { InputStream is = new FileInputStream(f); String savedFile = toString(is); is.close(); Assert.assertEquals(body, savedFile); } finally { FileUtils.deleteQuietly(f); } }
From source file:org.apache.james.transport.mailets.StripAttachmentTest.java
@Test public void testSimpleAttachment3() throws MessagingException, IOException { Mailet mailet = initMailet();//from ww w. j a va2 s. c o m // System.setProperty("mail.mime.decodefilename", "true"); MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); MimeMultipart mm = new MimeMultipart(); MimeBodyPart mp = new MimeBodyPart(); mp.setText("simple text"); mm.addBodyPart(mp); String body = "\u0023\u00A4\u00E3\u00E0\u00E9"; MimeBodyPart mp2 = new MimeBodyPart( new ByteArrayInputStream(("Content-Transfer-Encoding: 8bit\r\n\r\n" + body).getBytes("UTF-8"))); mp2.setDisposition("attachment"); mp2.setFileName("=?iso-8859-15?Q?=E9_++++Pubblicit=E0_=E9_vietata____Milano9052.tmp?="); mm.addBodyPart(mp2); String body2 = "\u0014\u00A3\u00E1\u00E2\u00E4"; MimeBodyPart mp3 = new MimeBodyPart( new ByteArrayInputStream(("Content-Transfer-Encoding: 8bit\r\n\r\n" + body2).getBytes("UTF-8"))); mp3.setDisposition("attachment"); mp3.setFileName("temp.zip"); mm.addBodyPart(mp3); message.setSubject("test"); message.setContent(mm); message.saveChanges(); // message.writeTo(System.out); // System.out.println("--------------------------\n\n\n"); Mail mail = FakeMail.builder().mimeMessage(message).build(); mailet.service(mail); ByteArrayOutputStream rawMessage = new ByteArrayOutputStream(); mail.getMessage().writeTo(rawMessage, new String[] { "Bcc", "Content-Length", "Message-ID" }); // String res = rawMessage.toString(); @SuppressWarnings("unchecked") Collection<String> c = (Collection<String>) mail .getAttribute(StripAttachment.SAVED_ATTACHMENTS_ATTRIBUTE_KEY); Assert.assertNotNull(c); Assert.assertEquals(1, c.size()); String name = c.iterator().next(); // System.out.println("--------------------------\n\n\n"); // System.out.println(name); Assert.assertTrue(name.startsWith("e_Pubblicita_e_vietata_Milano9052")); File f = new File("./" + name); try { InputStream is = new FileInputStream(f); String savedFile = toString(is); is.close(); Assert.assertEquals(body, savedFile); } finally { FileUtils.deleteQuietly(f); } }
From source file:org.apache.james.transport.mailets.StripAttachmentTest.java
@Test public void testToAndFromAttributes() throws MessagingException, IOException { Mailet strip = new StripAttachment(); FakeMailetConfig mci = new FakeMailetConfig("Test", FakeMailContext.defaultContext()); mci.setProperty("attribute", "my.attribute"); mci.setProperty("remove", "all"); mci.setProperty("notpattern", ".*\\.tmp.*"); strip.init(mci);/*from www.j ava 2 s.c o m*/ Mailet recover = new RecoverAttachment(); FakeMailetConfig mci2 = new FakeMailetConfig("Test", FakeMailContext.defaultContext()); mci2.setProperty("attribute", "my.attribute"); recover.init(mci2); Mailet onlyText = new OnlyText(); onlyText.init(new FakeMailetConfig("Test", FakeMailContext.defaultContext())); MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); MimeMultipart mm = new MimeMultipart(); MimeBodyPart mp = new MimeBodyPart(); mp.setText("simple text"); mm.addBodyPart(mp); String body = "\u0023\u00A4\u00E3\u00E0\u00E9"; MimeBodyPart mp2 = new MimeBodyPart(new ByteArrayInputStream( ("Content-Transfer-Encoding: 8bit\r\nContent-Type: application/octet-stream; charset=utf-8\r\n\r\n" + body).getBytes("UTF-8"))); mp2.setDisposition("attachment"); mp2.setFileName("=?iso-8859-15?Q?=E9_++++Pubblicit=E0_=E9_vietata____Milano9052.tmp?="); mm.addBodyPart(mp2); String body2 = "\u0014\u00A3\u00E1\u00E2\u00E4"; MimeBodyPart mp3 = new MimeBodyPart(new ByteArrayInputStream( ("Content-Transfer-Encoding: 8bit\r\nContent-Type: application/octet-stream; charset=utf-8\r\n\r\n" + body2).getBytes("UTF-8"))); mp3.setDisposition("attachment"); mp3.setFileName("temp.zip"); mm.addBodyPart(mp3); message.setSubject("test"); message.setContent(mm); message.saveChanges(); Mail mail = FakeMail.builder().mimeMessage(message).build(); Assert.assertTrue(mail.getMessage().getContent() instanceof MimeMultipart); Assert.assertEquals(3, ((MimeMultipart) mail.getMessage().getContent()).getCount()); strip.service(mail); Assert.assertTrue(mail.getMessage().getContent() instanceof MimeMultipart); Assert.assertEquals(1, ((MimeMultipart) mail.getMessage().getContent()).getCount()); onlyText.service(mail); Assert.assertFalse(mail.getMessage().getContent() instanceof MimeMultipart); Assert.assertEquals("simple text", mail.getMessage().getContent()); // prova per caricare il mime message da input stream che altrimenti // javamail si comporta differentemente. String mimeSource = "Message-ID: <26194423.21197328775426.JavaMail.bago@bagovista>\r\nSubject: test\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=us-ascii\r\nContent-Transfer-Encoding: 7bit\r\n\r\nsimple text"; MimeMessage mmNew = new MimeMessage(Session.getDefaultInstance(new Properties()), new ByteArrayInputStream(mimeSource.getBytes("UTF-8"))); mmNew.writeTo(System.out); mail.setMessage(mmNew); recover.service(mail); Assert.assertTrue(mail.getMessage().getContent() instanceof MimeMultipart); Assert.assertEquals(2, ((MimeMultipart) mail.getMessage().getContent()).getCount()); Object actual = ((MimeMultipart) mail.getMessage().getContent()).getBodyPart(1).getContent(); if (actual instanceof ByteArrayInputStream) { Assert.assertEquals(body2, toString((ByteArrayInputStream) actual)); } else { Assert.assertEquals(body2, actual); } }