List of usage examples for javax.mail.internet MimeBodyPart MimeBodyPart
public MimeBodyPart()
From source file:com.anteam.alert.email.service.EmailAntlert.java
@Override public boolean send(AntlertMessage antlertMsg) { MimeMessage mimeMsg; // MIME // from?to?/*from w w w . j av a2s .c o m*/ mimeMsg = new javax.mail.internet.MimeMessage(mailSession); try { // ? mimeMsg.setFrom(sender); // mimeMsg.setRecipients(RecipientType.TO, receivers); // mimeMsg.setSubject(antlertMsg.getTitle(), CHARSET); // MimeBodyPart messageBody = new MimeBodyPart(); messageBody.setContent(antlertMsg.getContent(), CONTENT_MIME_TYPE); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBody); mimeMsg.setContent(multipart); // ?? mimeMsg.setSentDate(new Date()); mimeMsg.saveChanges(); // ?? Transport.send(mimeMsg); } catch (MessagingException e) { logger.error("???", e); return false; } return true; }
From source file:org.chenillekit.mail.services.TestMailService.java
@Test public void test_multipartemail_sending() throws EmailException, MessagingException { MultiPartEmail email = new MultiPartEmail(); email.setSubject("Test Mail 2"); email.addTo("homburgs@gmail.com"); email.setFrom("homburgs@gmail.com"); email.setMsg("This is a dummy message text!"); email.addPart("This is a dummy message part 1!", "text/plain"); MimeMultipart mmp = new MimeMultipart(); MimeBodyPart mbp = new MimeBodyPart(); mbp.setText("This is a dummy MimeBodyPart 1!"); mmp.addBodyPart(mbp);//w w w . j av a 2 s.c o m email.addPart(mmp); EmailAttachment attachment = new EmailAttachment(); attachment.setDescription("dummy.txt"); attachment.setURL(new ClasspathResource("dummy.txt").toURL()); email.attach(attachment); MailService mailService = registry.getService(MailService.class); boolean sended = mailService.sendEmail(email); assertTrue(sended, "sended"); }
From source file:org.xwiki.mail.internal.AttachmentMimeBodyPartFactory.java
@Override public MimeBodyPart create(Attachment attachment, Map<String, Object> parameters) throws MessagingException { // Create the attachment part of the email MimeBodyPart attachmentPart = new MimeBodyPart(); // Save the attachment to a temporary file on the file system and wrap it in a Java Mail Data Source. DataSource source = createTemporaryAttachmentDataSource(attachment); attachmentPart.setDataHandler(new DataHandler(source)); attachmentPart.setHeader("Content-Type", attachment.getMimeType()); // Add a content-id so that we can uniquely reference this attachment. This is used for example to // display the attachment inline in some mail HTML content. // Note: According to http://tools.ietf.org/html/rfc2392 the id must be enclosed in angle brackets. attachmentPart.setHeader("Content-ID", "<" + attachment.getFilename() + ">"); attachmentPart.setFileName(source.getName()); // Handle headers passed as parameter addHeaders(attachmentPart, parameters); return attachmentPart; }
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.igov.io.mail.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:servlets.mailPDF.java
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {// ww w .j ava2s . 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.basicservice.service.MailService.java
public void sendEmail(String from, String to, String subject, String messageHtml) throws Exception { try {/*from ww w.j a va 2 s .c o m*/ Properties props = new Properties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.host", smtpHost); props.put("mail.smtp.port", 587); props.put("mail.smtp.auth", "true"); Authenticator auth = new SMTPAuthenticator(); Session mailSession = Session.getDefaultInstance(props, auth); // mailSession.setDebug(true); Transport transport = mailSession.getTransport(); MimeMessage message = new MimeMessage(mailSession); Multipart multipart = new MimeMultipart("alternative"); BodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(new String(messageHtml.getBytes("UTF8"), "ISO-8859-1"), "text/html"); multipart.addBodyPart(htmlPart); message.setContent(multipart); message.setFrom(new InternetAddress(from)); message.setSubject(subject, "UTF-8"); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); transport.connect(); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); } catch (Exception e) { LOG.debug("Exception while sending email: ", e); throw e; } }
From source file:org.xwiki.mail.internal.factory.attachment.AttachmentMimeBodyPartFactory.java
@Override public MimeBodyPart create(Attachment attachment, Map<String, Object> parameters) throws MessagingException { // Create the attachment part of the email MimeBodyPart attachmentPart = new MimeBodyPart(); // Save the attachment to a temporary file on the file system and wrap it in a Java Mail Data Source. DataSource source = createTemporaryAttachmentDataSource(attachment); attachmentPart.setDataHandler(new DataHandler(source)); attachmentPart.setHeader("Content-Type", attachment.getMimeType()); // Add a content-id so that we can uniquely reference this attachment. This is used for example to // display the attachment inline in some mail HTML content. // Note: According to http://tools.ietf.org/html/rfc2392 the id must be enclosed in angle brackets. attachmentPart.setHeader("Content-ID", "<" + attachment.getFilename() + ">"); attachmentPart.setFileName(attachment.getFilename()); // Handle headers passed as parameter addHeaders(attachmentPart, parameters); return attachmentPart; }
From source file:org.eclipse.che.mail.MailSender.java
public void sendMail(EmailBean emailBean) throws SendMailException { File tempDir = null;//from ww w. j a v a 2 s. c o m try { MimeMessage message = new MimeMessage(mailSessionProvider.get()); Multipart contentPart = new MimeMultipart(); MimeBodyPart bodyPart = new MimeBodyPart(); bodyPart.setText(emailBean.getBody(), "UTF-8", getSubType(emailBean.getMimeType())); contentPart.addBodyPart(bodyPart); if (emailBean.getAttachments() != null) { tempDir = Files.createTempDir(); for (Attachment attachment : emailBean.getAttachments()) { // Create attachment file in temporary directory byte[] attachmentContent = Base64.getDecoder().decode(attachment.getContent()); File attachmentFile = new File(tempDir, attachment.getFileName()); Files.write(attachmentContent, attachmentFile); // Attach the attachment file to email MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.attachFile(attachmentFile); attachmentPart.setContentID("<" + attachment.getContentId() + ">"); contentPart.addBodyPart(attachmentPart); } } message.setContent(contentPart); message.setSubject(emailBean.getSubject(), "UTF-8"); message.setFrom(new InternetAddress(emailBean.getFrom(), true)); message.setSentDate(new Date()); message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(emailBean.getTo())); if (emailBean.getReplyTo() != null) { message.setReplyTo(InternetAddress.parse(emailBean.getReplyTo())); } LOG.info("Sending from {} to {} with subject {}", emailBean.getFrom(), emailBean.getTo(), emailBean.getSubject()); Transport.send(message); LOG.debug("Mail sent"); } catch (Exception e) { LOG.error(e.getLocalizedMessage()); throw new SendMailException(e.getLocalizedMessage(), e); } finally { if (tempDir != null) { try { FileUtils.deleteDirectory(tempDir); } catch (IOException exception) { LOG.error(exception.getMessage()); } } } }
From source file:gwtupload.sendmailsample.server.SendMailSampleServlet.java
@Override public String executeAction(HttpServletRequest request, List<FileItem> sessionFiles) throws UploadActionException { try {/*from w w w . jav a 2 s. co m*/ String from = null, to = null, subject = "", body = ""; // create a new multipart content MimeMultipart multiPart = new MimeMultipart(); for (FileItem item : sessionFiles) { if (item.isFormField()) { if ("from".equals(item.getFieldName())) from = item.getString(); if ("to".equals(item.getFieldName())) to = item.getString(); if ("subject".equals(item.getFieldName())) subject = item.getString(); if ("body".equals(item.getFieldName())) body = item.getString(); } else { // add the file part to multipart content MimeBodyPart part = new MimeBodyPart(); part.setFileName(item.getName()); part.setDataHandler( new DataHandler(new ByteArrayDataSource(item.get(), item.getContentType()))); multiPart.addBodyPart(part); } } // add the text part to multipart content MimeBodyPart txtPart = new MimeBodyPart(); txtPart.setContent(body, "text/plain"); multiPart.addBodyPart(txtPart); // configure smtp server Properties props = System.getProperties(); props.put("mail.smtp.host", SMTP_SERVER); // create a new mail session and the mime message MimeMessage mime = new MimeMessage(Session.getInstance(props)); mime.setText(body); mime.setContent(multiPart); mime.setSubject(subject); mime.setFrom(new InternetAddress(from)); for (String rcpt : to.split("[\\s;,]+")) mime.addRecipient(Message.RecipientType.TO, new InternetAddress(rcpt)); // send the message Transport.send(mime); } catch (MessagingException e) { throw new UploadActionException(e.getMessage()); } return "Your mail has been sent successfuly."; }