List of usage examples for javax.mail BodyPart setDataHandler
public void setDataHandler(DataHandler dh) throws MessagingException;
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); attachment.setFileName(name);//from ww w .j a va 2s . c o m attachment.setHeader("Content-ID", "<" + name + ">"); iBody.addBodyPart(attachment); }
From source file:servlets.mailPDF.java
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {//w w w . j av a2 s.c om 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:com.mylab.mail.OpenCmsMailService.java
public void sendMultipartMail(MessageConfig config, DataSource ds, String filename) throws MessagingException { log.debug("Sending multipart message " + config); Session session = getSession();/*from w ww .j a v a2 s . co m*/ MimeMultipart multipart = new MimeMultipart(); MimeBodyPart html = new MimeBodyPart(); html.setContent(config.getContent(), config.getContentType()); html.setHeader("MIME-Version", "1.0"); html.setHeader("Content-Type", html.getContentType()); multipart.addBodyPart(html); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(ds)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); final MimeMessage message = new MimeMessage(session); message.setContent(multipart); try { message.setFrom(new InternetAddress(config.getFrom(), config.getFromName())); addRecipientsWhitelist(message, config.getTo(), config.getToName(), config.getCardconfig()); } catch (UnsupportedEncodingException ex) { throw new MessagingException("Setting from or to failed", ex); } message.setSubject(config.getSubject()); // we don't send in a new Thread so that we get the Exception Transport.send(message); }
From source file:org.unitime.commons.Email.java
public void addAttachement(File file, String name) throws MessagingException { BodyPart attachement = new MimeBodyPart(); attachement.setDataHandler(new DataHandler(new FileDataSource(file))); attachement.setFileName(name == null ? file.getName() : name); iBody.addBodyPart(attachement);/*from w w w . ja v a 2 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. j ava2 s . c om*/ }
From source file:com.mylab.mail.OpenCmsMailService.java
public void sendMultipartMail(MessageConfig config, DataSource imageds, String image, DataSource audiods, String audio) throws MessagingException { log.debug("Sending multipart message " + config); Session session = getSession();/*w w w.jav a 2 s . com*/ MimeMultipart multipart = new MimeMultipart(); MimeBodyPart html = new MimeBodyPart(); html.setContent(config.getContent(), config.getContentType()); html.setHeader("MIME-Version", "1.0"); html.setHeader("Content-Type", html.getContentType()); multipart.addBodyPart(html); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(imageds)); messageBodyPart.setFileName(image); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(audiods)); messageBodyPart.setFileName(audio); multipart.addBodyPart(messageBodyPart); final MimeMessage message = new MimeMessage(session); message.setContent(multipart); try { message.setFrom(new InternetAddress(config.getFrom(), config.getFromName())); addRecipientsWhitelist(message, config.getTo(), config.getToName(), config.getCardconfig()); } catch (UnsupportedEncodingException ex) { throw new MessagingException("Setting from or to failed", ex); } message.setSubject(config.getSubject()); // we don't send in a new Thread so that we get the Exception Transport.send(message); }
From source file:org.unitime.commons.Email.java
public void addAttachement(final FormFile file) throws MessagingException { BodyPart attachement = new MimeBodyPart(); attachement.setDataHandler(new DataHandler(new DataSource() { @Override//from www .j a v a2 s .c om public OutputStream getOutputStream() throws IOException { throw new IOException("No output stream."); } @Override public String getName() { return file.getFileName(); } @Override public InputStream getInputStream() throws IOException { return file.getInputStream(); } @Override public String getContentType() { return file.getContentType(); } })); attachement.setFileName(file.getFileName()); iBody.addBodyPart(attachement); }
From source file:org.openmeetings.test.calendar.TestSendIcalMessage.java
private void sendIcalMessage() throws Exception { log.debug("sendIcalMessage"); // Evaluating Configuration Data String smtpServer = cfgManagement.getConfKey(3, "smtp_server").getConf_value(); String smtpPort = cfgManagement.getConfKey(3, "smtp_port").getConf_value(); // String from = "openmeetings@xmlcrm.org"; String from = cfgManagement.getConfKey(3, "system_email_addr").getConf_value(); String emailUsername = cfgManagement.getConfKey(3, "email_username").getConf_value(); String emailUserpass = cfgManagement.getConfKey(3, "email_userpass").getConf_value(); Properties props = System.getProperties(); props.put("mail.smtp.host", smtpServer); props.put("mail.smtp.port", smtpPort); Configuration conf = cfgManagement.getConfKey(3, "mail.smtp.starttls.enable"); if (conf != null) { if (conf.getConf_value().equals("1")) { props.put("mail.smtp.starttls.enable", "true"); }//from w ww . j av a 2s. com } // Check for Authentification Session session = null; if (emailUsername != null && emailUsername.length() > 0 && emailUserpass != null && emailUserpass.length() > 0) { // use SMTP Authentication props.put("mail.smtp.auth", "true"); session = Session.getDefaultInstance(props, new SmtpAuthenticator(emailUsername, emailUserpass)); } else { // not use SMTP Authentication session = Session.getDefaultInstance(props, null); } // Building MimeMessage MimeMessage mimeMessage = new MimeMessage(session); mimeMessage.setSubject(subject); mimeMessage.setFrom(new InternetAddress(from)); mimeMessage.addRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients, false)); // -- Create a new message -- BodyPart msg = new MimeBodyPart(); msg.setDataHandler(new DataHandler(new ByteArrayDataSource(htmlBody, "text/html; charset=\"utf-8\""))); Multipart multipart = new MimeMultipart(); BodyPart iCalAttachment = new MimeBodyPart(); iCalAttachment.setDataHandler(new DataHandler(new javax.mail.util.ByteArrayDataSource( new ByteArrayInputStream(iCalMimeBody), "text/calendar;method=REQUEST;charset=\"UTF-8\""))); iCalAttachment.setFileName("invite.ics"); multipart.addBodyPart(iCalAttachment); multipart.addBodyPart(msg); mimeMessage.setSentDate(new Date()); mimeMessage.setContent(multipart); // -- Set some other header information -- // mimeMessage.setHeader("X-Mailer", "XML-Mail"); // mimeMessage.setSentDate(new Date()); // Transport trans = session.getTransport("smtp"); Transport.send(mimeMessage); }
From source file:org.linagora.linshare.core.service.impl.MailNotifierServiceImpl.java
@Override public void sendNotification(String smtpSender, String replyTo, String recipient, String subject, String htmlContent, String inReplyTo, String references) throws SendFailedException { if (smtpServer.equals("")) { logger.warn("Mail notifications are disabled."); return;//from www . j a v a 2s .c o m } // get the mail session Session session = getMailSession(); // Define message MimeMessage messageMim = new MimeMessage(session); try { messageMim.setFrom(new InternetAddress(smtpSender)); if (replyTo != null) { InternetAddress reply[] = new InternetAddress[1]; reply[0] = new InternetAddress(replyTo); messageMim.setReplyTo(reply); } messageMim.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(recipient)); if (inReplyTo != null && inReplyTo != "") { // This field should contain only ASCCI character (RFC 822) if (isPureAscii(inReplyTo)) { messageMim.setHeader("In-Reply-To", inReplyTo); } } if (references != null && references != "") { // This field should contain only ASCCI character (RFC 822) if (isPureAscii(references)) { messageMim.setHeader("References", references); } } messageMim.setSubject(subject, charset); // Create a "related" Multipart message // content type is multipart/alternative // it will contain two part BodyPart 1 and 2 Multipart mp = new MimeMultipart("alternative"); // BodyPart 2 // content type is multipart/related // A multipart/related is used to indicate that message parts should // not be considered individually but rather // as parts of an aggregate whole. The message consists of a root // part (by default, the first) which reference other parts inline, // which may in turn reference other parts. Multipart html_mp = new MimeMultipart("related"); // Include an HTML message with images. // BodyParts: the HTML file and an image // Get the HTML file BodyPart rel_bph = new MimeBodyPart(); rel_bph.setDataHandler( new DataHandler(new ByteArrayDataSource(htmlContent, "text/html; charset=" + charset))); html_mp.addBodyPart(rel_bph); // Create the second BodyPart of the multipart/alternative, // set its content to the html multipart, and add the // second bodypart to the main multipart. BodyPart alt_bp2 = new MimeBodyPart(); alt_bp2.setContent(html_mp); mp.addBodyPart(alt_bp2); messageMim.setContent(mp); // RFC 822 "Date" header field // Indicates that the message is complete and ready for delivery messageMim.setSentDate(new GregorianCalendar().getTime()); // Since we used html tags, the content must be marker as text/html // messageMim.setContent(content,"text/html; charset="+charset); Transport tr = session.getTransport("smtp"); // Connect to smtp server, if needed if (needsAuth) { tr.connect(smtpServer, smtpPort, smtpUser, smtpPassword); messageMim.saveChanges(); tr.sendMessage(messageMim, messageMim.getAllRecipients()); tr.close(); } else { // Send message Transport.send(messageMim); } } catch (SendFailedException e) { logger.error("Error sending notification on " + smtpServer + " port " + smtpPort + " to " + recipient, e); throw e; } catch (MessagingException e) { logger.error("Error sending notification on " + smtpServer + " port " + smtpPort, e); throw new TechnicalException(TechnicalErrorCode.MAIL_EXCEPTION, "Error sending notification", e); } catch (Exception e) { logger.error("Error sending notification on " + smtpServer + " port " + smtpPort, e); throw new TechnicalException(TechnicalErrorCode.MAIL_EXCEPTION, "Error sending notification", e); } }
From source file:bioLockJ.module.agent.MailAgent.java
private BodyPart getAttachment(final String filePath) throws Exception { try {/*from w ww . j av a 2s. c o m*/ final DataSource source = new FileDataSource(filePath); final File logFile = new File(filePath); final double fileSize = logFile.length() / 1000000; if (fileSize < emailMaxAttachmentMB) { final BodyPart attachPart = new MimeBodyPart(); attachPart.setDataHandler(new DataHandler(source)); attachPart.setFileName(filePath.substring(filePath.lastIndexOf(File.separator) + 1)); return attachPart; } else { Log.out.warn("File [" + filePath + "] too large to attach. Max file size configured in prop file set to = " + emailMaxAttachmentMB + " MB"); } } catch (final Exception ex) { Log.out.error("Unable to attach file", ex); } return null; }