List of usage examples for javax.mail BodyPart setHeader
public void setHeader(String header_name, String header_value) throws MessagingException;
From source file:org.apache.camel.component.mail.MailBinding.java
protected void addAttachmentsToMultipart(MimeMultipart multipart, String partDisposition, Exchange exchange) throws MessagingException { LOG.trace("Adding attachments +++ start +++"); int i = 0;/*from w w w . j a va2 s. c o m*/ for (Map.Entry<String, DataHandler> entry : exchange.getIn().getAttachments().entrySet()) { String attachmentFilename = entry.getKey(); DataHandler handler = entry.getValue(); if (LOG.isTraceEnabled()) { LOG.trace("Attachment #" + i + ": Disposition: " + partDisposition); LOG.trace("Attachment #" + i + ": DataHandler: " + handler); LOG.trace("Attachment #" + i + ": FileName: " + attachmentFilename); } if (handler != null) { if (shouldAddAttachment(exchange, attachmentFilename, handler)) { // Create another body part BodyPart messageBodyPart = new MimeBodyPart(); // Set the data handler to the attachment messageBodyPart.setDataHandler(handler); if (attachmentFilename.toLowerCase().startsWith("cid:")) { // add a Content-ID header to the attachment // must use angle brackets according to RFC: http://www.ietf.org/rfc/rfc2392.txt messageBodyPart.addHeader("Content-ID", "<" + attachmentFilename.substring(4) + ">"); // Set the filename without the cid messageBodyPart.setFileName(attachmentFilename.substring(4)); } else { // Set the filename messageBodyPart.setFileName(attachmentFilename); } LOG.trace("Attachment #" + i + ": ContentType: " + messageBodyPart.getContentType()); if (contentTypeResolver != null) { String contentType = contentTypeResolver.resolveContentType(attachmentFilename); LOG.trace("Attachment #" + i + ": Using content type resolver: " + contentTypeResolver + " resolved content type as: " + contentType); if (contentType != null) { String value = contentType + "; name=" + attachmentFilename; messageBodyPart.setHeader("Content-Type", value); LOG.trace("Attachment #" + i + ": ContentType: " + messageBodyPart.getContentType()); } } // Set Disposition messageBodyPart.setDisposition(partDisposition); // Add part to multipart multipart.addBodyPart(messageBodyPart); } else { LOG.trace("shouldAddAttachment: false"); } } else { LOG.warn("Cannot add attachment: " + attachmentFilename + " as DataHandler is null"); } i++; } LOG.trace("Adding attachments +++ done +++"); }
From source file:org.eurekastreams.server.service.actions.strategies.EmailerFactory.java
/** * @param message// www.j ava 2s.c o m * Email message being built. * @param htmlBody * Rich HTML body of the Email. * @throws MessagingException * Thrown if there are problems creating the message. */ public void setHtmlBody(final MimeMessage message, final String htmlBody) throws MessagingException { BodyPart htmlBp = new MimeBodyPart(); htmlBp.setContent(htmlBody, "text/html"); htmlBp.setHeader("MIME-VERSION", "1.0"); htmlBp.setHeader("Content-Type", "text/html; charset=ISO-8859-1"); getMultipart(message).addBodyPart(htmlBp); }
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 ww w. j a va 2s .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.mule.transport.email.transformers.ObjectToMimeMessage.java
protected void addBodyPartHeaders(BodyPart part, String name, MuleMessage message) { Map<String, String> headers = message .getOutboundProperty(name + AbstractMailConnector.ATTACHMENT_HEADERS_PROPERTY_POSTFIX); if (null != headers) { for (String key : headers.keySet()) { try { part.setHeader(key, headers.get(key)); } catch (MessagingException me) { logger.error("Failed to set bodypart header", me); }//www. java 2 s . c o m } } }
From source file:org.openhim.mediator.normalization.XDSbMimeProcessorActor.java
private void copyHeadersToPart(Enumeration headers, BodyPart part) throws MessagingException { while (headers.hasMoreElements()) { Header header = (Header) headers.nextElement(); part.setHeader(header.getName(), header.getValue()); }/*from w w w.java2 s.c om*/ }
From source file:org.unitime.commons.Email.java
public void addAttachement(DataSource source) throws MessagingException { BodyPart attachement = new MimeBodyPart(); attachement.setDataHandler(new DataHandler(source)); attachement.setFileName(source.getName()); attachement.setHeader("Content-ID", source.getName()); iBody.addBodyPart(attachement);/* w ww. java2 s .c om*/ }
From source file:org.unitime.commons.JavaMailWrapper.java
@Override protected void addAttachment(String name, DataHandler data) throws MessagingException { BodyPart attachment = new MimeBodyPart(); attachment.setDataHandler(data);/*from w w w. j a va 2 s . c om*/ attachment.setFileName(name); attachment.setHeader("Content-ID", "<" + name + ">"); iBody.addBodyPart(attachment); }
From source file:org.xwiki.commons.internal.DefaultMailSender.java
private Multipart generateMimeMultipart(Mail mail) throws MessagingException { Multipart contentsMultipart = new MimeMultipart("alternative"); List<String> foundEmbeddedImages = new ArrayList<String>(); boolean hasAttachment = false; if (mail.getContents().size() == 1) // To add an alternative plain part {//w w w. j a v a 2 s. c o m String[] content = mail.getContents().get(0); if (content[0].equals("text/plain")) { BodyPart alternativePart = new MimeBodyPart(); alternativePart.setContent(content[1], content[0] + "; charset=" + EMAIL_ENCODING); alternativePart.setHeader("Content-Disposition", "inline"); alternativePart.setHeader("Content-Transfer-Encoding", "quoted-printable"); contentsMultipart.addBodyPart(alternativePart); } if (content[0].equals("text/html")) { BodyPart alternativePart = new MimeBodyPart(); String parsedText = createPlain(content[1]); alternativePart.setContent(parsedText, "text/plain; charset=" + EMAIL_ENCODING); alternativePart.setHeader("Content-Disposition", "inline"); alternativePart.setHeader("Content-Transfer-Encoding", "quoted-printable"); contentsMultipart.addBodyPart(alternativePart); } } for (String[] content : mail.getContents()) { BodyPart contentPart = new MimeBodyPart(); contentPart.setContent(content[1], content[0] + "; charset=" + EMAIL_ENCODING); contentPart.setHeader("Content-Disposition", "inline"); contentPart.setHeader("Content-Transfer-Encoding", "quoted-printable"); if (content[0].equals("text/html")) { boolean hasEmbeddedImages = false; List<MimeBodyPart> embeddedImages = new ArrayList<MimeBodyPart>(); Pattern cidPattern = Pattern.compile("src=('|\")cid:([^'\"]*)('|\")", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); Matcher matcher = cidPattern.matcher(content[1]); String filename; while (matcher.find()) { filename = matcher.group(2); for (Attachment attachment : mail.getAttachments()) { if (filename.equals(attachment.getFilename())) { hasEmbeddedImages = true; MimeBodyPart part = createAttachmentPart(attachment); embeddedImages.add(part); foundEmbeddedImages.add(filename); } } } if (hasEmbeddedImages) { Multipart htmlMultipart = new MimeMultipart("related"); htmlMultipart.addBodyPart(contentPart); for (MimeBodyPart imagePart : embeddedImages) { htmlMultipart.addBodyPart(imagePart); } BodyPart HtmlWrapper = new MimeBodyPart(); HtmlWrapper.setContent(htmlMultipart); contentsMultipart.addBodyPart(HtmlWrapper); } else contentsMultipart.addBodyPart(contentPart); } else { contentsMultipart.addBodyPart(contentPart); } } Multipart attachmentsMultipart = new MimeMultipart(); for (Attachment attachment : mail.getAttachments()) { if (!foundEmbeddedImages.contains(attachment.getFilename())) { hasAttachment = true; MimeBodyPart part = this.createAttachmentPart(attachment); attachmentsMultipart.addBodyPart(part); } } if (hasAttachment) { Multipart wrapper = new MimeMultipart("mixed"); BodyPart body = new MimeBodyPart(); body.setContent(contentsMultipart); wrapper.addBodyPart(body); BodyPart attachments = new MimeBodyPart(); attachments.setContent(attachmentsMultipart); wrapper.addBodyPart(attachments); return wrapper; } return contentsMultipart; }
From source file:se.inera.axel.shs.processor.ShsMessageMarshaller.java
public void marshal(ShsMessage shsMessage, OutputStream outputStream) throws IllegalMessageStructureException { log.trace("marshal(ShsMessage, OutputStream)"); MimeMultipart multipart = new MimeMultipart(); BodyPart bodyPart = new MimeBodyPart(); try {//from w ww. j a v a 2s .c om ShsLabel label = shsMessage.getLabel(); if (label == null) { throw new IllegalMessageStructureException("label not found in shs message"); } Content content = label.getContent(); if (content == null) { throw new IllegalMessageStructureException("label/content not found in shs label"); } else { // we will update this according to our data parts below. content.getDataOrCompound().clear(); String contentId = content.getContentId(); content.setContentId(contentId.substring(0, Math.min(contentId.length(), MAX_LENGTH_CONTENT_ID))); } List<DataPart> dataParts = shsMessage.getDataParts(); if (dataParts.isEmpty()) { throw new IllegalMessageStructureException("dataparts not found in message"); } for (DataPart dp : dataParts) { Data data = new Data(); data.setDatapartType(dp.getDataPartType()); data.setFilename(dp.getFileName()); if (dp.getContentLength() != null && dp.getContentLength() > 0) data.setNoOfBytes("" + dp.getContentLength()); content.getDataOrCompound().add(data); } bodyPart.setContent(shsLabelMarshaller.marshal(label), "text/xml; charset=ISO-8859-1"); bodyPart.setHeader("Content-Transfer-Encoding", "binary"); multipart.addBodyPart(bodyPart); for (DataPart dataPart : dataParts) { bodyPart = new MimeBodyPart(); bodyPart.setDisposition(Part.ATTACHMENT); if (StringUtils.isNotBlank(dataPart.getFileName())) { bodyPart.setFileName(dataPart.getFileName()); } bodyPart.setDataHandler(dataPart.getDataHandler()); if (dataPart.getTransferEncoding() != null) { bodyPart.addHeader("Content-Transfer-Encoding", dataPart.getTransferEncoding().toLowerCase()); } multipart.addBodyPart(bodyPart); } MimeMessage mimeMessage = new MimeMessage(session); mimeMessage.setSubject("SHS Message"); mimeMessage.addHeader("Content-Transfer-Encoding", "binary"); mimeMessage.setContent(multipart); mimeMessage.saveChanges(); String ignoreList[] = { "Message-ID" }; mimeMessage.writeTo(outputStream, ignoreList); outputStream.flush(); } catch (Exception e) { if (e instanceof IllegalMessageStructureException) { throw (IllegalMessageStructureException) e; } throw new IllegalMessageStructureException(e); } }
From source file:ste.xtest.mail.BugFreeFileTransport.java
@Test public void send_multipart_message() throws Exception { Session session = Session.getInstance(config); Message message = new MimeMessage(Session.getInstance(config)); message.setFrom(new InternetAddress("from@domain.com")); message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@domain.com")); message.setSubject("the subject"); MimeMultipart multipart = new MimeMultipart("related"); BodyPart messageBodyPart = new MimeBodyPart(); String htmlText = "<H1>hello world</H1><img src=\"cid:image\">"; messageBodyPart.setContent(htmlText, "text/html"); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); DataSource fds = new FileDataSource("src/test/resources/images/6096.png"); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setHeader("Content-ID", "<image>"); multipart.addBodyPart(messageBodyPart); message.setContent(multipart);/* ww w . j a va 2 s . co m*/ session.getTransport().sendMessage(message, message.getAllRecipients()); then(FileUtils.readFileToString(new File(TMP.getRoot(), "message"))).contains("From: from@domain.com\r") .contains("To: to@domain.com\r").contains("Subject: the subject\r").contains("hello world") .contains("Content-ID: <image>"); }