Search in sources :

Example 51 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project nhin-d by DirectProject.

the class NotificationTest method testWarning_AssertWarning.

public void testWarning_AssertWarning() throws Exception {
    Notification noti = new Notification(NotificationType.Processed);
    MimeMultipart mm = noti.getAsMultipart();
    assertNotNull(mm);
    assertEquals(2, mm.getCount());
    BodyPart part = mm.getBodyPart(1);
    assertTrue(part.getContentType().startsWith("message/disposition-notification"));
    InternetHeaders headers = noti.getNotificationFieldsAsHeaders();
    assertNull(headers.getHeader(MDNStandard.Headers.FinalRecipient));
    // set a new gateway
    noti.setWarning("junit warning");
    mm = noti.getAsMultipart();
    assertNotNull(mm);
    assertEquals(2, mm.getCount());
    part = mm.getBodyPart(1);
    assertTrue(part.getContentType().startsWith("message/disposition-notification"));
    headers = noti.getNotificationFieldsAsHeaders();
    assertNotNull(headers.getHeader(MDNStandard.Headers.Warning));
    assertEquals("junit warning", headers.getHeader(MDNStandard.Headers.Warning, ","));
}
Also used : BodyPart(javax.mail.BodyPart) InternetHeaders(javax.mail.internet.InternetHeaders) MimeMultipart(javax.mail.internet.MimeMultipart) DispositionNotification(com.sun.mail.dsn.DispositionNotification)

Example 52 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project nhin-d by DirectProject.

the class NotificationTest method testSetExplanation_AssertExplanation.

public void testSetExplanation_AssertExplanation() throws Exception {
    Notification noti = new Notification(NotificationType.Processed);
    MimeMultipart mm = noti.getAsMultipart();
    assertNotNull(mm);
    assertEquals(2, mm.getCount());
    BodyPart part = mm.getBodyPart(0);
    assertTrue(part.getContentType().startsWith("text/plain"));
    assertEquals("Your message was successfully processed.", part.getContent().toString());
    // set a new explanation
    noti.setExplanation("Testing this explantation");
    mm = noti.getAsMultipart();
    assertNotNull(mm);
    assertEquals(2, mm.getCount());
    part = mm.getBodyPart(0);
    assertTrue(part.getContentType().startsWith("text/plain"));
    assertEquals("Testing this explantation", noti.getExplanation());
    assertEquals(noti.getExplanation(), part.getContent().toString());
    // make sure this didn't change
    part = mm.getBodyPart(1);
    assertTrue(part.getContentType().startsWith("message/disposition-notification"));
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    part.writeTo(outStream);
    String content = new String(outStream.toByteArray());
    assertTrue(content.contains("automatic-action/MDN-sent-automatically;processed"));
}
Also used : BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) DispositionNotification(com.sun.mail.dsn.DispositionNotification)

Example 53 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project jdk8u_jdk by JetBrains.

the class MailTest method sendMail.

void sendMail() {
    try {
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        Session session = Session.getInstance(props);
        session.setDebug(true);
        // Define message
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipients(Message.RecipientType.TO, to);
        message.setSubject("this is a multipart test");
        Multipart multipart = new MimeMultipart();
        BodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setText("please send also this Content\n ciao!");
        multipart.addBodyPart(messageBodyPart1);
        BodyPart messageBodyPart2 = new MimeBodyPart();
        messageBodyPart2.setContent("<b>please</b> send also this Content <br>ciao!", "text/html; charset=UTF-8");
        multipart.addBodyPart(messageBodyPart2);
        message.setContent(multipart);
        /*
                Transport tr = session.getTransport("smtp");
                tr.connect(host,user, password);
                tr.sendMessage(message,InternetAddress.parse(to));
                tr.close();
            */
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        message.writeTo(baos);
        String output = baos.toString();
        System.out.println("output = " + output);
        if (output.contains("also this Content")) {
            System.out.println("Test PASSED.");
        } else {
            System.out.println("Test FAILED, missing content.");
            throw new IllegalStateException("Test FAILED, missing content.");
        }
    } catch (MessagingException ignored) {
    } catch (IOException ignored) {
    }
}
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) MessagingException(javax.mail.MessagingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session)

Example 54 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project nhin-d by DirectProject.

the class NotificationTest method testFailedNotification.

public void testFailedNotification() throws Exception {
    Notification noti = new Notification(NotificationType.Failed);
    System.out.println(noti);
    MimeMultipart mm = noti.getAsMultipart();
    assertNotNull(mm);
    assertEquals(2, mm.getCount());
    BodyPart part = mm.getBodyPart(0);
    assertTrue(part.getContentType().startsWith("text/plain"));
    assertEquals(Notification.DefaultExplanationFailed, part.getContent().toString());
    part = mm.getBodyPart(1);
    assertTrue(part.getContentType().startsWith("message/disposition-notification"));
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    part.writeTo(outStream);
    String content = new String(outStream.toByteArray());
    assertTrue(content.contains("automatic-action/MDN-sent-automatically;failed"));
}
Also used : BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) DispositionNotification(com.sun.mail.dsn.DispositionNotification)

Example 55 with MimeMultipart

use of javax.mail.internet.MimeMultipart in project voldemort by voldemort.

the class CoordinatorRestAPITest method doGet.

private TestVersionedValue doGet(String key, Map<String, Object> options) {
    HttpURLConnection conn = null;
    String response = null;
    TestVersionedValue responseObj = null;
    int expectedResponseCode = 200;
    try {
        // Create the right URL and Http connection
        String base64Key = new String(Base64.encodeBase64(key.getBytes()));
        URL url = new URL(this.coordinatorURL + "/" + STORE_NAME + "/" + base64Key);
        conn = (HttpURLConnection) url.openConnection();
        // Set the right headers
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.setRequestProperty(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, "1000");
        conn.setRequestProperty(RestMessageHeaders.X_VOLD_REQUEST_ORIGIN_TIME_MS, Long.toString(System.currentTimeMillis()));
        // options
        if (options != null) {
            if (options.get("timeout") != null && options.get("timeout") instanceof String) {
                conn.setRequestProperty(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, (String) options.get("timeout"));
            }
            if (options.get("responseCode") != null && options.get("responseCode") instanceof Integer) {
                expectedResponseCode = (Integer) options.get("responseCode");
            }
        }
        // Check for the right response code
        if (conn.getResponseCode() != expectedResponseCode) {
            System.err.println("Illegal response during GET : " + conn.getResponseMessage());
            fail("Incorrect response received for a HTTP GET request :" + conn.getResponseCode());
        }
        if (conn.getResponseCode() == 404 || conn.getResponseCode() == 408) {
            return null;
        }
        // Buffer the result into a string
        ByteArrayDataSource ds = new ByteArrayDataSource(conn.getInputStream(), "multipart/mixed");
        MimeMultipart mp = new MimeMultipart(ds);
        assertEquals("The number of body parts expected is not 1", 1, mp.getCount());
        MimeBodyPart part = (MimeBodyPart) mp.getBodyPart(0);
        VectorClock vc = RestUtils.deserializeVectorClock(part.getHeader(RestMessageHeaders.X_VOLD_VECTOR_CLOCK)[0]);
        int contentLength = Integer.parseInt(part.getHeader(RestMessageHeaders.CONTENT_LENGTH)[0]);
        byte[] bodyPartBytes = new byte[contentLength];
        part.getInputStream().read(bodyPartBytes);
        response = new String(bodyPartBytes);
        responseObj = new TestVersionedValue(response, vc);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Error in sending the REST request");
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
    return responseObj;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) MimeMultipart(javax.mail.internet.MimeMultipart) VectorClock(voldemort.versioning.VectorClock) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) URL(java.net.URL)

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