use of javax.mail.internet.MimeMultipart in project spring-framework by spring-projects.
the class MimeMessageHelper method createMimeMultiparts.
/**
* Determine the MimeMultipart objects to use, which will be used
* to store attachments on the one hand and text(s) and inline elements
* on the other hand.
* <p>Texts and inline elements can either be stored in the root element
* itself (MULTIPART_MODE_MIXED, MULTIPART_MODE_RELATED) or in a nested element
* rather than the root element directly (MULTIPART_MODE_MIXED_RELATED).
* <p>By default, the root MimeMultipart element will be of type "mixed"
* (MULTIPART_MODE_MIXED) or "related" (MULTIPART_MODE_RELATED).
* The main multipart element will either be added as nested element of
* type "related" (MULTIPART_MODE_MIXED_RELATED) or be identical to the root
* element itself (MULTIPART_MODE_MIXED, MULTIPART_MODE_RELATED).
* @param mimeMessage the MimeMessage object to add the root MimeMultipart
* object to
* @param multipartMode the multipart mode, as passed into the constructor
* (MIXED, RELATED, MIXED_RELATED, or NO)
* @throws MessagingException if multipart creation failed
* @see #setMimeMultiparts
* @see #MULTIPART_MODE_NO
* @see #MULTIPART_MODE_MIXED
* @see #MULTIPART_MODE_RELATED
* @see #MULTIPART_MODE_MIXED_RELATED
*/
protected void createMimeMultiparts(MimeMessage mimeMessage, int multipartMode) throws MessagingException {
switch(multipartMode) {
case MULTIPART_MODE_NO:
setMimeMultiparts(null, null);
break;
case MULTIPART_MODE_MIXED:
MimeMultipart mixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED);
mimeMessage.setContent(mixedMultipart);
setMimeMultiparts(mixedMultipart, mixedMultipart);
break;
case MULTIPART_MODE_RELATED:
MimeMultipart relatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
mimeMessage.setContent(relatedMultipart);
setMimeMultiparts(relatedMultipart, relatedMultipart);
break;
case MULTIPART_MODE_MIXED_RELATED:
MimeMultipart rootMixedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_MIXED);
mimeMessage.setContent(rootMixedMultipart);
MimeMultipart nestedRelatedMultipart = new MimeMultipart(MULTIPART_SUBTYPE_RELATED);
MimeBodyPart relatedBodyPart = new MimeBodyPart();
relatedBodyPart.setContent(nestedRelatedMultipart);
rootMixedMultipart.addBodyPart(relatedBodyPart);
setMimeMultiparts(rootMixedMultipart, nestedRelatedMultipart);
break;
default:
throw new IllegalArgumentException("Only multipart modes MIXED_RELATED, RELATED and NO supported");
}
}
use of javax.mail.internet.MimeMultipart in project spring-framework by spring-projects.
the class MimeMessageHelper method getMainPart.
private MimeBodyPart getMainPart() throws MessagingException {
MimeMultipart mimeMultipart = getMimeMultipart();
MimeBodyPart bodyPart = null;
for (int i = 0; i < mimeMultipart.getCount(); i++) {
BodyPart bp = mimeMultipart.getBodyPart(i);
if (bp.getFileName() == null) {
bodyPart = (MimeBodyPart) bp;
}
}
if (bodyPart == null) {
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeMultipart.addBodyPart(mimeBodyPart);
bodyPart = mimeBodyPart;
}
return bodyPart;
}
use of javax.mail.internet.MimeMultipart in project rest.li by linkedin.
the class TestQueryTunnel method testNestedMultiPartBody.
@Test
public void testNestedMultiPartBody() throws Exception {
// Construct a request with multi-part entity as a body and make sure it can be tunneled
// The entity will be nested - the original multi-part body will become the 2nd part of the tunneled
// body with the query passed as the form-encoded url
final MimeMultipart multi = new MimeMultipart("mixed");
MimeBodyPart partOne = new MimeBodyPart();
partOne.setContent(new String("{\"name\":\"value\"}").getBytes(), "application/json");
partOne.setHeader("Content-Type", "application/json");
// Encode query params as form-urlencoded
MimeBodyPart partTwo = new MimeBodyPart();
partTwo.setContent("x=y&z=w&q=10", "application/x-www-form-urlencoded");
partTwo.setHeader("Content-Type", "application /x-www-form-urlencoded");
multi.addBodyPart(partTwo);
multi.addBodyPart(partOne);
ByteArrayOutputStream os = new ByteArrayOutputStream();
multi.writeTo(os);
// Create request with multi-part body and query args
final RestRequest request = new RestRequestBuilder(new URI("http://localhost:7279?args=xyz")).setMethod("PUT").setHeader("Content-Type", multi.getContentType()).setEntity(ByteString.copy(os.toByteArray())).build();
// Encode and verify
RestRequest encoded = encode(request, 0);
Assert.assertEquals(encoded.getMethod(), "POST");
Assert.assertEquals(encoded.getURI().toString(), "http://localhost:7279");
Assert.assertTrue(encoded.getEntity().length() > 0);
Assert.assertTrue(encoded.getHeader("Content-Type").startsWith("multipart/mixed"));
Assert.assertEquals(encoded.getHeader("Content-Length"), Integer.toString(encoded.getEntity().length()));
// Decode and make sure we have the original request back
RequestContext requestContext = new RequestContext();
RestRequest decoded = decode(encoded, requestContext);
Assert.assertEquals(decoded.getURI().toString(), "http://localhost:7279?args=xyz");
Assert.assertEquals(decoded.getMethod(), "PUT");
Assert.assertEquals(decoded.getEntity(), request.getEntity());
Assert.assertTrue(encoded.getHeader("Content-Type").startsWith("multipart/mixed"));
Assert.assertTrue((Boolean) requestContext.getLocalAttr(R2Constants.IS_QUERY_TUNNELED));
Assert.assertEquals(decoded.getHeader("Content-Length"), Integer.toString(request.getEntity().length()));
}
use of javax.mail.internet.MimeMultipart in project google-app-engine-jappstart by taylorleese.
the class MailService method sendActivationEmail.
/**
* Sends the activation e-mail to the given user.
*
* @param user the user
* @param locale the locale
* @throws MessagingException messaging exception
*/
public final void sendActivationEmail(final UserAccount user, final String locale) throws MessagingException {
final Properties props = new Properties();
final Session session = Session.getDefaultInstance(props, null);
final Message message = new MimeMessage(session);
final Multipart multipart = new MimeMultipart();
final MimeBodyPart htmlPart = new MimeBodyPart();
final MimeBodyPart textPart = new MimeBodyPart();
message.setFrom(new InternetAddress(getFromAddress()));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
message.setSubject(messageSource.getMessage("mail.subject", null, new Locale(locale)));
textPart.setContent(messageSource.getMessage("mail.body.txt", new Object[] { getHostname(), user.getActivationKey() }, new Locale(locale)), "text/plain");
htmlPart.setContent(messageSource.getMessage("mail.body.html", new Object[] { getHostname(), user.getActivationKey() }, new Locale(locale)), "text/html");
multipart.addBodyPart(textPart);
multipart.addBodyPart(htmlPart);
message.setContent(multipart);
Transport.send(message);
}
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"));
}
Aggregations