List of usage examples for javax.mail.internet MimeBodyPart MimeBodyPart
public MimeBodyPart()
From source file:org.masukomi.aspirin.core.Bouncer.java
/** * This generates a response to the Return-Path address, or the address of * the message's sender if the Return-Path is not available. Note that this * is different than a mail-client's reply, which would use the Reply-To or * From header./*ww w .j a v a2 s . c o m*/ * * @param mail * DOCUMENT ME! * @param message * DOCUMENT ME! * @param bouncer * DOCUMENT ME! * * @throws MessagingException * DOCUMENT ME! */ static public void bounce(MailQue que, Mail mail, String message, MailAddress bouncer) throws MessagingException { if (bouncer != null) { if (log.isDebugEnabled()) { log.debug("bouncing message to postmaster"); } MimeMessage orig = mail.getMessage(); //Create the reply message MimeMessage reply = (MimeMessage) orig.reply(false); //If there is a Return-Path header, if (orig.getHeader(RFC2822Headers.RETURN_PATH) != null) { //Return the message to that address, not to the Reply-To // address reply.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(orig.getHeader(RFC2822Headers.RETURN_PATH)[0])); } //Create the list of recipients in our MailAddress format Collection recipients = new HashSet(); Address[] addresses = reply.getAllRecipients(); for (int i = 0; i < addresses.length; i++) { recipients.add(new MailAddress((InternetAddress) addresses[i])); } //Change the sender... reply.setFrom(bouncer.toInternetAddress()); try { //Create the message body MimeMultipart multipart = new MimeMultipart(); //Add message as the first mime body part MimeBodyPart part = new MimeBodyPart(); part.setContent(message, "text/plain"); part.setHeader(RFC2822Headers.CONTENT_TYPE, "text/plain"); multipart.addBodyPart(part); //Add the original message as the second mime body part part = new MimeBodyPart(); part.setContent(orig.getContent(), orig.getContentType()); part.setHeader(RFC2822Headers.CONTENT_TYPE, orig.getContentType()); multipart.addBodyPart(part); reply.setHeader(RFC2822Headers.DATE, rfc822DateFormat.format(new Date())); reply.setContent(multipart); reply.setHeader(RFC2822Headers.CONTENT_TYPE, multipart.getContentType()); } catch (IOException ioe) { throw new MessagingException("Unable to create multipart body", ioe); } //Send it off... //sendMail( bouncer, recipients, reply ); que.queMail(reply); } }
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 w w w. ja va 2 s . co m*/ 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.ambraproject.wombat.service.FreemarkerMailServiceImpl.java
@Override public Multipart createContent(Site site, String templateFilename, Model context) throws IOException, MessagingException { Template textTemplate = getEmailTemplate(site, "txt", templateFilename); Template htmlTemplate = getEmailTemplate(site, "html", templateFilename); // Create a "text" Multipart message Multipart mp = createPartForMultipart(textTemplate, context, "alternative", ContentType.TEXT_PLAIN); // Create a "HTML" Multipart message Multipart htmlContent = createPartForMultipart(htmlTemplate, context, "related", ContentType.TEXT_HTML); BodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(htmlContent);/*from ww w . java 2 s . co m*/ mp.addBodyPart(htmlPart); return mp; }
From source file:com.assetmanager.service.mail.MailService.java
/** * Sends the activation e-mail to the given user. * * @param user the user/*from w w w .j a v a2s .co m*/ * @param locale the locale * @throws MessagingException messaging exception */ public final void sendActivationEmail(final UserAccount user, final String locale) throws MessagingException { final Properties props = new Properties(); final Session session = Session.getDefaultInstance(props, null); final Message message = new MimeMessage(session); final Multipart multipart = new MimeMultipart(); final MimeBodyPart htmlPart = new MimeBodyPart(); final MimeBodyPart textPart = new MimeBodyPart(); message.setFrom(new InternetAddress(getFromAddress())); message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); message.setSubject(messageSource.getMessage("mail.subject", null, new Locale(locale))); textPart.setContent(messageSource.getMessage("mail.body.txt", new Object[] { getHostname(), user.getActivationKey() }, new Locale(locale)), "text/plain"); htmlPart.setContent(messageSource.getMessage("mail.body.html", new Object[] { getHostname(), user.getActivationKey() }, new Locale(locale)), "text/html"); multipart.addBodyPart(textPart); multipart.addBodyPart(htmlPart); message.setContent(multipart); Transport.send(message); }
From source file:com.autentia.tnt.mail.DefaultMailService.java
public void sendFiles(String to, String subject, String text, Collection<File> attachments) throws MessagingException { MimeMessage message = new MimeMessage(session); Transport t = session.getTransport("smtp"); message.setFrom(new InternetAddress(configurationUtil.getMailUsername())); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject);/*www . jav a 2 s . c om*/ message.setSentDate(new Date()); if (attachments == null || attachments.size() < 1) { message.setText(text); } else { // create the message part MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText(text); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); for (File attachment : attachments) { messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(attachment); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(attachment.getName()); multipart.addBodyPart(messageBodyPart); } message.setContent(multipart); } t.connect(configurationUtil.getMailUsername(), configurationUtil.getMailPassword()); t.sendMessage(message, message.getAllRecipients()); t.close(); }
From source file:com.gitlab.anlar.lunatic.server.ServerTest.java
private void sendMultiPartEmail(String from, String to, String subject, String body) throws MessagingException { Properties props = createEmailProps(serverPort); Session session = Session.getInstance(props); Message msg = createBaseMessage(from, to, subject, session); MimeBodyPart p1 = new MimeBodyPart(); p1.setText(body);/* w ww. j a v a 2 s. com*/ MimeBodyPart p2 = new MimeBodyPart(); p2.setText("Second part"); Multipart mp = new MimeMultipart(); mp.addBodyPart(p1); mp.addBodyPart(p2); msg.setContent(mp); Transport.send(msg); }
From source file:com.spartasystems.holdmail.util.TestMailClient.java
private BodyPart createHtmlBodyPart(String htmlMessageBody) throws MessagingException { BodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(htmlMessageBody, "text/html"); return htmlPart; }
From source file:ro.agrade.jira.qanda.listeners.DirectEmailMessageHandler.java
@Override protected void sendMail(String[] recipients, String subject, String message, String from) throws MessagingException { SMTPMailServer server = mailServerManager.getDefaultSMTPMailServer(); if (server == null) { LOG.debug("Email server is not configured. QandA is unable to send mails ..."); return;//from w w w .ja v a2 s . com } LOG.debug("Email message: initializing."); //Set the host smtp address Properties props = new Properties(); String proto = server.getMailProtocol().getProtocol(); props.put("mail.transport.protocol", proto); props.put("mail." + proto + ".host", server.getHostname()); props.put("mail." + proto + ".port", server.getPort()); String username = server.getUsername(); String password = server.getPassword(); Authenticator auth = null; if (username != null && password != null) { auth = new SMTPAuthenticator(username, password); props.put("mail." + proto + ".auth", "true"); } Session session; try { session = auth != null ? Session.getDefaultInstance(props, auth) : Session.getDefaultInstance(props); } catch (SecurityException ex) { LOG.warn("Could not get default session. Attempting to create a new one."); session = auth != null ? Session.getInstance(props, auth) : Session.getInstance(props); } // create a message MimeMessage msg = new MimeMessage(session); Multipart multipart = new MimeMultipart(); if (from == null) { from = server.getDefaultFrom(); } // set the from address if (from != null) { InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); } if (recipients != null && recipients.length > 0) { // set TO address(es) InternetAddress[] addressTo = new InternetAddress[recipients.length]; for (int i = 0; i < recipients.length; i++) { addressTo[i] = new InternetAddress(recipients[i]); } msg.setRecipients(Message.RecipientType.TO, addressTo); } // Setting the Subject msg.setSubject(subject); // Setting text content MimeBodyPart contentPart = new MimeBodyPart(); contentPart.setContent(message, "text/html; charset=utf-8"); multipart.addBodyPart(contentPart); msg.setContent(multipart); Transport.send(msg); LOG.debug("Email message sent successfully."); }
From source file:org.xwiki.mail.internal.factory.html.HTMLMimeBodyPartFactory.java
@Override public MimeBodyPart create(String content, Map<String, Object> parameters) throws MessagingException { MimeBodyPart resultBodyPart;/*from w w w . j av a2 s . co m*/ // Separate normal attachment from embedded image attachments List<Attachment> allAttachments = (List<Attachment>) parameters.get("attachments"); Pair<List<Attachment>, List<Attachment>> attachmentPairs = separateAttachments(content, allAttachments); List<Attachment> embeddedImageAttachments = attachmentPairs.getLeft(); List<Attachment> normalAttachments = attachmentPairs.getRight(); // Step 1: Handle the HTML section of the mail. MimeBodyPart htmlBodyPart; if (!embeddedImageAttachments.isEmpty()) { htmlBodyPart = new MimeBodyPart(); htmlBodyPart.setContent(createHTMLMultipart(content, embeddedImageAttachments)); } else { // Create the HTML body part of the email htmlBodyPart = createHTMLBodyPart(content, false); } // Step 2: Handle the optional alternative text String alternativeText = (String) parameters.get("alternate"); if (alternativeText != null) { resultBodyPart = createAlternativePart(htmlBodyPart, this.defaultPartFactory.create(alternativeText, Collections.<String, Object>emptyMap())); } else { // No alternative text, just add the HTML body part to the Multipart resultBodyPart = htmlBodyPart; } // Step 3 Add the normal attachments (if any). Any embedded images have already been handled in the HTML body // part. Note: If there are attachments we need to wrap our body part inside a "mixed" Multipart. if (!normalAttachments.isEmpty()) { MimeMultipart multipart = new MimeMultipart("mixed"); multipart.addBodyPart(resultBodyPart); handleAttachments(multipart, normalAttachments); resultBodyPart = new MimeBodyPart(); resultBodyPart.setContent(multipart); } // Handle headers passed as parameter addHeaders(resultBodyPart, parameters); return resultBodyPart; }
From source file:net.sourceforge.subsonic.backend.service.EmailSession.java
public void sendHtmlMessage(String from, List<String> to, List<String> cc, List<String> bcc, List<String> replyTo, String subject, String html, String plain) throws MessagingException { MimeMessage message = createMessage(from, to, cc, bcc, replyTo, subject); MimeBodyPart plainPart = new MimeBodyPart(); plainPart.setText(plain);/* w ww. j a va 2s. co m*/ MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setText(html, "utf-8", "html"); MimeMultipart multipart = new MimeMultipart("alternative"); multipart.addBodyPart(plainPart); multipart.addBodyPart(htmlPart); message.setContent(multipart); sendMessage(message); }