List of usage examples for javax.mail.internet MimeMessage saveChanges
@Override public void saveChanges() throws MessagingException
From source file:ch.entwine.weblounge.kernel.mail.SmtpService.java
/** * Method to send a test message.//from w w w . ja va 2 s .co m * * @throws MessagingException * if sending the message failed */ private void sendTestMessage(String recipient) throws MessagingException { MimeMessage message = createMessage(); message.addRecipient(RecipientType.TO, new InternetAddress(recipient)); message.setSubject("Test from Weblounge"); message.setText("Hello world"); message.saveChanges(); send(message); }
From source file:com.zimbra.cs.mailbox.MailboxTestUtil.java
public static ParsedMessage generateMessageWithAttachment(String subject) throws Exception { MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession()); mm.setHeader("From", "Vera Oliphant <oli@example.com>"); mm.setHeader("To", "Jimmy Dean <jdean@example.com>"); mm.setHeader("Subject", subject); mm.setText("Good as gold"); MimeMultipart multi = new ZMimeMultipart("mixed"); ContentDisposition cdisp = new ContentDisposition(Part.ATTACHMENT); cdisp.setParameter("filename", "fun.txt"); ZMimeBodyPart bp = new ZMimeBodyPart(); // MimeBodyPart.setDataHandler() invalidates Content-Type and CTE if there is any, so make sure // it gets called before setting Content-Type and CTE headers. try {//from w w w . j a v a 2 s . c o m bp.setDataHandler(new DataHandler(new ByteArrayDataSource("Feeling attached.", "text/plain"))); } catch (IOException e) { throw new MessagingException("could not generate mime part content", e); } bp.addHeader("Content-Disposition", cdisp.toString()); bp.addHeader("Content-Type", "text/plain"); bp.addHeader("Content-Transfer-Encoding", MimeConstants.ET_8BIT); multi.addBodyPart(bp); mm.setContent(multi); mm.saveChanges(); return new ParsedMessage(mm, false); }
From source file:easyproject.utils.SendMail.java
@Override public void run() { Properties props = new Properties(); props.put("mail.debug", "true"); props.put("mail.smtp.auth", true); props.put("mail.smtp.starttls.enable", true); props.put("mail.smtp.host", servidorSMTP); props.put("mail.smtp.port", puerto); Session session = Session.getInstance(props, null); try {//from www . j a v a2 s . c o m MimeMessage message1 = new MimeMessage(session); message1.addRecipient(Message.RecipientType.TO, new InternetAddress(destino)); message1.setSubject(asunto); message1.setSentDate(new Date()); message1.setContent(mensaje, "text/html; charset=utf-8"); Transport tr = session.getTransport("smtp"); tr.connect(servidorSMTP, usuario, password); message1.saveChanges(); tr.sendMessage(message1, message1.getAllRecipients()); tr.close(); } catch (MessagingException e) { e.printStackTrace(); } }
From source file:mitm.common.mail.MailUtilsTest.java
@Test public void testConvert8BitTo7BitSimpleText() throws IOException, MessagingException { MimeMessage message = loadMessage("8bit-text.eml"); assertEquals("8bIt", message.getEncoding()); assertTrue(MailUtils.convertTo7Bit(message)); message.saveChanges(); assertEquals("quoted-printable", message.getEncoding()); MailUtils.validateMessage(message);//from w w w .j a v a2 s . co m File file = new File("test/tmp/testConvert8BitTo7BitSimpleText.eml"); MailUtils.writeMessage(message, file); assertEquals("from 8bit to quoted-printable by Djigzo", message.getHeader("X-MIME-Autoconverted", ",")); }
From source file:mitm.application.djigzo.james.mailets.SMIMEEncrypt.java
@Override public void serviceMail(Mail mail) { try {/*from w w w . j av a 2 s .c om*/ DjigzoMailAttributes mailAttributes = new DjigzoMailAttributesImpl(mail); Certificates certificateStore = mailAttributes.getCertificates(); if (certificateStore != null) { Collection<X509Certificate> certificates = certificateStore.getCertificates(); if (certificates.size() > 0) { MimeMessage message = mail.getMessage(); if (message != null) { SMIMEBuilder sMIMEBuilder = new SMIMEBuilderImpl(message, getProtectedHeaders(mail)); sMIMEBuilder.setUseDeprecatedContentTypes(useDeprecatedContentTypes); for (X509Certificate certificate : certificates) { sMIMEBuilder.addRecipient(certificate, getRecipientMode()); } SMIMEEncryptionAlgorithm localAlgorithm = getEncryptionAlgorithm(mail); int localKeySize = (keySize != null ? keySize : localAlgorithm.defaultKeySize()); getLogger().debug("Encrypting the message. Encryption algorithm: {}, key size: {}", localAlgorithm, localKeySize); sMIMEBuilder.encrypt(localAlgorithm, localKeySize); MimeMessage encrypted = sMIMEBuilder.buildMessage(); if (encrypted != null) { encrypted.saveChanges(); /* * A new MimeMessage instance will be created. This makes ure that the * MimeMessage can be written by James (using writeTo()). The message-ID * of the source message will be retained if told to do so */ encrypted = retainMessageID ? new MimeMessageWithID(encrypted, message.getMessageID()) : new MimeMessage(encrypted); mail.setMessage(encrypted); } } else { getLogger().warn("Message is null."); } } else { getLogger().warn("Certificate collection is empty."); } } else { getLogger().warn("Certificates attribute not found."); } } catch (SMIMEBuilderException e) { getLogger().error("Error encrypting the message.", e); } catch (MessagingException e) { getLogger().error("Error reading the message.", e); } catch (IOException e) { getLogger().error("IOException.", e); } }
From source file:mitm.common.mail.MailUtilsTest.java
@Test public void testConvert8BitTo7BitBinary() throws IOException, MessagingException { MimeMessage message = loadMessage("8bit-binary.eml"); assertEquals("8bit", message.getEncoding()); assertTrue(MailUtils.convertTo7Bit(message)); message.saveChanges(); assertEquals("base64", message.getEncoding()); MailUtils.validateMessage(message);/* w w w.j a v a2s . co m*/ File file = new File("test/tmp/testConvert8BitTo7BitBinary.eml"); MailUtils.writeMessage(message, file); String mime = FileUtils.readFileToString(file); assertTrue(mime.contains("X-MIME-Autoconverted: from 8bit to base64 by Djigzo")); assertTrue(mime.contains("VGhpcyBpcyBhIHRlc3Qgd2l0aCB1bmxhdXRzOiBTY2jDtm4KCg==")); }
From source file:mitm.common.mail.MailUtilsTest.java
@Test public void testValidateMessageCorruptBase64() throws IOException, MessagingException { MimeMessage message = loadMessage("corrupt-base64.eml"); /*// ww w . j a v a 2s. c o m * Saving the mssage used to fail with Javamail <= 1.4.3 but no longer fails with 1.4.4 */ message.saveChanges(); MailUtils.validateMessage(message); try { message.getContent(); fail(); } catch (DecodingException e) { // expected } }
From source file:mitm.common.mail.MailUtilsTest.java
@Test public void testValidateMessageShouldFail() throws Exception { MimeMessage message = new MimeMessage(MailSession.getDefaultSession()); MimeBodyPart emptyPart = new MimeBodyPart(); message.setContent(emptyPart, "text/plain"); message.saveChanges(); try {//from www. ja v a 2 s.co m MailUtils.validateMessage(message); fail(); } catch (IOException e) { // expected. The message should be corrupt } }
From source file:mitm.common.mail.MailUtilsTest.java
@Test public void testConvertTo7BitNoConversion() throws IOException, MessagingException { MimeMessage message = loadMessage("multiple-attachments.eml"); String messageID = message.getMessageID(); assertFalse(MailUtils.convertTo7Bit(message)); message.saveChanges(); MailUtils.validateMessage(message);// w ww . j a va 2 s . co m // saveChanges changes the message ID so we cannot compare the result. // we need to set the original message id MimeMessageWithID mime = new MimeMessageWithID(message, messageID); mime.saveChanges(); File file = new File("test/tmp/testConvertTo7BitNoConversion.eml"); MailUtils.writeMessage(mime, file); // we need to correct CR/LF pairs because org only uses LF String result = FileUtils.readFileToString(file); result = StringUtils.replace(result, "\r\n", "\n").trim(); String exp = FileUtils.readFileToString(new File(testBase, "multiple-attachments.eml")); exp = StringUtils.replace(exp, "\r\n", "\n").trim(); assertEquals(exp, result); }
From source file:com.zimbra.cs.mime.MimeTest.java
@Test public void fixBase64LineWrappingAttachments() throws Exception { InputStream is = getClass().getResourceAsStream("bug95114.txt"); InputStream expectedIs = getClass().getResourceAsStream("bug95114_expected.txt"); String expected = IOUtils.toString(expectedIs); MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), is); mm.saveChanges(); Mime.fixBase64MimePartLineFolding(mm); mm.saveChanges();// ww w . j a va 2 s . c om String actual = IOUtils.toString(mm.getInputStream()); Assert.assertEquals("Content altered.", expected, actual); }