Search in sources :

Example 86 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project webservices-axiom by apache.

the class AttachmentsTest method testGetRootPartContentID.

private void testGetRootPartContentID(String contentTypeStartParam, String contentId) throws Exception {
    MimeMessage message = new MimeMessage((Session) null);
    MimeMultipart mp = new MimeMultipart("related");
    MimeBodyPart rootPart = new MimeBodyPart();
    rootPart.setText("<root/>", "utf-8", "xml");
    rootPart.addHeader("Content-Transfer-Encoding", "binary");
    rootPart.addHeader("Content-ID", "<" + contentId + ">");
    mp.addBodyPart(rootPart);
    message.setContent(mp);
    message.saveChanges();
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    mp.writeTo(out);
    out.close();
    Attachments attachments = new Attachments(blob.getInputStream(), new ContentType(message.getContentType()).toBuilder().setParameter("start", contentTypeStartParam).build().toString());
    assertEquals("Did not obtain correct content ID", contentId, attachments.getRootPartContentID());
}
Also used : ContentType(org.apache.axiom.mime.ContentType) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MemoryBlob(org.apache.axiom.blob.MemoryBlob) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) PipedOutputStream(java.io.PipedOutputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 87 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project webservices-axiom by apache.

the class TestCreateSOAPModelBuilderMTOMContentTypeMismatch method runTest.

@Override
protected void runTest() throws Throwable {
    final SOAPSample sample = SOAPSampleSet.NO_HEADER.getMessage(spec);
    // Generate an MTOM message with the wrong content type
    MimeMessage message = new MimeMessage((Session) null);
    MimeMultipart mp = new MimeMultipart("related");
    MimeBodyPart bp = new MimeBodyPart();
    String contentID = "<" + UIDGenerator.generateContentId() + ">";
    bp.setDataHandler(new DataHandler(new DataSource() {

        @Override
        public String getContentType() {
            return "application/xop+xml; charset=\"" + sample.getEncoding() + "\"; type=\"" + spec.getAltSpec().getContentType() + "\"";
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return sample.getInputStream();
        }

        @Override
        public String getName() {
            return null;
        }

        @Override
        public OutputStream getOutputStream() {
            throw new UnsupportedOperationException();
        }
    }));
    bp.addHeader("Content-Transfer-Encoding", "binary");
    bp.addHeader("Content-ID", contentID);
    mp.addBodyPart(bp);
    message.setContent(mp);
    message.saveChanges();
    ContentType contentType = new ContentType(message.getContentType()).toBuilder().setParameter("type", "application/xop+xml").setParameter("start", contentID).setParameter("start-info", spec.getAltSpec().getContentType()).build();
    MemoryBlob blob = Blobs.createMemoryBlob();
    OutputStream out = blob.getOutputStream();
    mp.writeTo(out);
    out.close();
    // Now attempt to create an Axiom builder
    try {
        OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, MultipartBody.builder().setInputStream(blob.getInputStream()).setContentType(contentType).build());
        fail("Expected SOAPProcessingException");
    } catch (SOAPProcessingException ex) {
    // Expected
    }
}
Also used : ContentType(org.apache.axiom.mime.ContentType) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MemoryBlob(org.apache.axiom.blob.MemoryBlob) OutputStream(java.io.OutputStream) SOAPProcessingException(org.apache.axiom.soap.SOAPProcessingException) DataHandler(javax.activation.DataHandler) SOAPSample(org.apache.axiom.ts.soap.SOAPSample) MimeBodyPart(javax.mail.internet.MimeBodyPart) DataSource(javax.activation.DataSource)

Example 88 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project Gargoyle by callakrsos.

the class EmailAttachmentSender method sendEmailWithAttachments.

public static void sendEmailWithAttachments(String host, String port, final String userName, final String password, String toAddress, String subject, String message, String[] attachFiles) throws AddressException, MessagingException {
    // sets SMTP server properties
    Properties properties = new Properties();
    properties.put("mail.smtp.host", host);
    properties.put("mail.smtp.port", port);
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.user", userName);
    properties.put("mail.password", password);
    // creates a new session with an authenticator
    Authenticator auth = new Authenticator() {

        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
        }
    };
    Session session = Session.getInstance(properties, auth);
    // creates a new e-mail message
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(userName));
    InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    msg.setSubject(subject);
    msg.setSentDate(new Date());
    // creates message part
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setContent(message, "text/html");
    // creates multi-part
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    // adds attachments
    if (attachFiles != null && attachFiles.length > 0) {
        for (String filePath : attachFiles) {
            MimeBodyPart attachPart = new MimeBodyPart();
            try {
                attachPart.attachFile(filePath);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            multipart.addBodyPart(attachPart);
        }
    }
    // sets the multi-part as e-mail's content
    msg.setContent(multipart);
    // sends the e-mail
    Transport.send(msg);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) IOException(java.io.IOException) Properties(java.util.Properties) Date(java.util.Date) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication) Session(javax.mail.Session)

Example 89 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project opennms by OpenNMS.

the class JavaMailAckReaderIT method workingWithMultiPartMessages.

/**
 * tests the ability to create acknowledgments from an email for a multi-part text.  This test
 * creates a message from scratch rather than reading from an inbox.  This message creation
 * may not actually represent what comes from a mail server.
 */
@Test
public void workingWithMultiPartMessages() throws JavaMailerException, MessagingException {
    List<Message> msgs = new ArrayList<>();
    Properties props = new Properties();
    Message msg = new MimeMessage(Session.getDefaultInstance(props));
    Address[] addrs = new Address[1];
    addrs[0] = new InternetAddress("david@opennms.org");
    msg.addFrom(addrs);
    msg.addRecipient(RecipientType.TO, new InternetAddress("david@opennms.org"));
    msg.setSubject("Re: Notice #1234 JavaMailReaderImplTest Test Message");
    Multipart mpContent = new MimeMultipart();
    BodyPart textBp = new MimeBodyPart();
    BodyPart htmlBp = new MimeBodyPart();
    textBp.setText("ack");
    htmlBp.setContent("<html>\n" + " <head>\n" + "  <title>\n" + "   Acknowledge\n" + "  </title>\n" + " </head>\n" + " <body>\n" + "  <h1>\n" + "   ack\n" + "  </h1>\n" + " </body>\n" + "</html>", "text/html");
    mpContent.addBodyPart(textBp);
    mpContent.addBodyPart(htmlBp);
    msg.setContent(mpContent);
    msgs.add(msg);
    List<OnmsAcknowledgment> acks = m_processor.createAcks(msgs);
    Assert.assertEquals(1, acks.size());
    Assert.assertEquals(AckType.NOTIFICATION, acks.get(0).getAckType());
    Assert.assertEquals("david@opennms.org", acks.get(0).getAckUser());
    Assert.assertEquals(AckAction.ACKNOWLEDGE, acks.get(0).getAckAction());
    Assert.assertEquals(new Integer(1234), acks.get(0).getRefId());
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) OnmsAcknowledgment(org.opennms.netmgt.model.OnmsAcknowledgment) Message(javax.mail.Message) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) MimeMessage(javax.mail.internet.MimeMessage) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) ArrayList(java.util.ArrayList) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 90 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project hub-alert by blackducksoftware.

the class MimeMultipartBuilder method buildEmailBodyPart.

private MimeBodyPart buildEmailBodyPart() throws MessagingException {
    final MimeMultipart emailContent = new MimeMultipart("alternative");
    // add from low fidelity to high fidelity
    if (StringUtils.isNotBlank(text)) {
        final MimeBodyPart textBodyPart = buildTextBodyPart();
        emailContent.addBodyPart(textBodyPart);
    }
    if (StringUtils.isNotBlank(html)) {
        final MimeBodyPart htmlBodyPart = buildHtmlBodyPart();
        emailContent.addBodyPart(htmlBodyPart);
    }
    final MimeBodyPart emailBodyPart = new MimeBodyPart();
    emailBodyPart.setContent(emailContent);
    return emailBodyPart;
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

MimeMultipart (javax.mail.internet.MimeMultipart)213 MimeBodyPart (javax.mail.internet.MimeBodyPart)134 MimeMessage (javax.mail.internet.MimeMessage)85 MessagingException (javax.mail.MessagingException)69 ByteArrayOutputStream (java.io.ByteArrayOutputStream)55 BodyPart (javax.mail.BodyPart)55 IOException (java.io.IOException)47 ByteString (com.linkedin.data.ByteString)37 DataHandler (javax.activation.DataHandler)33 InternetAddress (javax.mail.internet.InternetAddress)33 Test (org.testng.annotations.Test)31 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)27 HashMap (java.util.HashMap)27 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)26 Multipart (javax.mail.Multipart)26 Properties (java.util.Properties)25 Session (javax.mail.Session)24 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)24 InputStream (java.io.InputStream)23 Date (java.util.Date)21