List of usage examples for javax.mail.internet MimeBodyPart setText
@Override public void setText(String text, String charset) throws MessagingException
From source file:org.tizzit.util.mail.MailHelper.java
/** * Send an email in html-format in iso-8859-1-encoding * /*ww w. j a v a 2 s . co 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 w w.ja v a 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); } }
From source file:org.wf.dp.dniprorada.util.Mail.java
public Mail _Attach(String sBody) { try {/*from w w w.j ava 2s. c o m*/ MimeBodyPart oMimeBodyPart = new MimeBodyPart(); //oMimeBodyPart.setText(sBody,DEFAULT_ENCODING,"Content-Type: text/html;"); oMimeBodyPart.setText(sBody, DEFAULT_ENCODING); // oMimeBodyPart.setHeader("Content-Type", "text/html"); oMimeBodyPart.setHeader("Content-Type", "text/html;charset=utf-8"); oMultiparts.addBodyPart(oMimeBodyPart); log.info("[_Attach:sBody]:sBody=" + sBody); } catch (Exception oException) { log.error("[_Attach:sBody]", oException); } return this; }
From source file:uk.ac.cam.cl.dtg.util.Mailer.java
/** * Utility method to allow us to send multipart messages using HTML and plain text. * * //w ww .j a v a 2s . c o m * @param recipient * - string array of recipients that the message should be sent to * @param from * - the e-mail address that should be used as the sending address * @param replyTo * - (nullable) the e-mail address that should be used as the reply-to address * @param replyToName * - (nullable) the name that should be used as the reply-to name * @param subject * - The message subject * @param plainText * - The message body * @param html * - The message body in html * @throws MessagingException * - if we cannot send the message for some reason. * @throws AddressException * - if the address is not valid. */ public void sendMultiPartMail(final String[] recipient, final String from, @Nullable final String replyTo, @Nullable final String replyToName, final String subject, final String plainText, final String html) throws MessagingException, AddressException, UnsupportedEncodingException { Message msg = this.setupMessage(recipient, from, replyTo, replyToName, subject); // Create the text part MimeBodyPart textPart = new MimeBodyPart(); textPart.setText(plainText, "utf-8"); MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(html, "text/html; charset=utf-8"); Multipart multiPart = new MimeMultipart("alternative"); multiPart.addBodyPart(textPart); multiPart.addBodyPart(htmlPart); msg.setContent(multiPart); Transport.send(msg); }