List of usage examples for javax.mail.internet MimeBodyPart MimeBodyPart
public MimeBodyPart()
From source file:com.netflix.ice.basic.BasicWeeklyCostEmailService.java
private void sendEmail(boolean test, AmazonSimpleEmailServiceClient emailService, String email, List<ApplicationGroup> appGroups) throws IOException, MessagingException { StringBuilder body = new StringBuilder(); body.append(/* ww w.j a va 2s .co m*/ "<html><head><style type=\"text/css\">a:link, a:visited{color:#006DBA;}a:link, a:visited, a:hover {\n" + "text-decoration: none;\n" + "}\n" + "body {\n" + "color: #333;\n" + "}" + "</style></head>"); List<MimeBodyPart> mimeBodyParts = Lists.newArrayList(); int index = 0; String subject = ""; for (ApplicationGroup appGroup : appGroups) { boolean hasData = false; for (String prodName : appGroup.data.keySet()) { if (config.productService.getProductByName(prodName) == null) continue; hasData = appGroup.data.get(prodName) != null && appGroup.data.get(prodName).size() > 0; if (hasData) break; } if (!hasData) continue; try { MimeBodyPart mimeBodyPart = constructEmail(index, appGroup, body); index++; if (mimeBodyPart != null) { mimeBodyParts.add(mimeBodyPart); subject = subject + (subject.length() > 0 ? ", " : "") + appGroup.getDisplayName(); } } catch (Exception e) { logger.error("Error contructing email", e); } } body.append("</html>"); if (mimeBodyParts.size() == 0) return; DateTime end = new DateTime(DateTimeZone.UTC).withDayOfWeek(1).withMillisOfDay(0); subject = String.format("%s Weekly AWS Costs (%s - %s)", subject, formatter.print(end.minusWeeks(1)), formatter.print(end)); String toEmail = test ? testEmail : email; Session session = Session.getInstance(new Properties()); MimeMessage mimeMessage = new MimeMessage(session); mimeMessage.setSubject(subject); mimeMessage.setRecipients(javax.mail.Message.RecipientType.TO, toEmail); if (!test && !StringUtils.isEmpty(bccEmail)) { mimeMessage.addRecipients(Message.RecipientType.BCC, bccEmail); } MimeMultipart mimeMultipart = new MimeMultipart(); BodyPart p = new MimeBodyPart(); p.setContent(body.toString(), "text/html"); mimeMultipart.addBodyPart(p); for (MimeBodyPart mimeBodyPart : mimeBodyParts) mimeMultipart.addBodyPart(mimeBodyPart); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); mimeMessage.setContent(mimeMultipart); mimeMessage.writeTo(outputStream); RawMessage rawMessage = new RawMessage(ByteBuffer.wrap(outputStream.toByteArray())); SendRawEmailRequest rawEmailRequest = new SendRawEmailRequest(rawMessage); rawEmailRequest.setDestinations(Lists.<String>newArrayList(toEmail)); rawEmailRequest.setSource(fromEmail); logger.info("sending email to " + toEmail + " " + body.toString()); emailService.sendRawEmail(rawEmailRequest); }
From source file:immf.MyHtmlEmail.java
/** * @throws EmailException EmailException * @throws MessagingException MessagingException *//* www . ja v a 2 s.c om*/ private void build() throws MessagingException, EmailException { MimeMultipart rootContainer = this.getContainer(); MimeMultipart bodyEmbedsContainer = rootContainer; MimeMultipart bodyContainer = rootContainer; BodyPart msgHtml = null; BodyPart msgText = null; rootContainer.setSubType("mixed"); // determine how to form multiparts of email if (StringUtils.isNotEmpty(this.html) && this.inlineEmbeds.size() > 0) { //If HTML body and embeds are used, create a related container and add it to the root container bodyEmbedsContainer = new MimeMultipart("related"); bodyContainer = bodyEmbedsContainer; this.addPart(bodyEmbedsContainer, 0); //If TEXT body was specified, create a alternative container and add it to the embeds container if (StringUtils.isNotEmpty(this.text)) { bodyContainer = new MimeMultipart("alternative"); BodyPart bodyPart = createBodyPart(); try { bodyPart.setContent(bodyContainer); bodyEmbedsContainer.addBodyPart(bodyPart, 0); } catch (MessagingException me) { throw new EmailException(me); } } } else if (StringUtils.isNotEmpty(this.text) && StringUtils.isNotEmpty(this.html)) { //If both HTML and TEXT bodies are provided, create a alternative container and add it to the root container bodyContainer = new MimeMultipart("alternative"); this.addPart(bodyContainer, 0); } if (StringUtils.isNotEmpty(this.html)) { msgHtml = new MimeBodyPart(); bodyContainer.addBodyPart(msgHtml, 0); // apply default charset if one has been set if (StringUtils.isNotEmpty(this.charset)) { msgHtml.setContent(this.html, Email.TEXT_HTML + "; charset=" + this.charset); } else { msgHtml.setContent(this.html, Email.TEXT_HTML); } if (contentTransferEncoding != null) { msgHtml.setHeader("Content-Transfer-Encoding", contentTransferEncoding); } Iterator<InlineImage> iter = this.inlineEmbeds.values().iterator(); while (iter.hasNext()) { InlineImage ii = (InlineImage) iter.next(); bodyEmbedsContainer.addBodyPart(ii.getMbp()); } } if (StringUtils.isNotEmpty(this.text)) { msgText = new MimeBodyPart(); bodyContainer.addBodyPart(msgText, 0); // apply default charset if one has been set if (StringUtils.isNotEmpty(this.charset)) { msgText.setContent(this.text, Email.TEXT_PLAIN + "; charset=" + this.charset); } else { msgText.setContent(this.text, Email.TEXT_PLAIN); } if (contentTransferEncoding != null) { msgText.setHeader("Content-Transfer-Encoding", contentTransferEncoding); } } }
From source file:lucee.runtime.net.smtp.SMTPClient.java
public MimeBodyPart toMimeBodyPart(Multipart mp, lucee.runtime.config.Config config, Attachment att) throws MessagingException { MimeBodyPart mbp = new MimeBodyPart(); // set Data Source String strRes = att.getAbsolutePath(); if (!StringUtil.isEmpty(strRes)) { mbp.setDataHandler(new DataHandler(new ResourceDataSource(config.getResource(strRes)))); } else/* www .j a v a 2 s . com*/ mbp.setDataHandler(new DataHandler(new URLDataSource2(att.getURL()))); mbp.setFileName(att.getFileName()); if (!StringUtil.isEmpty(att.getType())) mbp.setHeader("Content-Type", att.getType()); if (!StringUtil.isEmpty(att.getDisposition())) { mbp.setDisposition(att.getDisposition()); /*if(mp instanceof MimeMultipart) { ((MimeMultipart)mp).setSubType("related"); }*/ } if (!StringUtil.isEmpty(att.getContentID())) mbp.setContentID(att.getContentID()); return mbp; }
From source file:com.zotoh.crypto.CryptoUte.java
/** * @param contentType//from ww w. jav a2 s. c o m * @param msg * @return * @throws IOException * @throws MessagingException * @throws GeneralSecurityException */ public static MimeBodyPart compressContent(String contentType, StreamData msg) throws IOException, MessagingException, GeneralSecurityException { tstEStrArg("content-type", contentType); tstObjArg("input-content", msg); SMIMECompressedGenerator gen = new SMIMECompressedGenerator(); MimeBodyPart bp = new MimeBodyPart(); SmDataSource ds; if (msg.isDiskFile()) { ds = new SmDataSource(msg.getFileRef(), contentType); } else { ds = new SmDataSource(msg.getBytes(), contentType); } bp.setDataHandler(new DataHandler(ds)); try { return gen.generate(bp, SMIMECompressedGenerator.ZLIB); } catch (SMIMEException e) { throw new GeneralSecurityException(e); } }
From source file:com.liferay.mail.imap.IMAPAccessor.java
protected Message createMessage(String personalName, String sender, Address[] to, Address[] cc, Address[] bcc, String subject, String body, List<MailFile> mailFiles) throws MessagingException, UnsupportedEncodingException { Message jxMessage = new MimeMessage(_imapConnection.getSession()); jxMessage.setFrom(new InternetAddress(sender, personalName)); jxMessage.addRecipients(Message.RecipientType.TO, to); jxMessage.addRecipients(Message.RecipientType.CC, cc); jxMessage.addRecipients(Message.RecipientType.BCC, bcc); jxMessage.setSentDate(new Date()); jxMessage.setSubject(subject);/*from w w w.j a v a 2 s. c om*/ MimeMultipart multipart = new MimeMultipart(); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(body, ContentTypes.TEXT_HTML_UTF8); multipart.addBodyPart(messageBodyPart); if (mailFiles != null) { for (MailFile mailFile : mailFiles) { File file = mailFile.getFile(); if (!file.exists()) { continue; } DataSource dataSource = new FileDataSource(file); BodyPart attachmentBodyPart = new MimeBodyPart(); attachmentBodyPart.setDataHandler(new DataHandler(dataSource)); attachmentBodyPart.setFileName(mailFile.getFileName()); multipart.addBodyPart(attachmentBodyPart); } } jxMessage.setContent(multipart); return jxMessage; }
From source file:org.apache.james.transport.mailets.managesieve.ManageSieveMailetTestCase.java
private MimeMessage prepareMessageWithAttachment(String scriptContent, String subject) throws MessagingException, IOException { MimeMessage message = prepareMimeMessage(subject); MimeMultipart multipart = new MimeMultipart(); MimeBodyPart scriptPart = new MimeBodyPart(); scriptPart.setDataHandler(//from ww w. j a va2 s.c o m new DataHandler(new ByteArrayDataSource(scriptContent, "application/sieve; charset=UTF-8"))); scriptPart.setDisposition(MimeBodyPart.ATTACHMENT); // setting a DataHandler with no mailcap definition is not // supported by the specs. Javamail activation still work, // but Geronimo activation translate it to text/plain. // Let's manually force the header. scriptPart.setHeader("Content-Type", "application/sieve; charset=UTF-8"); scriptPart.setFileName(SCRIPT_NAME); multipart.addBodyPart(scriptPart); message.setContent(multipart); message.saveChanges(); return message; }
From source file:fr.xebia.cloud.amazon.aws.iam.AmazonAwsIamAccountCreator.java
private void sendEmail(Map<String, String> templatesParams, List<BodyPart> attachments, String toAddress) throws MessagingException { MimeBodyPart htmlAndPlainTextAlternativeBody = new MimeBodyPart(); // TEXT AND HTML MESSAGE (gmail requires plain text alternative, otherwise, it displays the 1st plain text attachment in the preview) MimeMultipart cover = new MimeMultipart("alternative"); htmlAndPlainTextAlternativeBody.setContent(cover); BodyPart textHtmlBodyPart = new MimeBodyPart(); String textHtmlBody = FreemarkerUtils.generate(templatesParams, "/fr/xebia/cloud/amazon/aws/iam/amazon-aws-iam-credentials-email-" + environment.getIdentifier() + ".html.ftl"); textHtmlBodyPart.setContent(textHtmlBody, "text/html"); cover.addBodyPart(textHtmlBodyPart); BodyPart textPlainBodyPart = new MimeBodyPart(); cover.addBodyPart(textPlainBodyPart); String textPlainBody = FreemarkerUtils.generate(templatesParams, "/fr/xebia/cloud/amazon/aws/iam/amazon-aws-iam-credentials-email-" + environment.getIdentifier() + ".txt.ftl"); textPlainBodyPart.setContent(textPlainBody, "text/plain"); MimeMultipart content = new MimeMultipart("related"); content.addBodyPart(htmlAndPlainTextAlternativeBody); // ATTACHMENTS for (BodyPart bodyPart : attachments) { content.addBodyPart(bodyPart);//from w ww. j a va2s . co m } MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(mailFrom); msg.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(toAddress)); msg.addRecipient(javax.mail.Message.RecipientType.CC, mailFrom); String subject = "[Xebia Amazon AWS " + environment.getIdentifier() + "] Credentials"; msg.setSubject(subject); msg.setContent(content); mailTransport.sendMessage(msg, msg.getAllRecipients()); }
From source file:org.apache.camel.component.mail.MailBinding.java
protected void createMultipartAlternativeMessage(MimeMessage mimeMessage, MailConfiguration configuration, Exchange exchange) throws MessagingException, IOException { MimeMultipart multipartAlternative = new MimeMultipart("alternative"); mimeMessage.setContent(multipartAlternative); MimeBodyPart plainText = new MimeBodyPart(); plainText.setText(getAlternativeBody(configuration, exchange), determineCharSet(configuration, exchange)); // remove the header with the alternative mail now that we got it // otherwise it might end up twice in the mail reader exchange.getIn().removeHeader(configuration.getAlternativeBodyHeader()); multipartAlternative.addBodyPart(plainText); // if there are no attachments, add the body to the same mulitpart message if (!exchange.getIn().hasAttachments()) { addBodyToMultipart(configuration, multipartAlternative, exchange); } else {//from w ww .j a va 2s. c om // if there are attachments, but they aren't set to be inline, add them to // treat them as normal. It will append a multipart-mixed with the attachments and the body text if (!configuration.isUseInlineAttachments()) { BodyPart mixedAttachments = new MimeBodyPart(); mixedAttachments.setContent(createMixedMultipartAttachments(configuration, exchange)); multipartAlternative.addBodyPart(mixedAttachments); } else { // if the attachments are set to be inline, attach them as inline attachments MimeMultipart multipartRelated = new MimeMultipart("related"); BodyPart related = new MimeBodyPart(); related.setContent(multipartRelated); multipartAlternative.addBodyPart(related); addBodyToMultipart(configuration, multipartRelated, exchange); addAttachmentsToMultipart(multipartRelated, Part.INLINE, exchange); } } }
From source file:org.jahia.services.workflow.jbpm.custom.email.JBPMMailProducer.java
protected void addAttachments(MailTemplate template, WorkItem workItem, Multipart multipart, JCRSessionWrapper session) throws Exception { for (AttachmentTemplate attachmentTemplate : template.getAttachmentTemplates()) { BodyPart attachmentPart = new MimeBodyPart(); // resolve description String description = attachmentTemplate.getDescription(); if (description != null) { attachmentPart.setDescription(evaluateExpression(workItem, description, session)); }//ww w. ja v a2s .c om // obtain interface to data DataHandler dataHandler = createDataHandler(attachmentTemplate, workItem, session); attachmentPart.setDataHandler(dataHandler); // resolve file name String name = attachmentTemplate.getName(); if (name != null) { attachmentPart.setFileName(evaluateExpression(workItem, name, session)); } else { // fall back on data source DataSource dataSource = dataHandler.getDataSource(); if (dataSource instanceof URLDataSource) { name = extractResourceName(((URLDataSource) dataSource).getURL()); } else { name = dataSource.getName(); } if (name != null) { attachmentPart.setFileName(name); } } multipart.addBodyPart(attachmentPart); } }
From source file:com.zotoh.crypto.CryptoUte.java
/** * @param cType/*from w w w. j ava 2s .c om*/ * @param cte * @param contentLoc * @param cid * @param msg * @return * @throws MessagingException * @throws IOException * @throws GeneralSecurityException */ public static MimeBodyPart compressContent(String cType, String cte, String contentLoc, String cid, StreamData msg) throws MessagingException, IOException, GeneralSecurityException { tstEStrArg("content-type", cType); tstEStrArg("content-id", cid); tstObjArg("input-content", msg); SMIMECompressedGenerator gen = new SMIMECompressedGenerator(); MimeBodyPart bp = new MimeBodyPart(); SmDataSource ds; if (msg.isDiskFile()) { ds = new SmDataSource(msg.getFileRef(), cType); } else { ds = new SmDataSource(msg.getBytes(), cType); } if (!isEmpty(contentLoc)) { bp.setHeader("content-location", contentLoc); } try { bp.setHeader("content-id", cid); bp.setDataHandler(new DataHandler(ds)); bp = gen.generate(bp, SMIMECompressedGenerator.ZLIB); } catch (SMIMEException e) { throw new GeneralSecurityException(e); } if (true) { int pos = cid.lastIndexOf(">"); if (pos >= 0) { cid = cid.substring(0, pos) + "--z>"; } else { cid = cid + "--z"; } } if (!isEmpty(contentLoc)) { bp.setHeader("content-location", contentLoc); } bp.setHeader("content-id", cid); // always base64 cte = "base64"; if (!isEmpty(cte)) { bp.setHeader("content-transfer-encoding", cte); } return bp; }