List of usage examples for javax.mail.internet MimeMultipart addBodyPart
@Override public synchronized void addBodyPart(BodyPart part) throws MessagingException
From source file:org.snopoke.util.Emailer.java
private MimeMultipart getCoverPart() throws MessagingException { // Alternative TEXT/HTML content MimeMultipart cover = new MimeMultipart("alternative"); if (textContent != null && !textContent.isEmpty()) { MimeBodyPart text = new MimeBodyPart(); cover.addBodyPart(text); text.setText(textContent);/*from w w w .j a v a2 s.co m*/ } if (htmlContent != null && !htmlContent.isEmpty()) { MimeBodyPart html = new MimeBodyPart(); cover.addBodyPart(html); html.setContent(htmlContent, "text/html"); } return cover; }
From source file:org.tizzit.util.mail.Mail.java
public boolean sendPlaintextMail() { try {//from ww w.j a va 2 s . co m if (this.attachments.size() > 0) { MimeMultipart multiPart = new MimeMultipart("mixed"); BodyPart plainTextBodyPart = new MimeBodyPart(); plainTextBodyPart.setText(this.messageText); multiPart.addBodyPart(plainTextBodyPart); for (int i = 0; i < this.attachments.size(); i++) { multiPart.addBodyPart(this.attachments.get(i)); } this.message.setContent(multiPart); } else { this.message.setText(this.messageText, this.encoding, "plain"); } this.message.saveChanges(); doSend(); } catch (MessagingException exception) { log.error("Error sending plain text mail", exception); return false; } return true; }
From source file:org.tizzit.util.mail.Mail.java
public boolean sendHtmlMail(String alternativePlaintextBody) { try {/*from ww w . j a v a 2 s . co m*/ if (this.attachments.size() > 0) { MimeMultipart mainMultiPart = new MimeMultipart("mixed"); if (alternativePlaintextBody != null) { MimeMultipart alternativeMultiPart = new MimeMultipart("alternative"); MimeBodyPart plainTextBodyPart = new MimeBodyPart(); MimeBodyPart htmlBodyPart = new MimeBodyPart(); plainTextBodyPart.setText(alternativePlaintextBody, this.encoding, "plain"); htmlBodyPart.setText(this.messageText, this.encoding, "html"); alternativeMultiPart.addBodyPart(plainTextBodyPart); alternativeMultiPart.addBodyPart(htmlBodyPart); MimeBodyPart containerBodyPart = new MimeBodyPart(); containerBodyPart.setContent(alternativeMultiPart); mainMultiPart.addBodyPart(containerBodyPart); } else { // without plain text alternative MimeBodyPart htmlBodyPart = new MimeBodyPart(); htmlBodyPart.setText(this.messageText, this.encoding, "html"); mainMultiPart.addBodyPart(htmlBodyPart); } for (int i = 0; i < this.attachments.size(); i++) { mainMultiPart.addBodyPart(this.attachments.get(i)); } this.message.setContent(mainMultiPart); } else { // no attachments if (alternativePlaintextBody != null) { MimeMultipart mainMultipart = new MimeMultipart("alternative"); MimeBodyPart plainTextBodyPart = new MimeBodyPart(); MimeBodyPart htmlBodyPart = new MimeBodyPart(); plainTextBodyPart.setText(alternativePlaintextBody, this.encoding, "plain"); htmlBodyPart.setText(this.messageText, this.encoding, "html"); mainMultipart.addBodyPart(plainTextBodyPart); mainMultipart.addBodyPart(htmlBodyPart); this.message.setContent(mainMultipart); } else { // no alternative plain text neither -> no MimeMessage! this.message.setContent(this.messageText, "text/html"); } } this.message.saveChanges(); doSend(); } catch (MessagingException exception) { log.error("Error sending HTML mail", exception); return false; } return true; }
From source file:org.tizzit.util.mail.MailHelper.java
/** * Send an email in html-format in iso-8859-1-encoding * /* w w w . ja va 2 s .c o m*/ * @param subject the subject of the new mail * @param htmlMessage the content of the mail as html * @param alternativeTextMessage the content of the mail as text * @param from the sender-address * @param to the receiver-address * @param cc the address of the receiver of a copy of this mail * @param bcc the address of the receiver of a blind-copy of this mail */ public static void sendHtmlMail(String subject, String htmlMessage, String alternativeTextMessage, String from, String to, String cc, String bcc) { try { MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(from)); if (cc != null && !cc.equals("")) msg.setRecipients(Message.RecipientType.CC, cc); if (bcc != null && !bcc.equals("")) msg.setRecipients(Message.RecipientType.BCC, bcc); msg.addRecipients(Message.RecipientType.TO, to); msg.setSubject(subject, "iso-8859-1"); msg.setSentDate(new Date()); MimeMultipart multiPart = new MimeMultipart(); BodyPart bodyPart = new MimeBodyPart(); bodyPart.setText(alternativeTextMessage); multiPart.addBodyPart(bodyPart); bodyPart = new MimeBodyPart(); bodyPart.setContent(htmlMessage, "text/html"); multiPart.addBodyPart(bodyPart); multiPart.setSubType("alternative"); msg.setContent(multiPart); Transport.send(msg); } catch (Exception e) { log.error("Error sending html-mail: " + e.getLocalizedMessage()); } }
From source file:org.tizzit.util.mail.MailHelper.java
/** * Send an email in html-format in iso-8859-1-encoding * // w ww . j a v a 2 s . c om * @param subject the subject of the new mail * @param htmlMessage the content of the mail as html * @param alternativeTextMessage the content of the mail as text * @param from the sender-address * @param to the receiver-address * @param cc the address of the receiver of a copy of this mail * @param bcc the address of the receiver of a blind-copy of this mail */ public static void sendHtmlMail2(String subject, String htmlMessage, String alternativeTextMessage, String from, String to, String cc, String bcc) { try { MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(from)); if (cc != null && !cc.equals("")) msg.setRecipients(Message.RecipientType.CC, cc); if (bcc != null && !bcc.equals("")) msg.setRecipients(Message.RecipientType.BCC, bcc); msg.addRecipients(Message.RecipientType.TO, to); msg.setSubject(subject, "ISO8859_1"); msg.setSentDate(new Date()); MimeMultipart multiPart = new MimeMultipart("alternative"); MimeBodyPart htmlPart = new MimeBodyPart(); MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(alternativeTextMessage, "ISO8859_1"); textPart.setHeader("MIME-Version", "1.0"); //textPart.setHeader("Content-Type", textPart.getContentType()); textPart.setHeader("Content-Type", "text/plain;charset=\"ISO-8859-1\""); htmlPart.setContent(htmlMessage, "text/html;charset=\"ISO8859_1\""); htmlPart.setHeader("MIME-Version", "1.0"); htmlPart.setHeader("Content-Type", "text/html;charset=\"ISO-8859-1\""); //htmlPart.setHeader("Content-Type", htmlPart.getContentType()); multiPart.addBodyPart(textPart); multiPart.addBodyPart(htmlPart); multiPart.setSubType("alternative"); msg.setContent(multiPart); msg.setHeader("MIME-Version", "1.0"); msg.setHeader("Content-Type", multiPart.getContentType()); Transport.send(msg); } catch (Exception e) { log.error("Error sending html-mail: " + e.getLocalizedMessage()); } }
From source file:org.tizzit.util.mail.MailHelper.java
/** * Send an email in html-format in iso-8859-1-encoding * // w ww .j a v a 2s .c o m * @param subject the subject of the new mail * @param htmlMessage the content of the mail as html * @param alternativeTextMessage the content of the mail as text * @param from the sender-address * @param to the receiver-address * @param cc the address of the receiver of a copy of this mail * @param bcc the address of the receiver of a blind-copy of this mail */ public static void sendHtmlMail2(String subject, String htmlMessage, String alternativeTextMessage, String from, String[] to, String[] cc, String[] bcc) { try { MimeMessage msg = new MimeMessage(mailSession); msg.setFrom(new InternetAddress(from)); if (cc != null && cc.length != 0) { for (int i = cc.length - 1; i >= 0; i--) { msg.addRecipients(Message.RecipientType.CC, cc[i]); } } if (bcc != null && bcc.length != 0) { for (int i = bcc.length - 1; i >= 0; i--) { msg.addRecipients(Message.RecipientType.BCC, bcc[i]); } } if (to != null && to.length != 0) { for (int i = to.length - 1; i >= 0; i--) { msg.addRecipients(Message.RecipientType.TO, to[i]); } } msg.setSubject(subject, "ISO8859_1"); msg.setSentDate(new Date()); MimeMultipart multiPart = new MimeMultipart("alternative"); MimeBodyPart htmlPart = new MimeBodyPart(); MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(alternativeTextMessage, "ISO8859_1"); textPart.setHeader("MIME-Version", "1.0"); //textPart.setHeader("Content-Type", textPart.getContentType()); textPart.setHeader("Content-Type", "text/plain;charset=\"ISO-8859-1\""); htmlPart.setContent(htmlMessage, "text/html;charset=\"ISO8859_1\""); htmlPart.setHeader("MIME-Version", "1.0"); htmlPart.setHeader("Content-Type", "text/html;charset=\"ISO-8859-1\""); //htmlPart.setHeader("Content-Type", htmlPart.getContentType()); multiPart.addBodyPart(textPart); multiPart.addBodyPart(htmlPart); multiPart.setSubType("alternative"); msg.setContent(multiPart); msg.setHeader("MIME-Version", "1.0"); msg.setHeader("Content-Type", multiPart.getContentType()); Transport.send(msg); } catch (Exception e) { log.error("Error sending html-mail: " + e.getLocalizedMessage()); } }
From source file:org.viafirma.util.SendMailUtil.java
public MultiPartEmail buildMessage(String subject, String mailTo, String texto, String htmlTexto, String alias, String password) throws ExcepcionErrorInterno, ExcepcionCertificadoNoEncontrado { try {// w ww. jav a 2 s .com // 1.- Preparamos el certificado // Recuperamos la clave privada asociada al alias PrivateKey privateKey = KeyStoreLoader.getPrivateKey(alias, password); if (privateKey == null) { throw new ExcepcionCertificadoNoEncontrado( "No existe una clave privada para el alias '" + alias + "'"); } if (log.isDebugEnabled()) log.info("Firmando el documento con el certificado " + alias); // Recuperamos el camino de confianza asociado al certificado List<Certificate> chain = KeyStoreLoader.getCertificateChain(alias); // Obtenemos los datos del certificado utilizado. X509Certificate certificadoX509 = (X509Certificate) chain.get(0); CertificadoGenerico datosCertificado = CertificadoGenericoFactory.getInstance() .generar(certificadoX509); String emailFrom = datosCertificado.getEmail(); String emailFromDesc = datosCertificado.getCn(); if (StringUtils.isEmpty(emailFrom)) { log.warn("El certificado indicado no tiene un email asociado, No es vlido para firmar emails" + datosCertificado); throw new ExcepcionCertificadoNoEncontrado( "El certificado indicado no tiene un email asociado, No es vlido para firmar emails."); } CertStore certificadosYcrls = CertStore.getInstance("Collection", new CollectionCertStoreParameters(chain), BouncyCastleProvider.PROVIDER_NAME); // 2.- Preparamos el mail MimeBodyPart bodyPart = new MimeBodyPart(); MimeMultipart dataMultiPart = new MimeMultipart(); MimeBodyPart msgHtml = new MimeBodyPart(); if (StringUtils.isNotEmpty(htmlTexto)) { msgHtml.setContent(htmlTexto, Email.TEXT_HTML + "; charset=UTF-8"); } else { msgHtml.setContent("<p>" + htmlTexto + "</p>", Email.TEXT_PLAIN + "; charset=UTF-8"); } // create the message we want signed MimeBodyPart mensajeTexto = new MimeBodyPart(); if (StringUtils.isNotEmpty(texto)) { mensajeTexto.setText(texto, "UTF-8"); } else if (StringUtils.isEmpty(texto)) { mensajeTexto.setText(CadenaUtilities.cleanHtml(htmlTexto), "UTF-8"); } dataMultiPart.addBodyPart(mensajeTexto); dataMultiPart.addBodyPart(msgHtml); bodyPart.setContent(dataMultiPart); // Crea el nuevo mensaje firmado MimeMultipart multiPart = createMultipartWithSignature(privateKey, certificadoX509, certificadosYcrls, bodyPart); // Creamos el mensaje que finalmente sera enviadio. MultiPartEmail mail = createMultiPartEmail(subject, mailTo, emailFrom, emailFromDesc, multiPart, multiPart.getContentType()); return mail; } catch (InvalidAlgorithmParameterException e) { throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e); } catch (NoSuchAlgorithmException e) { throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e); } catch (NoSuchProviderException e) { throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e); } catch (MessagingException e) { throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e); } catch (CertificateParsingException e) { throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e); } catch (CertStoreException e) { throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e); } catch (SMIMEException e) { throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e); } catch (EmailException e) { throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e); } }
From source file:org.wandora.piccolo.actions.SendEmail.java
public void doAction(User user, javax.servlet.ServletRequest request, javax.servlet.ServletResponse response, Application application) {/*from w w w. ja va 2 s.co m*/ try { Properties props = new Properties(); props.put("mail.smtp.host", smtpServer); Session session = Session.getDefaultInstance(props, null); MimeMessage message = new MimeMessage(session); if (subject != null) message.setSubject(subject); if (from != null) message.setFrom(new InternetAddress(from)); Vector<String> recipients = new Vector<String>(); if (recipient != null) { String[] rs = recipient.split(","); String r = null; for (int i = 0; i < rs.length; i++) { r = rs[i]; if (r != null) recipients.add(r); } } MimeMultipart multipart = new MimeMultipart(); Template template = application.getTemplate(emailTemplate, user); HashMap context = new HashMap(); context.put("request", request); context.put("message", message); context.put("recipients", recipients); context.put("emailhelper", new MailHelper()); context.put("multipart", multipart); context.putAll(application.getDefaultContext(user)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); template.process(context, baos); MimeBodyPart mimeBody = new MimeBodyPart(); mimeBody.setContent(new String(baos.toByteArray(), template.getEncoding()), template.getMimeType()); // mimeBody.setContent(baos.toByteArray(),template.getMimeType()); multipart.addBodyPart(mimeBody); message.setContent(multipart); Transport transport = session.getTransport("smtp"); transport.connect(smtpServer, smtpUser, smtpPass); Address[] recipientAddresses = new Address[recipients.size()]; for (int i = 0; i < recipientAddresses.length; i++) { recipientAddresses[i] = new InternetAddress(recipients.elementAt(i)); } transport.sendMessage(message, recipientAddresses); } catch (Exception e) { logger.writelog("WRN", e); } }
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.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; }