List of usage examples for javax.mail.internet MimeBodyPart MimeBodyPart
public MimeBodyPart()
From source file:com.intuit.tank.mail.TankMailer.java
/** * @{inheritDoc//from w w w . jav a 2 s . co m */ @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:com.trivago.mail.pigeon.mail.MailFacade.java
public void sendMail(MailTransport mailTransport) { log.debug("Mail delivery started"); File propertyfile = ((PropertiesConfiguration) Settings.create().getConfiguration()).getFile(); Properties config = new Properties(); try {/*from w ww . ja v a 2 s . c o m*/ config.load(new FileReader(propertyfile)); } catch (IOException e) { log.error(e); } Session session = Session.getDefaultInstance(config); log.debug("Received session"); MimeMessage message = new MimeMessage(session); String to = mailTransport.getTo(); String from = mailTransport.getFrom(); String replyTo = mailTransport.getReplyTo(); String subject = mailTransport.getSubject(); String html = mailTransport.getHtml(); String text = mailTransport.getText(); try { Address fromAdr = new InternetAddress(from); Address toAdr = new InternetAddress(to); Address rplyAdr = new InternetAddress(replyTo); message.setSubject(subject); message.setFrom(fromAdr); message.setRecipient(Message.RecipientType.TO, toAdr); message.setReplyTo(new Address[] { rplyAdr }); message.setSender(fromAdr); message.addHeader("Return-path", replyTo); message.addHeader("X-TRV-MID", mailTransport.getmId()); message.addHeader("X-TRV-UID", mailTransport.getuId()); // Content MimeBodyPart messageTextPart = new MimeBodyPart(); messageTextPart.setText(text); messageTextPart.setContent(html, "text/html"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageTextPart); // Put parts in message message.setContent(multipart); log.debug("Dispatching message"); Transport.send(message); log.debug("Mail delivery ended"); } catch (MessagingException e) { log.error(e); } }
From source file:com.adaptris.core.MimeEncoderImpl.java
protected MimeBodyPart asMimePart(Exception e) throws Exception { MimeBodyPart p = new MimeBodyPart(); try (ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream printer = new PrintStream(out, true)) { e.printStackTrace(printer);/*from ww w.j a v a 2 s. co m*/ p.setDataHandler(new DataHandler(new ByteArrayDataSource(out.toByteArray()))); } return p; }
From source file:com.tdclighthouse.commons.mail.util.MailClient.java
public void sendMail(String from, String[] to, Mail mail) throws MessagingException, AddressException { // a brief validation if ((from == null) || "".equals(from) || (to.length == 0) || (mail == null)) { throw new IllegalArgumentException(); }//from www . j a v a 2s.c om Session session = getSession(); // Define a new mail message MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); for (int i = 0; i < to.length; i++) { message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i])); } message.setSubject(mail.getSubject(), "utf-8"); // use a MimeMultipart as we need to handle the file attachments Multipart multipart = new MimeMultipart("alternative"); if ((mail.getMessageBody() != null) && !"".equals(mail.getMessageBody())) { // add the message body to the mime message BodyPart textPart = new MimeBodyPart(); textPart.setContent(mail.getMessageBody(), "text/plain; charset=utf-8"); // sets type to "text/plain" multipart.addBodyPart(textPart); } if (mail.getHtmlBody() != null) { BodyPart pixPart = new MimeBodyPart(); pixPart.setContent(mail.getHtmlBody(), "text/html; charset=utf-8"); multipart.addBodyPart(pixPart); } // add any file attachments to the message addAtachments(mail.getAttachments(), multipart); // Put all message parts in the message message.setContent(multipart); // Send the message Transport.send(message); }
From source file:com.pushinginertia.commons.net.email.EmailUtils.java
/** * Populates a newly instantiated {@link MultiPartEmail} with the given arguments. * @param email email instance to populate * @param smtpHost SMTP host that the message will be sent to * @param msg container object for the email's headers and contents * @throws IllegalArgumentException if the inputs are not valid * @throws EmailException if a problem occurs constructing the email *///from www . j a v a2 s . c o m public static void populateMultiPartEmail(final MultiPartEmail email, final String smtpHost, final EmailMessage msg) throws IllegalArgumentException, EmailException { ValidateAs.notNull(email, "email"); ValidateAs.notNull(smtpHost, "smtpHost"); ValidateAs.notNull(msg, "msg"); email.setHostName(smtpHost); email.setCharset(UTF_8); email.setFrom(msg.getSender().getEmail(), msg.getSender().getName(), UTF_8); email.addTo(msg.getRecipient().getEmail(), msg.getRecipient().getName(), UTF_8); final NameEmail replyTo = msg.getReplyTo(); if (replyTo != null) { email.addReplyTo(replyTo.getEmail(), replyTo.getName(), UTF_8); } final String bounceEmailAddress = msg.getBounceEmailAddress(); if (bounceEmailAddress != null) { email.setBounceAddress(bounceEmailAddress); } email.setSubject(msg.getSubject()); final String languageId = msg.getRecipient().getLanguage(); if (languageId != null) { email.addHeader("Language", languageId); email.addHeader("Content-Language", languageId); } // add optional headers final EmailMessageHeaders headers = msg.getHeaders(); if (headers != null) { for (final Map.Entry<String, String> header : headers.getHeaders().entrySet()) { email.addHeader(header.getKey(), header.getValue()); } } // generate email body try { final MimeMultipart mm = new MimeMultipart("alternative; charset=UTF-8"); final MimeBodyPart text = new MimeBodyPart(); text.setContent(msg.getTextContent(), "text/plain; charset=UTF-8"); mm.addBodyPart(text); final MimeBodyPart html = new MimeBodyPart(); html.setContent(msg.getHtmlContent(), "text/html; charset=UTF-8"); mm.addBodyPart(html); email.setContent(mm); } catch (MessagingException e) { throw new EmailException(e); } }
From source file:com.synyx.greetingcard.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 w w .jav a 2s . c o 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())); message.addRecipient(Message.RecipientType.TO, new InternetAddress(config.getTo(), config.getToName())); } 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.cgiar.ccafs.marlo.action.TestSMTPAction.java
@Override public String execute() throws Exception { Properties properties = System.getProperties(); properties.put("mail.smtp.host", config.getEmailHost()); properties.put("mail.smtp.port", config.getEmailPort()); Session session = Session.getInstance(properties, new Authenticator() { @Override/* w ww . j a v a 2 s . c o m*/ protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(config.getEmailUsername(), config.getEmailPassword()); } }); // Create a new message MimeMessage msg = new MimeMessage(session) { @Override protected void updateMessageID() throws MessagingException { if (this.getHeader("Message-ID") == null) { super.updateMessageID(); } } }; msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("h.jimenez@cgiar.org", false)); msg.setSubject("Test email"); msg.setSentDate(new Date()); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setContent("If you receive this email, it means that the server is working correctly.", "text; charset=utf-8"); Thread thread = new Thread() { @Override public void run() { sent = false; int i = 0; while (!sent) { try { Transport.send(sendMail); LOG.info("Message sent TRIED#: " + i + " \n" + "Test email"); sent = true; } catch (MessagingException e) { LOG.info("Message DON'T sent: \n" + "Test email"); i++; if (i == 10) { break; } try { Thread.sleep(1 * // minutes to sleep 60 * // seconds to a minute 1000); } catch (InterruptedException e1) { e1.printStackTrace(); } e.printStackTrace(); } } }; }; thread.run(); if (sent) { return SUCCESS; } else { return INPUT; } }
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: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:org.roda.core.util.EmailUtility.java
/** * @param from// www . j a v a2 s . com * @param recipients * @param subject * @param message * @throws MessagingException */ public void sendMail(String from, String recipients[], String subject, String message) throws MessagingException { boolean debug = false; // Set the host smtp address Properties props = new Properties(); props.put("mail.smtp.host", this.smtpHost); // create some properties and get the default Session Session session = Session.getDefaultInstance(props, null); session.setDebug(debug); // create a message Message msg = new MimeMessage(session); // set the from and to address InternetAddress addressFrom = new InternetAddress(from); msg.setFrom(addressFrom); 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); // Optional : You can also set your custom headers in the Email if you // want // msg.addHeader("MyHeaderName", "myHeaderValue"); String htmlMessage = String.format( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<html><body><pre>%s</pre></body></html>", StringEscapeUtils.escapeHtml4(message)); MimeMultipart mimeMultipart = new MimeMultipart(); MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setContent(htmlMessage, "text/html;charset=UTF-8"); mimeMultipart.addBodyPart(mimeBodyPart); // Setting the Subject and Content Type msg.setSubject(subject); msg.setContent(mimeMultipart); // msg.setContent(message, "text/plain;charset=UTF-8"); Transport.send(msg); }