List of usage examples for javax.mail.internet MimeMultipart MimeMultipart
public MimeMultipart(DataSource ds) throws MessagingException
From source file:com.threewks.thundr.mail.JavaMailMailer.java
@Override protected void sendInternal(Map.Entry<String, String> from, Map.Entry<String, String> replyTo, Map<String, String> to, Map<String, String> cc, Map<String, String> bcc, String subject, Object body, List<Attachment> attachments) { try {//from w ww. j a v a 2s . com Session emailSession = getSession(); Message message = new MimeMessage(emailSession); message.setFrom(emailAddress(from)); if (replyTo != null) { message.setReplyTo(new Address[] { emailAddress(replyTo) }); } message.setSubject(subject); BasicViewRenderer viewRenderer = render(body); String content = viewRenderer.getOutputAsString(); String contentType = ContentType.cleanContentType(viewRenderer.getContentType()); contentType = StringUtils.isBlank(contentType) ? ContentType.TextHtml.value() : contentType; if (Expressive.isEmpty(attachments)) { message.setContent(content, contentType); } else { Multipart multipart = new MimeMultipart("mixed"); // subtype must be "mixed" or inline & regular attachments won't play well together addBody(multipart, content, contentType); addAttachments(multipart, attachments); message.setContent(multipart); } addRecipients(to, message, RecipientType.TO); addRecipients(cc, message, RecipientType.CC); addRecipients(bcc, message, RecipientType.BCC); sendMessage(message); } catch (MessagingException e) { throw new MailException(e, "Failed to send an email: %s", e.getMessage()); } }
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:com.jwm123.loggly.reporter.ReportMailer.java
public void send() throws MessagingException { if (StringUtils.isNotBlank(config.getMailServer()) && recipients.length > 0) { Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.port", config.getMailPort().toString()); props.setProperty("mail.smtp.host", config.getMailServer()); if (StringUtils.isNotBlank(config.getMailUsername()) && StringUtils.isNotBlank(config.getMailPassword())) { props.setProperty("mail.smtp.user", config.getMailUsername()); props.setProperty("mail.smtp.password", config.getMailPassword()); }/*from ww w.j ava 2 s. co m*/ Session session = Session.getDefaultInstance(props); MimeMessage message = new MimeMessage(session); message.addFrom(new Address[] { new InternetAddress(config.getMailFrom()) }); message.setSubject(subject); for (String recipient : recipients) { message.addRecipient(RecipientType.TO, new InternetAddress(recipient)); } MimeMultipart containingMultipart = new MimeMultipart("mixed"); MimeMultipart messageMultipart = new MimeMultipart("alternative"); containingMultipart.addBodyPart(newMultipartBodyPart(messageMultipart)); messageMultipart.addBodyPart(newTextBodyPart(getText())); MimeMultipart htmlMultipart = new MimeMultipart("related"); htmlMultipart.addBodyPart(newHtmlBodyPart(getHtml())); messageMultipart.addBodyPart(newMultipartBodyPart(htmlMultipart)); containingMultipart.addBodyPart(addReportAttachment()); message.setContent(containingMultipart); Transport.send(message); } }
From source file:org.igov.io.mail.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:it.ozimov.springboot.templating.mail.service.EmailServiceImpl.java
public MimeMessage send(final @NonNull Email email, final @NonNull String template, final Map<String, Object> modelObject, final @NonNull InlinePicture... inlinePictures) throws CannotSendEmailException { email.setSentAt(new Date()); final MimeMessage mimeMessage = toMimeMessage(email); try {/*w w w .ja va 2 s . c o m*/ final MimeMultipart content = new MimeMultipart("related"); String text = templateService.mergeTemplateIntoString(template, fromNullable(modelObject).or(ImmutableMap.of())); for (final InlinePicture inlinePicture : inlinePictures) { final String cid = UUID.randomUUID().toString(); //Set the cid in the template text = text.replace(inlinePicture.getTemplateName(), "cid:" + cid); //Set the image part final MimeBodyPart imagePart = new MimeBodyPart(); imagePart.attachFile(inlinePicture.getFile()); imagePart.setContentID('<' + cid + '>'); imagePart.setDisposition(MimeBodyPart.INLINE); imagePart.setHeader("Content-Type", inlinePicture.getImageType().getContentType()); content.addBodyPart(imagePart); } //Set the HTML text part final MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(text, email.getEncoding().displayName(), "html"); content.addBodyPart(textPart); mimeMessage.setContent(content); javaMailSender.send(mimeMessage); } catch (IOException e) { log.error("The template file cannot be read", e); throw new CannotSendEmailException( "Error while sending the email due to problems with the template file", e); } catch (TemplateException e) { log.error("The template file cannot be processed", e); throw new CannotSendEmailException( "Error while processing the template file with the given model object", e); } catch (MessagingException e) { log.error("The mime message cannot be created", e); throw new CannotSendEmailException( "Error while sending the email due to problems with the mime content", e); } return mimeMessage; }
From source file:org.nuxeo.ecm.automation.server.jaxrs.io.MultiPartRequestReader.java
@Override public ExecutionRequest readFrom(Class<ExecutionRequest> arg0, Type arg1, Annotation[] arg2, MediaType arg3, MultivaluedMap<String, String> headers, InputStream in) throws IOException, WebApplicationException { ExecutionRequest req = null;// www . j a v a 2s . c om try { List<String> ctypes = headers.get("Content-Type"); String ctype = ctypes.get(0); // we need to copy first the stream into a file otherwise it may // happen that // javax.mail fail to receive some parts - I am not sure why - // perhaps the stream is no more available when javax.mail need it? File tmp = File.createTempFile("nx-automation-mp-upload-", ".tmp"); FileUtils.copyToFile(in, tmp); in = new SharedFileInputStream(tmp); // get the input from the saved // file try { MimeMultipart mp = new MimeMultipart(new InputStreamDataSource(in, ctype)); BodyPart part = mp.getBodyPart(0); // use content ids InputStream pin = part.getInputStream(); req = JsonRequestReader.readRequest(pin, headers, getCoreSession()); int cnt = mp.getCount(); if (cnt == 2) { // a blob req.setInput(readBlob(request, mp.getBodyPart(1))); } else if (cnt > 2) { // a blob list BlobList blobs = new BlobList(); for (int i = 1; i < cnt; i++) { blobs.add(readBlob(request, mp.getBodyPart(i))); } req.setInput(blobs); } else { log.error("Not all parts received."); for (int i = 0; i < cnt; i++) { log.error("Received parts: " + mp.getBodyPart(i).getHeader("Content-ID")[0] + " -> " + mp.getBodyPart(i).getContentType()); } throw ExceptionHandler.newException( new IllegalStateException("Received only " + cnt + " part in a multipart request")); } } finally { tmp.delete(); } } catch (Throwable e) { throw ExceptionHandler.newException("Failed to parse multipart request", e); } return req; }
From source file:com.warsaw.data.controller.LoginController.java
private Message buildEmail(Session session, String to) throws Exception { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(EMAIL)); // Set To: header field of the header. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); // Set Subject: header field message.setSubject("Testing Subject"); // This mail has 2 part, the BODY and the embedded image MimeMultipart multipart = new MimeMultipart("related"); // first part (the html) BodyPart messageBodyPart = new MimeBodyPart(); String htmlText = "<img style='width:800px' src=\"cid:image1\"><br/>" + "Dzie dobry,<br/><br/>" + "witamy na portalu TrasyPoWarszawsku.pl, na ktrym zostalo " + "zaoone konto<br/> dla osoby o danych: Jan Marian Ptak. W celu zakoczenia procesu tworzenia " + "konta prosimy uy linku aktywacyjnego:<br/><br/>" + "<a href='https://test.puesc.gov.pl?link=Gkhh&%JK.'>https://test.puesc.gov.pl?link=Gkhh&%JK.</a><br/><br/>" + "Na wywietlonym ekranie prosz wprowadzi zdefiniowane przez siebie haso awaryjne. Po prawidowym" + " wprowadzeniu danych<br/> oraz ustawieniu nowego hasa dostpowego konto na portalu PUESC zostanie aktywowane.<br/>" + "Link aktywacyjny pozostanie wany przez 24 godziny od momentu otrzymania niniejszej wiadomoci.<br/><br/>" + "<img style='width:800px' src=\"cid:image2\"><br/><br/>" + "Z powaaniem<br/><br/>" + "Zesp portalu PUESC<br/>" + "puesc@mofnet.gov.pl<br/>" + "<a href='http://puesc.gov.pl'>http://puesc.gov.pl</a>"; messageBodyPart.setContent(htmlText, "text/html; charset=ISO-8859-2"); // add it/*from ww w.j ava 2s.c om*/ multipart.addBodyPart(messageBodyPart); // second part (the image) messageBodyPart = new MimeBodyPart(); DataSource fds = new FileDataSource("C:\\Users\\Pawel\\Desktop\\header.png"); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setHeader("Content-ID", "<image1>"); // add image to the multipart multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); // URL url = new URL("http://ns3342351.ovh.net:8080/seap_lf_graphicsLayout_theme/images/refresh.png"); // URLDataSource fds1 =new URLDataSource(url); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setHeader("Content-ID", "<image2>"); multipart.addBodyPart(messageBodyPart); // put everything together message.setContent(multipart); ByteArrayOutputStream b = new ByteArrayOutputStream(); try { message.writeTo(b); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return message; }
From source file:org.nuxeo.ecm.automation.server.jaxrs.io.MultiPartFormRequestReader.java
@Override public ExecutionRequest readFrom(Class<ExecutionRequest> arg0, Type arg1, Annotation[] arg2, MediaType arg3, MultivaluedMap<String, String> headers, InputStream in) throws IOException, WebApplicationException { ExecutionRequest req = null;/* www .ja v a 2 s.c o m*/ try { List<String> ctypes = headers.get("Content-Type"); String ctype = ctypes.get(0); // we need to copy first the stream into a file otherwise it may // happen that // javax.mail fail to receive some parts - I am not sure why - // perhaps the stream is no more available when javax.mail need it? File tmp = File.createTempFile("nx-automation-mp-upload-", ".tmp"); FileUtils.copyToFile(in, tmp); // get the input from the saved file in = new SharedFileInputStream(tmp); try { MimeMultipart mp = new MimeMultipart(new InputStreamDataSource(in, ctype)); BodyPart part = mp.getBodyPart(0); // use content ids InputStream pin = part.getInputStream(); req = JsonRequestReader.readRequest(pin, headers, getCoreSession()); int cnt = mp.getCount(); if (cnt == 2) { // a blob req.setInput(readBlob(request, mp.getBodyPart(1))); } else if (cnt > 2) { // a blob list BlobList blobs = new BlobList(); for (int i = 1; i < cnt; i++) { blobs.add(readBlob(request, mp.getBodyPart(i))); } req.setInput(blobs); } else { log.error("Not all parts received."); for (int i = 0; i < cnt; i++) { log.error("Received parts: " + mp.getBodyPart(i).getHeader("Content-ID")[0] + " -> " + mp.getBodyPart(i).getContentType()); } throw ExceptionHandler.newException( new IllegalStateException("Received only " + cnt + " part in a multipart request")); } } finally { try { in.close(); } catch (IOException e) { // do nothing } tmp.delete(); } } catch (Throwable e) { throw ExceptionHandler.newException("Failed to parse multipart request", e); } return req; }
From source file:com.intuit.tank.mail.TankMailer.java
/** * @{inheritDoc//from www. j ava 2 s.c om */ @Override public void sendMail(MailMessage message, String... emailAddresses) { MailConfig mailConfig = new TankConfig().getMailConfig(); Properties props = new Properties(); props.put("mail.smtp.host", mailConfig.getSmtpHost()); props.put("mail.smtp.port", mailConfig.getSmtpPort()); Session mailSession = Session.getDefaultInstance(props); Message simpleMessage = new MimeMessage(mailSession); InternetAddress fromAddress = null; InternetAddress toAddress = null; try { fromAddress = new InternetAddress(mailConfig.getMailFrom()); simpleMessage.setFrom(fromAddress); for (String email : emailAddresses) { try { toAddress = new InternetAddress(email); simpleMessage.addRecipient(RecipientType.TO, toAddress); } catch (AddressException e) { LOG.warn("Error with recipient " + email + ": " + e.toString()); } } simpleMessage.setSubject(message.getSubject()); final MimeBodyPart textPart = new MimeBodyPart(); textPart.setContent(message.getPlainTextBody(), "text/plain"); textPart.setHeader("MIME-Version", "1.0"); textPart.setHeader("Content-Type", textPart.getContentType()); // HTML version final MimeBodyPart htmlPart = new MimeBodyPart(); // htmlPart.setContent(message.getHtmlBody(), "text/html"); htmlPart.setDataHandler(new DataHandler(new HTMLDataSource(message.getHtmlBody()))); htmlPart.setHeader("MIME-Version", "1.0"); htmlPart.setHeader("Content-Type", "text/html"); // Create the Multipart. Add BodyParts to it. final Multipart mp = new MimeMultipart("alternative"); mp.addBodyPart(textPart); mp.addBodyPart(htmlPart); // Set Multipart as the message's content simpleMessage.setContent(mp); simpleMessage.setHeader("MIME-Version", "1.0"); simpleMessage.setHeader("Content-Type", mp.getContentType()); logMsg(mailConfig.getSmtpHost(), simpleMessage); if (simpleMessage.getRecipients(RecipientType.TO) != null && simpleMessage.getRecipients(RecipientType.TO).length > 0) { Transport.send(simpleMessage); } } catch (MessagingException e) { throw new RuntimeException(e); } }
From source file:org.ambraproject.wombat.service.FreemarkerMailServiceImpl.java
private Multipart createPartForMultipart(Template htmlTemplate, Model context, String multipartType, ContentType contentType) throws IOException, MessagingException { Multipart multipart = new MimeMultipart(multipartType); multipart.addBodyPart(createBodyPart(contentType, htmlTemplate, context)); return multipart; }