List of usage examples for javax.mail.internet MimeBodyPart MimeBodyPart
public MimeBodyPart()
From source file:com.jwm123.loggly.reporter.ReportMailer.java
public MimeBodyPart addReportAttachment() throws MessagingException { MimeBodyPart mimeBodyPart = new MimeBodyPart(); String mimeType = "application/vnd.ms-excel"; if (fileName.endsWith(".xlsx")) { mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; }//from www . j ava 2 s . co m mimeBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(reportContent, mimeType))); mimeBodyPart.setDisposition("attachment"); mimeBodyPart.setFileName(fileName); return mimeBodyPart; }
From source file:gov.nih.nci.caarray.util.EmailUtil.java
private static void addBodyPart(Multipart mp, String content, String contentType) throws MessagingException { BodyPart bp = new MimeBodyPart(); bp.setContent(content, contentType); mp.addBodyPart(bp);//from www. j a v a 2s . c o m }
From source file:org.openmrs.notification.mail.MailMessageSender.java
/** * Creates a MimeMultipart, so that we can have an attachment. * * @param message//from w ww. j a v a 2s .c o m * @return */ private MimeMultipart createMultipart(Message message) throws Exception { MimeMultipart toReturn = new MimeMultipart(); MimeBodyPart textContent = new MimeBodyPart(); textContent.setContent(message.getContent(), message.getContentType()); MimeBodyPart attachment = new MimeBodyPart(); attachment.setContent(message.getAttachment(), message.getAttachmentContentType()); attachment.setFileName(message.getAttachmentFileName()); toReturn.addBodyPart(textContent); toReturn.addBodyPart(attachment); return toReturn; }
From source file:org.ambraproject.wombat.service.FreemarkerMailServiceImpl.java
private BodyPart createBodyPart(ContentType contentType, Template htmlTemplate, Model context) throws IOException, MessagingException { BodyPart htmlPage = new MimeBodyPart(); String encoding = getConfiguration().getDefaultEncoding(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(0x100); Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, encoding)); htmlTemplate.setOutputEncoding(encoding); htmlTemplate.setEncoding(encoding);// ww w .ja v a 2s .c o m try { htmlTemplate.process(context, writer); } catch (TemplateException e) { throw new MailPreparationException("Can't generate " + contentType.getMimeType() + " subscription mail", e); } htmlPage.setDataHandler(createBodyPartDataHandler(outputStream.toByteArray(), contentType.toString() + "; charset=" + getConfiguration().getDefaultEncoding())); return htmlPage; }
From source file:be.fedict.eid.pkira.blm.model.mail.MailHandlerBean.java
@Override @TransactionAttribute(TransactionAttributeType.REQUIRED) public void onMessage(Message arg0) { ObjectMessage objectMessage = (ObjectMessage) arg0; try {//from w w w . j av a2 s . c o m Mail mail = (Mail) objectMessage.getObject(); // Get properties String mailProtocol = getMailProtocol(); String mailServer = getSmtpServer(); String mailUser = getSmtpUser(); String mailPort = getSmtpPort(); String mailPassword = getSmtpPassword(); // Initialize a mail session Properties props = new Properties(); props.put("mail." + mailProtocol + ".host", mailServer); props.put("mail." + mailProtocol + ".port", mailPort); Session session = Session.getInstance(props); // Create the message MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(mail.getSender())); for (String recipient : mail.getRecipients()) { msg.addRecipient(RecipientType.TO, new InternetAddress(recipient)); } msg.setSubject(mail.getSubject(), "UTF-8"); Multipart multipart = new MimeMultipart(); msg.setContent(multipart); // Set the email message text and attachment MimeBodyPart messagePart = new MimeBodyPart(); messagePart.setContent(mail.getBody(), mail.getContentType()); multipart.addBodyPart(messagePart); if (mail.getAttachmentData() != null) { ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource(mail.getAttachmentData(), mail.getAttachmentContentType()); DataHandler dataHandler = new DataHandler(byteArrayDataSource); MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.setDataHandler(dataHandler); attachmentPart.setFileName(mail.getAttachmentFileName()); multipart.addBodyPart(attachmentPart); } // Open transport and send message Transport transport = session.getTransport(mailProtocol); if (StringUtils.isNotBlank(mailUser)) { transport.connect(mailUser, mailPassword); } else { transport.connect(); } msg.saveChanges(); transport.sendMessage(msg, msg.getAllRecipients()); } catch (JMSException e) { errorLogger.logError(ApplicationComponent.MAIL, "Cannot handle the object message from the queue", e); throw new RuntimeException(e); } catch (MessagingException e) { errorLogger.logError(ApplicationComponent.MAIL, "Cannot send a mail message", e); throw new RuntimeException(e); } }
From source file:com.mgmtp.jfunk.core.reporting.EmailReporter.java
private void sendMessage(final String content) { try {/*from w w w. j ava2 s. com*/ MimeMessage msg = new MimeMessage(sessionProvider.get()); msg.setSubject("jFunk E-mail Report"); msg.addRecipients(Message.RecipientType.TO, recipientsProvider.get()); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(content, "text/html; charset=UTF-8"); MimeMultipart multipart = new MimeMultipart("related"); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(getClass().getResource("check.gif"))); messageBodyPart.setHeader("Content-ID", "<check>"); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); messageBodyPart.setDataHandler(new DataHandler(getClass().getResource("error.gif"))); messageBodyPart.setHeader("Content-ID", "<error>"); multipart.addBodyPart(messageBodyPart); msg.setContent(multipart); smtpClientProvider.get().send(msg); int anzahlRecipients = msg.getAllRecipients().length; log.info( "Report e-mail was sent to " + anzahlRecipients + " recipient(s): " + recipientsProvider.get()); } catch (MessagingException e) { log.error("Error while creating report e-mail", e); } catch (MailException e) { log.error("Error while sending report e-mail", e); } }
From source file:com.threewks.thundr.mail.JavaMailMailer.java
private void addBody(Multipart multipart, String content, String contentType) throws MessagingException { BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(content, contentType); multipart.addBodyPart(messageBodyPart); }
From source file:de.betterform.connector.serializer.MultipartRelatedSerializer.java
protected void visitNode(Map cache, Node node, MimeMultipart multipart) throws Exception { ModelItem item = (ModelItem) node.getUserData(""); if (item != null && item.getDeclarationView().getDatatype() != null && item.getDeclarationView().getDatatype().equalsIgnoreCase("anyURI")) { String name = item.getFilename(); if (name == null || item.getValue() == null || item.getValue().equals("")) { return; }//from ww w.j a v a 2 s. c o m String cid = (String) cache.get(name); if (cid == null) { int count = multipart.getCount(); cid = name + "@part" + (count + 1); MimeBodyPart part = new MimeBodyPart(); part.setContentID("<" + cid + ">"); DataHandler dh = new DataHandler(new ModelItemDataSource(item)); part.setDataHandler(dh); part.addHeader("Content-Type", item.getMediatype()); part.addHeader("Content-Transfer-Encoding", "base64"); part.setDisposition("attachment"); part.setFileName(name); multipart.addBodyPart(part); cache.put(name, cid); } Element element = (Element) node; // remove text node NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); if (n.getNodeType() != Node.TEXT_NODE) { continue; } n.setNodeValue("cid:" + cid); break; } } else { NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { visitNode(cache, n, multipart); } } } }
From source file:com.formkiq.core.service.notification.ExternalMailSender.java
/** * Send Reset Email.//from www. j ava2 s . com * @param to {@link String} * @param email {@link String} * @param subject {@link String} * @param text {@link String} * @param html {@link String} * @throws MessagingException MessagingException */ private void sendResetEmail(final String to, final String email, final String subject, final String text, final String html) throws MessagingException { String hostname = this.systemProperties.getSystemHostname(); String resetToken = this.userservice.generateResetToken(to); StringSubstitutor s = new StringSubstitutor( ImmutableMap.of("hostname", hostname, "to", to, "email", email, "resettoken", resetToken)); MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(s.replace(text), "UTF-8"); s.replace(html); MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(s.replace(html), "text/html;charset=UTF-8"); final Multipart mp = new MimeMultipart("alternative"); mp.addBodyPart(textPart); mp.addBodyPart(htmlPart); MimeMessage msg = this.mail.createMimeMessage(); msg.setContent(mp); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); msg.setSubject(subject); this.mail.send(msg); }
From source file:ee.cyber.licensing.service.MailService.java
public void generateAndSendMail(MailBody mailbody, int licenseId, int fileId) throws MessagingException, IOException, SQLException { License license = licenseRepository.findById(licenseId); List<Contact> contacts = contactRepository.findAll(license.getCustomer()); List<String> receivers = getReceivers(mailbody, contacts); logger.info("1st ===> setup Mail Server Properties"); Properties mailServerProperties = getProperties(); final String email = mailServerProperties.getProperty("fromEmail"); final String password = mailServerProperties.getProperty("password"); final String host = mailServerProperties.getProperty("mail.smtp.host"); logger.info("2nd ===> create Authenticator object to pass in Session.getInstance argument"); Authenticator authentication = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(email, password); }/*from www. ja v a2 s. c o m*/ }; logger.info("Mail Server Properties have been setup successfully"); logger.info("3rd ===> get Mail Session.."); Session getMailSession = Session.getInstance(mailServerProperties, authentication); logger.info("4th ===> generateAndSendEmail() starts"); MimeMessage mailMessage = new MimeMessage(getMailSession); mailMessage.addHeader("Content-type", "text/html; charset=UTF-8"); mailMessage.addHeader("format", "flowed"); mailMessage.addHeader("Content-Transfer-Encoding", "8bit"); mailMessage.setFrom(new InternetAddress(email, "License dude")); //mailMessage.setReplyTo(InternetAddress.parse(email, false)); mailMessage.setSubject(mailbody.getSubject()); //String emailBody = body + "<br><br> Regards, <br>Cybernetica team"; mailMessage.setSentDate(new Date()); for (String receiver : receivers) { mailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(receiver)); } if (fileId != 0) { MailAttachment file = fileRepository.findById(fileId); if (file != null) { String fileName = file.getFileName(); byte[] fileData = file.getData_b(); if (fileName != null) { // Create a multipart message for attachment Multipart multipart = new MimeMultipart(); // Body part BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(mailbody.getBody(), "text/html"); multipart.addBodyPart(messageBodyPart); //Attachment part messageBodyPart = new MimeBodyPart(); ByteArrayDataSource source = new ByteArrayDataSource(fileData, "application/octet-stream"); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(fileName); multipart.addBodyPart(messageBodyPart); mailMessage.setContent(multipart); } } } else { mailMessage.setContent(mailbody.getBody(), "text/html"); } logger.info("5th ===> Get Session"); sendMail(email, password, host, getMailSession, mailMessage); }