List of usage examples for javax.mail.internet MimeMultipart getContentType
public synchronized String getContentType()
From source file:org.tizzit.util.mail.MailHelper.java
/** * Send an email in html-format in iso-8859-1-encoding * /* w ww .ja va2 s .com*/ * @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 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 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 {/*ww w . j a va 2 s.co m*/ // 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); } }