List of usage examples for javax.mail BodyPart setContent
public void setContent(Object obj, String type) throws MessagingException;
From source file:org.smartloli.kafka.eagle.api.email.MailServiceImpl.java
/** Send mail in HTML format */ private boolean sendHtmlMail(MailSenderInfo mailInfo) { boolean enableMailAlert = SystemConfigUtils.getBooleanProperty("kafka.eagle.mail.enable"); if (enableMailAlert) { SaAuthenticatorInfo authenticator = null; Properties pro = mailInfo.getProperties(); if (mailInfo.isValidate()) { authenticator = new SaAuthenticatorInfo(mailInfo.getUserName(), mailInfo.getPassword()); }/*from w ww. j a v a 2 s .co m*/ Session sendMailSession = Session.getDefaultInstance(pro, authenticator); try { Message mailMessage = new MimeMessage(sendMailSession); Address from = new InternetAddress(mailInfo.getFromAddress()); mailMessage.setFrom(from); Address[] to = new Address[mailInfo.getToAddress().split(",").length]; int i = 0; for (String e : mailInfo.getToAddress().split(",")) to[i++] = new InternetAddress(e); mailMessage.setRecipients(Message.RecipientType.TO, to); mailMessage.setSubject(mailInfo.getSubject()); mailMessage.setSentDate(new Date()); Multipart mainPart = new MimeMultipart(); BodyPart html = new MimeBodyPart(); html.setContent(mailInfo.getContent(), "text/html; charset=UTF-8"); mainPart.addBodyPart(html); if (mailInfo.getImagesMap() != null && !mailInfo.getImagesMap().isEmpty()) { for (Entry<String, String> entry : mailInfo.getImagesMap().entrySet()) { MimeBodyPart mbp = new MimeBodyPart(); File file = new File(entry.getValue()); FileDataSource fds = new FileDataSource(file); mbp.setDataHandler(new DataHandler(fds)); try { mbp.setFileName(MimeUtility.encodeWord(entry.getKey(), "UTF-8", null)); } catch (Exception e) { e.printStackTrace(); } mbp.setContentID(entry.getKey()); mbp.setHeader("Content-ID", "<image>"); mainPart.addBodyPart(mbp); } } List<File> list = mailInfo.getFileList(); if (list != null && list.size() > 0) { for (File f : list) { MimeBodyPart mbp = new MimeBodyPart(); FileDataSource fds = new FileDataSource(f.getAbsolutePath()); mbp.setDataHandler(new DataHandler(fds)); mbp.setFileName(f.getName()); mainPart.addBodyPart(mbp); } list.clear(); } mailMessage.setContent(mainPart); // mailMessage.saveChanges(); Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } } return false; }
From source file:org.talend.dataprep.api.service.mail.MailFeedbackSender.java
@Override public void send(String subject, String body, String sender) { try {/*from w ww . j a v a 2s.c o m*/ final String recipientList = StringUtils.join((new HashSet<>(Arrays.asList(recipients))).toArray(), ','); subject = subjectPrefix + subject; body = bodyPrefix + "<br/>" + body + "<br/>" + bodySuffix; InternetAddress from = new InternetAddress(this.sender); InternetAddress replyTo = new InternetAddress(sender); Properties p = new Properties(); p.put("mail.smtp.host", smtpHost); p.put("mail.smtp.port", smtpPort); p.put("mail.smtp.starttls.enable", "true"); p.put("mail.smtp.auth", "true"); MailAuthenticator authenticator = new MailAuthenticator(userName, password); Session sendMailSession = Session.getInstance(p, authenticator); MimeMessage msg = new MimeMessage(sendMailSession); msg.setFrom(from); msg.setReplyTo(new Address[] { replyTo }); msg.addRecipients(Message.RecipientType.TO, recipientList); msg.setSubject(subject, "UTF-8"); msg.setSentDate(new Date()); Multipart mainPart = new MimeMultipart(); BodyPart html = new MimeBodyPart(); html.setContent(body, "text/html; charset=utf-8"); mainPart.addBodyPart(html); msg.setContent(mainPart); Transport.send(msg); LOGGER.debug("Sending mail:'{}' to '{}'", subject, recipients); } catch (Exception e) { throw new TDPException(APIErrorCodes.UNABLE_TO_SEND_MAIL, e); } }
From source file:org.tizzit.util.mail.MailHelper.java
/** * Send an email in html-format in iso-8859-1-encoding * // ww w. j a v a 2 s. c o m * @param subject the subject of the new mail * @param htmlMessage the content of the mail as html * @param alternativeTextMessage the content of the mail as text * @param from the sender-address * @param to the receiver-address * @param cc the address of the receiver of a copy of this mail * @param bcc the address of the receiver of a blind-copy of this mail */ public static void sendHtmlMail(String subject, String htmlMessage, String alternativeTextMessage, String from, String to, String cc, String bcc) { try { MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(from)); if (cc != null && !cc.equals("")) msg.setRecipients(Message.RecipientType.CC, cc); if (bcc != null && !bcc.equals("")) msg.setRecipients(Message.RecipientType.BCC, bcc); msg.addRecipients(Message.RecipientType.TO, to); msg.setSubject(subject, "iso-8859-1"); msg.setSentDate(new Date()); MimeMultipart multiPart = new MimeMultipart(); BodyPart bodyPart = new MimeBodyPart(); bodyPart.setText(alternativeTextMessage); multiPart.addBodyPart(bodyPart); bodyPart = new MimeBodyPart(); bodyPart.setContent(htmlMessage, "text/html"); multiPart.addBodyPart(bodyPart); multiPart.setSubType("alternative"); msg.setContent(multiPart); Transport.send(msg); } catch (Exception e) { log.error("Error sending html-mail: " + e.getLocalizedMessage()); } }
From source file:org.wf.dp.dniprorada.util.MailOld.java
public MailOld _PartHTML() throws MessagingException, EmailException { // init(); log.info("_PartHTML"); //oMultiPartEmail.setMsg("0"); MimeMultipart oMimeMultipart = new MimeMultipart("related"); BodyPart oBodyPart = new MimeBodyPart(); oBodyPart.setContent(getBody(), "text/html; charset=\"utf-8\""); oMimeMultipart.addBodyPart(oBodyPart); oMultiPartEmail.setContent(oMimeMultipart); log.info("getBody()=" + getBody()); return this; }
From source file:org.wf.dp.dniprorada.util.MailOld.java
public MailOld _Part(DataSource oDataSource) throws MessagingException, EmailException { // init(); log.info("_Part"); MimeMultipart oMimeMultipart = new MimeMultipart("related"); BodyPart oBodyPart = new MimeBodyPart(); oBodyPart.setContent(oDataSource, "application/zip"); oMimeMultipart.addBodyPart(oBodyPart); return this; }
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 {/*from www .jav 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 {// w ww .j a v a2 s. c o m 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:servlets.mailPDF.java
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {/* w w w . ja v a 2 s .c o m*/ System.out.println("Reached @ mailPDF_Bronze."); String pdfBase64 = request.getParameter("pdfBase64"); String reportdate = request.getParameter("reportdate"); String emailaddresses = request.getParameter("emailList"); String emailMessage = request.getParameter("emailMessage"); String filename = request.getParameter("filename"); List<String> emailList = Arrays.asList(emailaddresses.split("\\s*,\\s*")); System.out.println("pdfBase54 is --------------\n" + pdfBase64 + "\n-----------------End"); System.out.println(reportdate); System.out.println(emailaddresses); byte[] decodedBytes = decode(pdfBase64.substring(28)); MultiPartEmail email = new MultiPartEmail(); email.setSmtpPort(587); email.setDebug(false); email.setHostName("smtp.gmail.com"); email.setAuthentication("reporting@groupnp.com", "D3sign2015"); email.setTLS(true); for (String emailAddress : emailList) { email.addTo(emailAddress); } if (filename.equals("LNIndia Tracker (Gold - Weekly)")) { email.setFrom("reporting@groupnp.com"); email.setSubject(filename + " (" + reportdate + ")"); MimeMultipart part1 = new MimeMultipart(); BodyPart messageBodyPart1 = new MimeBodyPart(); messageBodyPart1.setContent(emailMessage, "text/html; charset=utf-8"); part1.addBodyPart(messageBodyPart1); email.addPart(part1); MimeMultipart part2 = new MimeMultipart(); BodyPart messageBodyPart2 = new MimeBodyPart(); messageBodyPart2 .setDataHandler(new DataHandler(new ByteArrayDataSource(decodedBytes, "application/pdf"))); messageBodyPart2.removeHeader("Content-Transfer-Encoding"); messageBodyPart2.addHeader("Content-Transfer-Encoding", "base64"); messageBodyPart2.setFileName(filename + " " + reportdate + ".pdf"); part2.addBodyPart(messageBodyPart2); email.addPart(part2); email.send(); } else if (filename.equals("LNIndia Tracker (Gold - Monthly)")) { email.setFrom("reporting@groupnp.com"); email.setSubject(filename + " (" + reportdate + ")"); MimeMultipart part1 = new MimeMultipart(); BodyPart messageBodyPart1 = new MimeBodyPart(); messageBodyPart1.setContent(emailMessage, "text/html; charset=utf-8"); part1.addBodyPart(messageBodyPart1); email.addPart(part1); MimeMultipart part2 = new MimeMultipart(); BodyPart messageBodyPart2 = new MimeBodyPart(); messageBodyPart2 .setDataHandler(new DataHandler(new ByteArrayDataSource(decodedBytes, "application/pdf"))); messageBodyPart2.removeHeader("Content-Transfer-Encoding"); messageBodyPart2.addHeader("Content-Transfer-Encoding", "base64"); messageBodyPart2.setFileName(filename + " " + reportdate + ".pdf"); part2.addBodyPart(messageBodyPart2); email.addPart(part2); email.send(); } } catch (EmailException | MessagingException ex) { Logger.getLogger(mailPDF.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:servlets.mailPDF_Bronze.java
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {//from w w w . ja v a2 s . c o m System.out.println("Reached @ mailPDF_Bronze."); String pdfBase64 = request.getParameter("pdfBase64"); String reportdate = request.getParameter("reportdate"); String emailaddresses = request.getParameter("emailList"); String emailMessage = request.getParameter("emailMessage"); List<String> emailList = Arrays.asList(emailaddresses.split("\\s*,\\s*")); System.out.println("pdfBase54 is --------------\n" + pdfBase64 + "\n-----------------End"); System.out.println(reportdate); System.out.println(emailaddresses); byte[] decodedBytes = decode(pdfBase64.substring(28)); MultiPartEmail email = new MultiPartEmail(); email.setSmtpPort(587); email.setDebug(false); email.setHostName("smtp.gmail.com"); email.setAuthentication("reporting@groupnp.com", "D3sign2015"); email.setTLS(true); for (String emailAddress : emailList) { email.addTo(emailAddress); } email.setFrom("reporting@groupnp.com"); email.setSubject("LexisNexis Risk India Tracker (" + reportdate + ")"); MimeMultipart part1 = new MimeMultipart(); BodyPart messageBodyPart1 = new MimeBodyPart(); messageBodyPart1.setContent(emailMessage, "text/html; charset=utf-8"); part1.addBodyPart(messageBodyPart1); email.addPart(part1); MimeMultipart part2 = new MimeMultipart(); BodyPart messageBodyPart2 = new MimeBodyPart(); messageBodyPart2 .setDataHandler(new DataHandler(new ByteArrayDataSource(decodedBytes, "application/pdf"))); messageBodyPart2.removeHeader("Content-Transfer-Encoding"); messageBodyPart2.addHeader("Content-Transfer-Encoding", "base64"); messageBodyPart2.setFileName("LNIndiaTracker_" + reportdate + ".pdf"); part2.addBodyPart(messageBodyPart2); email.addPart(part2); email.send(); } catch (EmailException | MessagingException ex) { Logger.getLogger(mailPDF_Bronze.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:servlets.mailPDF_Bronze_ACS.java
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {//from ww w. jav a2 s . com System.out.println("Reached @ mailPDF_Bronze."); String pdfBase64 = request.getParameter("pdfBase64"); String reportdate = request.getParameter("reportdate"); String emailaddresses = request.getParameter("emailList"); String emailMessage = request.getParameter("emailMessage"); List<String> emailList = Arrays.asList(emailaddresses.split("\\s*,\\s*")); System.out.println("pdfBase54 is --------------\n" + pdfBase64 + "\n-----------------End"); System.out.println(reportdate); System.out.println(emailaddresses); byte[] decodedBytes = decode(pdfBase64.substring(28)); MultiPartEmail email = new MultiPartEmail(); email.setSmtpPort(587); email.setDebug(false); email.setHostName("smtp.gmail.com"); email.setAuthentication("reporting@groupnp.com", "D3sign2015"); email.setTLS(true); for (String emailAddress : emailList) { email.addTo(emailAddress); } email.setFrom("reporting@groupnp.com"); email.setSubject("ACS Tracker (" + reportdate + ")"); MimeMultipart part1 = new MimeMultipart(); BodyPart messageBodyPart1 = new MimeBodyPart(); messageBodyPart1.setContent(emailMessage, "text/html; charset=utf-8"); part1.addBodyPart(messageBodyPart1); email.addPart(part1); MimeMultipart part2 = new MimeMultipart(); BodyPart messageBodyPart2 = new MimeBodyPart(); messageBodyPart2 .setDataHandler(new DataHandler(new ByteArrayDataSource(decodedBytes, "application/pdf"))); messageBodyPart2.removeHeader("Content-Transfer-Encoding"); messageBodyPart2.addHeader("Content-Transfer-Encoding", "base64"); messageBodyPart2.setFileName("ACSTracker_" + reportdate + ".pdf"); part2.addBodyPart(messageBodyPart2); email.addPart(part2); email.send(); } catch (EmailException | MessagingException ex) { Logger.getLogger(mailPDF_Bronze_ACS.class.getName()).log(Level.SEVERE, null, ex); } }