List of usage examples for javax.mail.internet MimeBodyPart setDisposition
@Override public void setDisposition(String disposition) throws MessagingException
From source file:com.szmslab.quickjavamail.send.MailSender.java
/** * MimeBodyPart????Content-Disposition: attachment * * @param file//from w w w. ja va2s . com * * @return MimeBodyPartContent-Disposition: attachment? * @throws MessagingException * @throws UnsupportedEncodingException */ private MimeBodyPart createAttachmentPart(AttachmentFile file) throws MessagingException, UnsupportedEncodingException { MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.setFileName(MimeUtility.encodeText(file.getFileName(), charset, null)); attachmentPart.setDataHandler(new DataHandler(file.getDataSource())); attachmentPart.setDisposition(MimeBodyPart.ATTACHMENT); return attachmentPart; }
From source file:com.ikon.util.MailUtils.java
/** * Create a mail./*w ww .j a v a 2 s. c om*/ * * @param fromAddress Origin address. * @param toAddress Destination addresses. * @param subject The mail subject. * @param text The mail body. * @throws MessagingException If there is any error. */ private static MimeMessage create(String fromAddress, Collection<String> toAddress, String subject, String text, Collection<String> docsPath, List<File> tmpAttachments) throws MessagingException, PathNotFoundException, AccessDeniedException, RepositoryException, IOException, DatabaseException { log.debug("create({}, {}, {}, {}, {})", new Object[] { fromAddress, toAddress, subject, text, docsPath }); Session mailSession = getMailSession(); MimeMessage msg = new MimeMessage(mailSession); if (fromAddress != null) { InternetAddress from = new InternetAddress(fromAddress); msg.setFrom(from); } else { msg.setFrom(); } InternetAddress[] to = new InternetAddress[toAddress.size()]; int idx = 0; for (Iterator<String> it = toAddress.iterator(); it.hasNext();) { to[idx++] = new InternetAddress(it.next()); } // Build a multiparted mail with HTML and text content for better SPAM behaviour Multipart content = new MimeMultipart(); // HTML Part MimeBodyPart htmlPart = new MimeBodyPart(); StringBuilder htmlContent = new StringBuilder(); htmlContent.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"); htmlContent.append("<html>\n<head>\n"); htmlContent.append("<meta content=\"text/html;charset=UTF-8\" http-equiv=\"Content-Type\"/>\n"); htmlContent.append("</head>\n<body>\n"); htmlContent.append(text); htmlContent.append("\n</body>\n</html>"); htmlPart.setContent(htmlContent.toString(), "text/html;charset=UTF-8"); htmlPart.setHeader("Content-Type", "text/html;charset=UTF-8"); htmlPart.setDisposition(Part.INLINE); content.addBodyPart(htmlPart); idx = 0; if (docsPath != null) { for (String docPath : docsPath) { InputStream is = null; FileOutputStream fos = null; String docName = PathUtils.getName(docPath); try { final Document doc = OKMDocument.getInstance().getProperties(null, docPath); is = OKMDocument.getInstance().getContent(null, docPath, false); final File tmpAttch = tmpAttachments.get(idx++); fos = new FileOutputStream(tmpAttch); IOUtils.copy(is, fos); fos.flush(); // Document attachment part MimeBodyPart docPart = new MimeBodyPart(); DataSource source = new FileDataSource(tmpAttch.getPath()) { public String getContentType() { return doc.getMimeType(); } }; docPart.setDataHandler(new DataHandler(source)); docPart.setFileName(MimeUtility.encodeText(docName)); docPart.setDisposition(Part.ATTACHMENT); content.addBodyPart(docPart); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(fos); } } } msg.setHeader("MIME-Version", "1.0"); msg.setHeader("Content-Type", content.getContentType()); msg.addHeader("Charset", "UTF-8"); msg.setRecipients(Message.RecipientType.TO, to); msg.setSubject(subject, "UTF-8"); msg.setSentDate(new Date()); msg.setContent(content); msg.saveChanges(); log.debug("create: {}", msg); return msg; }
From source file:immf.MyHtmlEmail.java
/** * Embeds the specified <code>DataSource</code> in the HTML using the * specified Content-ID. Returns the specified Content-ID string. * * @param dataSource the <code>DataSource</code> to embed * @param name the name that will be set in the filename header field * @param cid the Content-ID to use for this <code>DataSource</code> * @return the supplied Content-ID for this <code>DataSource</code> * @throws EmailException if the embedding fails or if <code>name</code> is * null or empty/*from w w w .j ava 2s .co m*/ * @since 1.1 */ public String embed(DataSource dataSource, String name, String cid) throws EmailException { if (StringUtils.isEmpty(name)) { throw new EmailException("name cannot be null or empty"); } MimeBodyPart mbp = new MimeBodyPart(); try { mbp.setDataHandler(new DataHandler(dataSource)); mbp.setFileName(name); mbp.setDisposition("inline"); mbp.setContentID("<" + cid + ">"); InlineImage ii = new InlineImage(cid, dataSource, mbp); this.inlineEmbeds.put(name, ii); return cid; } catch (MessagingException me) { throw new EmailException(me); } }
From source file:it.greenvulcano.gvesb.virtual.utils.MimeMessageHelper.java
public MimeMessage getMimeMessage() throws MessagingException { if (attachments.isEmpty()) { switch (messageType) { case HTML: mimeMessage.setContent(messageBody, "text/html; charset=utf-8"); break; case TEXT: mimeMessage.setText(messageBody, "UTF-8"); mimeMessage.addHeader("Content-Transfer-Encoding", "quoted-printable"); break; }//from w w w.j a va 2 s. c om } else { MimeBodyPart mimeBodyPart = new MimeBodyPart(); switch (messageType) { case HTML: mimeBodyPart.setContent(messageBody, "text/html; charset=utf-8"); break; case TEXT: mimeBodyPart.setText(messageBody, "UTF-8"); mimeMessage.addHeader("Content-Transfer-Encoding", "quoted-printable"); break; } mimeBodyPart.setDisposition(Part.INLINE); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(mimeBodyPart); for (BodyPart attachmentPart : attachments) { multipart.addBodyPart(attachmentPart); } mimeMessage.setContent(multipart); } return mimeMessage; }
From source file:com.szmslab.quickjavamail.send.MailSender.java
/** * MimeBodyPart????Content-Disposition: inline * * @param file//ww w . ja v a 2 s .c o m * ? * @return MimeBodyPartContent-Disposition: inline? * @throws MessagingException * @throws UnsupportedEncodingException */ private MimeBodyPart createImagePart(InlineImageFile file) throws MessagingException, UnsupportedEncodingException { MimeBodyPart imagePart = new MimeBodyPart(); imagePart.setContentID(file.getContentId()); imagePart.setFileName(MimeUtility.encodeText(file.getFileName(), charset, null)); imagePart.setDataHandler(new DataHandler(file.getDataSource())); imagePart.setDisposition(MimeBodyPart.INLINE); return imagePart; }
From source file:de.betterform.connector.serializer.MultipartRelatedSerializer.java
public void serialize(Submission submission, Node node, SerializerRequestWrapper wrapper, String defaultEncoding) throws Exception { Map cache = new HashMap(); MimeMultipart multipart = new MimeMultipart(); MimeBodyPart part = new MimeBodyPart(); multipart.addBodyPart(part);/*from w w w .j a v a2 s . c o m*/ String encoding = defaultEncoding; if (submission.getEncoding() != null) { encoding = submission.getEncoding(); } if (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == Node.DOCUMENT_NODE) { part.setText(new String(serializeXML(multipart, cache, submission, node, encoding), encoding), encoding); } else { part.setText(node.getTextContent()); } part.setContentID("<instance.xml@start>"); part.addHeader("Content-Type", "application/xml"); part.addHeader("Content-Transfer-Encoding", "base64"); part.setDisposition("attachment"); part.setFileName("instance.xml"); multipart.setSubType("related; type=\"" + submission.getMediatype() + "\"; start=\"instance.xml@start\""); // FIXME: Is this a global http header or a local mime header? wrapper.getBodyStream() .write(("Content-Type: " + multipart.getContentType() + "\n\nThis is a MIME message.\n") .getBytes(encoding)); multipart.writeTo(wrapper.getBodyStream()); }
From source file:immf.MyHtmlEmail.java
public String embed(DataSource dataSource, String name, String nameCharset, String cid) throws EmailException { if (StringUtils.isEmpty(name)) { throw new EmailException("name cannot be null or empty"); }//from w w w . ja v a 2 s . com MimeBodyPart mbp = new MimeBodyPart(); try { mbp.setDataHandler(new DataHandler(dataSource)); Util.setFileName(mbp, name, nameCharset, null); //mbp.setFileName(name); mbp.setDisposition("inline"); mbp.setContentID("<" + cid + ">"); InlineImage ii = new InlineImage(cid, dataSource, mbp); this.inlineEmbeds.put(name, ii); return cid; } catch (MessagingException me) { throw new EmailException(me); } }
From source file:com.openkm.util.MailUtils.java
/** * Create a mail.//from w w w .jav a 2s . c o m * * @param fromAddress Origin address. * @param toAddress Destination addresses. * @param subject The mail subject. * @param text The mail body. * @throws MessagingException If there is any error. */ private static MimeMessage create(String fromAddress, Collection<String> toAddress, String subject, String text, Collection<String> docsPath, List<File> tmpAttachments) throws MessagingException, PathNotFoundException, AccessDeniedException, RepositoryException, IOException, DatabaseException { log.debug("create({}, {}, {}, {}, {})", new Object[] { fromAddress, toAddress, subject, text, docsPath }); Session mailSession = getMailSession(); MimeMessage msg = new MimeMessage(mailSession); if (fromAddress != null && Config.SEND_MAIL_FROM_USER) { InternetAddress from = new InternetAddress(fromAddress); msg.setFrom(from); } else { msg.setFrom(); } InternetAddress[] to = new InternetAddress[toAddress.size()]; int idx = 0; for (Iterator<String> it = toAddress.iterator(); it.hasNext();) { to[idx++] = new InternetAddress(it.next()); } // Build a multiparted mail with HTML and text content for better SPAM behaviour Multipart content = new MimeMultipart(); // HTML Part MimeBodyPart htmlPart = new MimeBodyPart(); StringBuilder htmlContent = new StringBuilder(); htmlContent.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"); htmlContent.append("<html>\n<head>\n"); htmlContent.append("<meta content=\"text/html;charset=UTF-8\" http-equiv=\"Content-Type\"/>\n"); htmlContent.append("</head>\n<body>\n"); htmlContent.append(text); htmlContent.append("\n</body>\n</html>"); htmlPart.setContent(htmlContent.toString(), "text/html;charset=UTF-8"); htmlPart.setHeader("Content-Type", "text/html;charset=UTF-8"); htmlPart.setDisposition(Part.INLINE); content.addBodyPart(htmlPart); idx = 0; if (docsPath != null) { for (String docPath : docsPath) { InputStream is = null; FileOutputStream fos = null; String docName = PathUtils.getName(docPath); try { final Document doc = OKMDocument.getInstance().getProperties(null, docPath); is = OKMDocument.getInstance().getContent(null, docPath, false); final File tmpAttch = tmpAttachments.get(idx++); fos = new FileOutputStream(tmpAttch); IOUtils.copy(is, fos); fos.flush(); // Document attachment part MimeBodyPart docPart = new MimeBodyPart(); DataSource source = new FileDataSource(tmpAttch.getPath()) { public String getContentType() { return doc.getMimeType(); } }; docPart.setDataHandler(new DataHandler(source)); docPart.setFileName(MimeUtility.encodeText(docName)); docPart.setDisposition(Part.ATTACHMENT); content.addBodyPart(docPart); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(fos); } } } msg.setHeader("MIME-Version", "1.0"); msg.setHeader("Content-Type", content.getContentType()); msg.addHeader("Charset", "UTF-8"); msg.setRecipients(Message.RecipientType.TO, to); msg.setSubject(subject, "UTF-8"); msg.setSentDate(new Date()); msg.setContent(content); msg.saveChanges(); log.debug("create: {}", msg); return msg; }
From source file:com.netflix.ice.basic.BasicWeeklyCostEmailService.java
private MimeBodyPart constructEmail(int index, ApplicationGroup appGroup, StringBuilder body) throws IOException, MessagingException { if (index == 0 && !StringUtils.isEmpty(headerNote)) body.append(headerNote);//from w w w . j a v a 2 s . c o m numberFormatter.setMaximumFractionDigits(1); numberFormatter.setMinimumFractionDigits(1); File file = createImage(appGroup); if (file == null) return null; DateTime end = new DateTime(DateTimeZone.UTC).withDayOfWeek(1).withMillisOfDay(0); String link = getLink("area", ConsolidateType.hourly, appGroup, accounts, regions, end.minusWeeks(numWeeks), end); body.append(String.format("<b><h4><a href='%s'>%s</a> Weekly Costs:</h4></b>", link, appGroup.getDisplayName())); body.append("<table style=\"border: 1px solid #DDD; border-collapse: collapse\">"); body.append( "<tr style=\"background-color: whiteSmoke;text-align:center\" ><td style=\"border-left: 1px solid #DDD;\"></td>"); for (int i = 0; i <= accounts.size(); i++) { int cols = i == accounts.size() ? 1 : regions.size(); String accName = i == accounts.size() ? "total" : accounts.get(i).name; body.append(String.format( "<td style=\"border-left: 1px solid #DDD;font-weight: bold;padding: 4px\" colspan='%d'>", cols)) .append(accName).append("</td>"); } body.append("</tr>"); body.append("<tr style=\"background-color: whiteSmoke;text-align:center\" ><td></td>"); for (int i = 0; i < accounts.size(); i++) { boolean first = true; for (Region region : regions) { body.append("<td style=\"font-weight: bold;padding: 4px;" + (first ? "border-left: 1px solid #DDD;" : "") + "\">").append(region.name) .append("</td>"); first = false; } } body.append("<td style=\"border-left: 1px solid #DDD;\"></td></tr>"); Map<String, Double> costs = Maps.newHashMap(); Interval interval = new Interval(end.minusWeeks(numWeeks), end); double[] total = new double[numWeeks]; for (Product product : products) { List<ResourceGroup> resourceGroups = getResourceGroups(appGroup, product); if (resourceGroups.size() == 0) { continue; } DataManager dataManager = config.managers.getCostManager(product, ConsolidateType.weekly); if (dataManager == null) { continue; } for (int i = 0; i < accounts.size(); i++) { List<Account> accountList = Lists.newArrayList(accounts.get(i)); TagLists tagLists = new TagLists(accountList, regions, null, Lists.newArrayList(product), null, null, resourceGroups); Map<Tag, double[]> data = dataManager.getData(interval, tagLists, TagType.Region, AggregateType.none, false); for (Tag tag : data.keySet()) { for (int week = 0; week < numWeeks; week++) { String key = accounts.get(i) + "|" + tag + "|" + week; if (costs.containsKey(key)) costs.put(key, data.get(tag)[week] + costs.get(key)); else costs.put(key, data.get(tag)[week]); total[week] += data.get(tag)[week]; } } } } boolean firstLine = true; DateTime currentWeekEnd = end; for (int week = numWeeks - 1; week >= 0; week--) { String weekStr; if (week == numWeeks - 1) weekStr = "Last week"; else weekStr = (numWeeks - week - 1) + " weeks ago"; String background = week % 2 == 1 ? "background: whiteSmoke;" : ""; body.append(String.format( "<tr style=\"%s\"><td nowrap style=\"border-left: 1px solid #DDD;padding: 4px\">%s (%s - %s)</td>", background, weekStr, formatter.print(currentWeekEnd.minusWeeks(1)).substring(5), formatter.print(currentWeekEnd).substring(5))); for (int i = 0; i < accounts.size(); i++) { Account account = accounts.get(i); for (int j = 0; j < regions.size(); j++) { Region region = regions.get(j); String key = account + "|" + region + "|" + week; double cost = costs.get(key) == null ? 0 : costs.get(key); Double lastCost = week == 0 ? null : costs.get(account + "|" + region + "|" + (week - 1)); link = getLink("column", ConsolidateType.daily, appGroup, Lists.newArrayList(account), Lists.newArrayList(region), currentWeekEnd.minusWeeks(1), currentWeekEnd); body.append(getValueCell(cost, lastCost, link, firstLine)); } } link = getLink("column", ConsolidateType.daily, appGroup, accounts, regions, currentWeekEnd.minusWeeks(1), currentWeekEnd); body.append(getValueCell(total[week], week == 0 ? null : total[week - 1], link, firstLine)); body.append("</tr>"); firstLine = false; currentWeekEnd = currentWeekEnd.minusWeeks(1); } body.append("</table>"); numberFormatter.setMaximumFractionDigits(0); numberFormatter.setMinimumFractionDigits(0); if (!StringUtils.isEmpty(throughputMetrics)) body.append(throughputMetrics); body.append("<br><img src=\"cid:image_cid_" + index + "\"><br>"); for (Map.Entry<String, List<String>> entry : appGroup.data.entrySet()) { String product = entry.getKey(); List<String> selected = entry.getValue(); if (selected == null || selected.size() == 0) continue; link = getLink("area", ConsolidateType.hourly, appGroup, accounts, regions, end.minusWeeks(numWeeks), end); body.append(String.format("<b><h4>%s in <a href='%s'>%s</a>:</h4></b>", getResourceGroupsDisplayName(product), link, appGroup.getDisplayName())); for (String name : selected) body.append(" ").append(name).append("<br>"); } body.append("<hr><br>"); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setFileName(file.getName()); DataSource ds = new ByteArrayDataSource(new FileInputStream(file), "image/png"); mimeBodyPart.setDataHandler(new DataHandler(ds)); mimeBodyPart.setHeader("Content-ID", "<image_cid_" + index + ">"); mimeBodyPart.setHeader("Content-Disposition", "inline"); mimeBodyPart.setDisposition(MimeBodyPart.INLINE); file.delete(); return mimeBodyPart; }