List of usage examples for javax.mail.internet MimeMultipart MimeMultipart
public MimeMultipart(DataSource ds) throws MessagingException
From source file:com.meltmedia.cadmium.email.TestingMessageTransformer.java
public MimeMessage transform(MimeMessage message) throws MessagingException { MimeBodyPart sentToBodyPart = newSentToBodyPart(message); MimeBodyPart originalBodyPart = newOriginalBodyPart(message); // create a new multipart content for this message. MimeMultipart multipart = new MimeMultipart(EmailUtil.SUBTYPE_MIXED); // add the parts to the body. multipart.addBodyPart(originalBodyPart); multipart.addBodyPart(sentToBodyPart); // get the new values for all of the headers. InternetAddress newFromAddress = newFromAddress(message); InternetAddress[] newToAddresses = newToAddresses(message); InternetAddress[] newCcAddresses = newCcAddresses(message); InternetAddress[] newBccAddresses = newBccAddresses(message); String newSubject = newSubject(message); // update the message. message.setFrom(newFromAddress);// w w w . j ava2s . c om message.setRecipients(Message.RecipientType.TO, newToAddresses); message.setRecipients(Message.RecipientType.CC, newCcAddresses); message.setRecipients(Message.RecipientType.BCC, newBccAddresses); message.setSubject(newSubject); message.setContent(multipart); // save the message. message.saveChanges(); if (getLogProperty()) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); message.writeTo(out); log.info("Email Message Sent:\n{}", out.toString()); } catch (IOException ioe) { throw new MessagingException("Exception thrown while writing message to log.", ioe); } } if (!getSendProperty()) { message = null; } // return the message. return message; }
From source file:fr.hoteia.qalingo.core.util.impl.MimeMessagePreparatorImpl.java
public void prepare(MimeMessage message) throws Exception { // AUTO unsubscribe for Gmail/Hotmail etc : RFC2369 if (StringUtils.isNotEmpty(getUnsubscribeUrlOrEmail())) { message.addHeader("List-Unsubscribe", "<" + getUnsubscribeUrlOrEmail() + ">"); }/*from w w w . j a v a 2s. com*/ if (getFrom() != null) { List<InternetAddress> toAddress = new ArrayList<InternetAddress>(); toAddress.add(new InternetAddress(getFrom(), getFromName())); message.addFrom(toAddress.toArray(new InternetAddress[toAddress.size()])); } if (getTo() != null) { message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(getTo())); } if (getCc() != null) { message.addRecipients(Message.RecipientType.CC, InternetAddress.parse(getCc())); } if (getSubject() != null) { message.setSubject(getSubject()); } MimeMultipart mimeMultipart = new MimeMultipart("alternative");// multipart/mixed or mixed or related or alternative message.setContent(mimeMultipart); if (getPlainTextContent() != null) { BodyPart textBodyPart = new MimeBodyPart(); textBodyPart.setContent(getPlainTextContent(), "text/plain"); mimeMultipart.addBodyPart(textBodyPart); } if (getHtmlContent() != null) { BodyPart htmlBodyPart = new MimeBodyPart(); htmlBodyPart.setContent(getHtmlContent(), "text/html"); mimeMultipart.addBodyPart(htmlBodyPart); } }
From source file:com.formkiq.core.service.notification.ExternalMailSender.java
/** * Send Reset Email.//www . j a v a 2 s . com * @param to {@link String} * @param email {@link String} * @param subject {@link String} * @param text {@link String} * @param html {@link String} * @throws MessagingException MessagingException */ private void sendResetEmail(final String to, final String email, final String subject, final String text, final String html) throws MessagingException { String hostname = this.systemProperties.getSystemHostname(); String resetToken = this.userservice.generateResetToken(to); StringSubstitutor s = new StringSubstitutor( ImmutableMap.of("hostname", hostname, "to", to, "email", email, "resettoken", resetToken)); MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(s.replace(text), "UTF-8"); s.replace(html); MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(s.replace(html), "text/html;charset=UTF-8"); final Multipart mp = new MimeMultipart("alternative"); mp.addBodyPart(textPart); mp.addBodyPart(htmlPart); MimeMessage msg = this.mail.createMimeMessage(); msg.setContent(mp); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); msg.setSubject(subject); this.mail.send(msg); }
From source file:org.apache.axis2.transport.http.MultipartFormDataFormatterTest.java
public void testTransportHeadersOfFileAttachments() throws Exception { String FILE_NAME = "binaryFile.xml"; String FIELD_NAME = "fileData"; String CONTENT_TYPE_VALUE = "text/xml"; String MEDIATE_ELEMENT = "mediate"; String FILE_KEY = "fileAttachment"; String CONTENT_DISPOSITION = "Content-Disposition"; String CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding"; String CONTENT_TYPE = "Content-Type"; String FORM_DATA_ELEMENT = "form-data"; String NAME_ELEMENT = "name"; String FILE_NAME_ELEMENT = "filename"; String CONTENT_TRANSFER_ENCODING_VALUE = "binary"; MultipartFormDataFormatter formatter = new MultipartFormDataFormatter(); File binaryAttachment = getTestResourceFile(FILE_NAME); MessageContext mc = new MessageContext(); DiskFileItem diskFileItem = null;/*from ww w .j a v a 2 s .c o m*/ InputStream input = null; try { if (binaryAttachment.exists() && !binaryAttachment.isDirectory()) { diskFileItem = (DiskFileItem) new DiskFileItemFactory().createItem(FIELD_NAME, CONTENT_TYPE_VALUE, true, binaryAttachment.getName()); input = new FileInputStream(binaryAttachment); OutputStream os = diskFileItem.getOutputStream(); int ret = input.read(); while (ret != -1) { os.write(ret); ret = input.read(); } os.flush(); } } finally { input.close(); } DataSource dataSource = new DiskFileDataSource(diskFileItem); DataHandler dataHandler = new DataHandler(dataSource); SOAPFactory soapFactory = OMAbstractFactory.getSOAP12Factory(); SOAPEnvelope soapEnvelope = soapFactory.getDefaultEnvelope(); SOAPBody body = soapEnvelope.getBody(); OMElement bodyFirstChild = soapFactory.createOMElement(new QName(MEDIATE_ELEMENT), body); OMText binaryNode = soapFactory.createOMText(dataHandler, true); soapFactory.createOMElement(FILE_KEY, null, bodyFirstChild).addChild(binaryNode); mc.setEnvelope(soapEnvelope); OMOutputFormat format = new OMOutputFormat(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); formatter.writeTo(mc, format, baos, true); MimeMultipart mp = new MimeMultipart(new ByteArrayDataSource(baos.toByteArray(), format.getContentType())); BodyPart bp = mp.getBodyPart(0); String contentDispositionValue = FORM_DATA_ELEMENT + "; " + NAME_ELEMENT + "=\"" + FILE_KEY + "\"; " + FILE_NAME_ELEMENT + "=\"" + binaryAttachment.getName() + "\""; String contentTypeValue = bp.getHeader(CONTENT_TYPE)[0].split(";")[0]; assertEquals(contentDispositionValue, bp.getHeader(CONTENT_DISPOSITION)[0]); assertEquals(CONTENT_TYPE_VALUE, contentTypeValue); assertEquals(CONTENT_TRANSFER_ENCODING_VALUE, bp.getHeader(CONTENT_TRANSFER_ENCODING)[0]); }
From source file:org.grouter.common.mail.MailHandler.java
/** * Creates a multipart email with html and plain text email body to fallback to if * email client do not handle html base emails. * * @param mimeMessage/* w ww.j a v a 2 s. c o m*/ * @param dto * @throws MessagingException */ private void createHtmlAndPlainTextBody(MimeMessage mimeMessage, MailDto dto) throws MessagingException { if (StringUtils.isNotEmpty(dto.getHtmlTextBody())) { mimeMessage.setContentID(MailDto.CONTENT_TYPE_MULTIPART_ALTERNATIVE); // Create your text message part BodyPart plainBodyPart = new MimeBodyPart(); plainBodyPart.setContent(dto.getPlainTextBody(), MailDto.CONTENT_TYPE_TEXT_PLAIN); // Create a multi-part to combine the parts Multipart multipart = new MimeMultipart("alternative"); multipart.addBodyPart(plainBodyPart); StringBuilder sb = new StringBuilder(); sb.append(PRE_HTML_HEADER); sb.append(dto.getSubject()).append("\n"); sb.append(POST_HTML_HEADER); sb.append(dto.getHtmlTextBody()); sb.append(END_HTML); BodyPart htmlBodyPart = new MimeBodyPart(); htmlBodyPart.setContent(sb.toString(), MailDto.CONTENT_TYPE_TEXT_HTML); // Add html part to multi part multipart.addBodyPart(htmlBodyPart); mimeMessage.setContent(multipart); } else { mimeMessage.setSubject(dto.getSubject()); mimeMessage.setText(dto.getPlainTextBody()); mimeMessage.setContent(dto.getPlainTextBody(), MailDto.CONTENT_TYPE_TEXT_PLAIN); } }
From source file:org.xwiki.mail.internal.factory.html.HTMLMimeBodyPartFactory.java
@Override public MimeBodyPart create(String content, Map<String, Object> parameters) throws MessagingException { MimeBodyPart resultBodyPart;//from w w w . j av a2s .co m // Separate normal attachment from embedded image attachments List<Attachment> allAttachments = (List<Attachment>) parameters.get("attachments"); Pair<List<Attachment>, List<Attachment>> attachmentPairs = separateAttachments(content, allAttachments); List<Attachment> embeddedImageAttachments = attachmentPairs.getLeft(); List<Attachment> normalAttachments = attachmentPairs.getRight(); // Step 1: Handle the HTML section of the mail. MimeBodyPart htmlBodyPart; if (!embeddedImageAttachments.isEmpty()) { htmlBodyPart = new MimeBodyPart(); htmlBodyPart.setContent(createHTMLMultipart(content, embeddedImageAttachments)); } else { // Create the HTML body part of the email htmlBodyPart = createHTMLBodyPart(content, false); } // Step 2: Handle the optional alternative text String alternativeText = (String) parameters.get("alternate"); if (alternativeText != null) { resultBodyPart = createAlternativePart(htmlBodyPart, this.defaultPartFactory.create(alternativeText, Collections.<String, Object>emptyMap())); } else { // No alternative text, just add the HTML body part to the Multipart resultBodyPart = htmlBodyPart; } // Step 3 Add the normal attachments (if any). Any embedded images have already been handled in the HTML body // part. Note: If there are attachments we need to wrap our body part inside a "mixed" Multipart. if (!normalAttachments.isEmpty()) { MimeMultipart multipart = new MimeMultipart("mixed"); multipart.addBodyPart(resultBodyPart); handleAttachments(multipart, normalAttachments); resultBodyPart = new MimeBodyPart(); resultBodyPart.setContent(multipart); } // Handle headers passed as parameter addHeaders(resultBodyPart, parameters); return resultBodyPart; }
From source file:nl.surfnet.coin.teams.util.ControllerUtilImpl.java
@Override public MimeMultipart getMimeMultipartMessageBody(String plainText, String html) throws MessagingException { MimeMultipart multiPart = new MimeMultipart("alternative"); MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(plainText, "utf-8"); MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(html, "text/html; charset=utf-8"); multiPart.addBodyPart(textPart); // least important multiPart.addBodyPart(htmlPart); // most important return multiPart; }
From source file:org.hoteia.qalingo.core.util.impl.MimeMessagePreparatorImpl.java
public void prepare(MimeMessage message) throws Exception { message.addHeader("List-Unsubscribe", "<" + getUnsubscribeUrlOrEmail() + ">"); // AUTO unsubscribe for Gmail/Hotmail etc : RFC2369 if (StringUtils.isNotEmpty(getUnsubscribeUrlOrEmail())) { message.addHeader("List-Unsubscribe", "<" + getUnsubscribeUrlOrEmail() + ">"); }//from w w w. j a v a2s . c om if (getFrom() != null) { List<InternetAddress> toAddress = new ArrayList<InternetAddress>(); toAddress.add(new InternetAddress(getFrom(), getFromName())); message.addFrom(toAddress.toArray(new InternetAddress[toAddress.size()])); } if (getTo() != null) { message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(getTo())); } if (getCc() != null) { message.addRecipients(Message.RecipientType.CC, InternetAddress.parse(getCc())); } if (getBcc() != null) { message.addRecipients(Message.RecipientType.BCC, InternetAddress.parse(getBcc())); } if (getSubject() != null) { message.setSubject(getSubject()); } MimeMultipart mimeMultipart = new MimeMultipart("alternative");// multipart/mixed or mixed or related or alternative message.setContent(mimeMultipart); if (getPlainTextContent() != null) { BodyPart textBodyPart = new MimeBodyPart(); textBodyPart.setHeader("Content-Type", "text/plain; charset=\"UTF-8\""); textBodyPart.setHeader("Content-Transfer-Encoding", "base64"); textBodyPart.setContent(getPlainTextContent(), "text/plain; charset=\"UTF-8\""); mimeMultipart.addBodyPart(textBodyPart); } if (getHtmlContent() != null) { BodyPart htmlBodyPart = new MimeBodyPart(); htmlBodyPart.setHeader("Content-Type", "text/html; charset=\"UTF-8\""); htmlBodyPart.setHeader("Content-Transfer-Encoding", "base64"); htmlBodyPart.setContent(getHtmlContent(), "text/html; charset=\"UTF-8\""); mimeMultipart.addBodyPart(htmlBodyPart); } }
From source file:org.eurekastreams.server.service.actions.strategies.EmailerFactory.java
/** * Creates a "blank" email message, ready for the application to set the content (subject, body, etc.). * * @return An email message./*from w w w.jav a 2 s .c o m*/ * @throws MessagingException * Thrown if there are problems creating the message. */ public MimeMessage createMessage() throws MessagingException { Properties mailProps = new Properties(); mailProps.put("mail.transport.protocol", mailTransportProtocol); for (Map.Entry<String, String> cfg : transportConfiguration.entrySet()) { mailProps.put(cfg.getKey(), cfg.getValue()); } Session mailSession = Session.getInstance(mailProps, null); MimeMessage msg = new MimeMessage(mailSession); msg.setContent(new MimeMultipart("alternative")); msg.setSentDate(new Date()); msg.setFrom(new InternetAddress(defaultFromAddress)); return msg; }
From source file:org.apache.james.postage.mail.MailMatchingUtils.java
public static MimeMultipart convertToMimeMultipart(MimeMessage message) { try {/*from w ww. j av a 2 s .co m*/ return new MimeMultipart(message.getDataHandler().getDataSource()); } catch (MessagingException e) { throw new RuntimeException("could not convert MimeMessage to MimeMultipart", e); } }