Example usage for org.apache.commons.mail EmailException EmailException

List of usage examples for org.apache.commons.mail EmailException EmailException

Introduction

In this page you can find the example usage for org.apache.commons.mail EmailException EmailException.

Prototype

public EmailException(final Throwable rootCause) 

Source Link

Document

Constructs a new EmailException with specified nested Throwable root cause.

Usage

From source file:com.itcs.commons.email.impl.NoReplySystemMailSender.java

/**
 *
 * @param to//  w  w w .  ja v  a  2 s. co m
 * @param subject
 * @param body
 * @param attachments
 * @throws EmailException
 */
public static void sendHTML(String[] to, String subject, String body, List<EmailAttachment> attachments)
        throws EmailException {
    try {
        getExecutorService().execute(new RunnableSendHTMLEmail(getSession(), to, subject, body, attachments));
    } catch (NamingException ex) {
        Logger.getLogger(NoReplySystemMailSender.class.getName()).log(Level.INFO,
                "El nombre jndi de la session de email esta mal configurado!!", ex);
        throw new EmailException("El nombre jndi de la session de email esta mal configurado!!");
    }
}

From source file:com.dattack.jtoolbox.commons.email.HtmlEmailBuilder.java

/**
 * Builder method.//from   w w w  .  j  a  v  a 2 s. c o m
 *
 * @return the HtmlEmail
 * @throws EmailException
 *             if an error occurs while creating the email
 */
public HtmlEmail build() throws EmailException {

    if (hostname == null || hostname.isEmpty()) {
        throw new EmailException(String.format("Invalid SMTP server (hostname: '%s')", hostname));
    }

    if (from == null || from.isEmpty()) {
        throw new EmailException(String.format("Invalid email address (FROM: '%s'", from));
    }

    final HtmlEmail email = new HtmlEmail();
    email.setHostName(hostname);
    email.setFrom(from);
    email.setSubject(subject);

    if (message != null && !message.isEmpty()) {
        email.setMsg(message);
    }

    if (port > 0) {
        email.setSmtpPort(port);
    }

    if (username != null && !username.isEmpty()) {
        email.setAuthenticator(new DefaultAuthenticator(username, password));
    }

    if (sslOnConnect != null) {
        email.setSSLOnConnect(sslOnConnect);
    }

    if (startTlsEnabled != null) {
        email.setStartTLSEnabled(startTlsEnabled);
    }

    if (!toList.isEmpty()) {
        email.setTo(toList);
    }

    if (!ccList.isEmpty()) {
        email.setCc(ccList);
    }

    if (!bccList.isEmpty()) {
        email.setBcc(bccList);
    }
    return email;
}

From source file:io.mapzone.controller.email.EmailService.java

public void send(Email email) throws EmailException {
    String env = System.getProperty("io.mapzone.controller.SMTP");
    if (env == null) {
        throw new IllegalStateException(
                "Environment variable missing: io.mapzone.controller.SMTPP. Format: <host>|<login>|<passwd>|<from>");
    }/*from   w ww. j  a v a2s .c  o  m*/
    String[] parts = StringUtils.split(env, "|");
    if (parts.length < 3 || parts.length > 4) {
        throw new IllegalStateException(
                "Environment variable wrong: io.mapzone.controller.SMTP. Format: <host>|<login>|<passwd>|<from> : "
                        + env);
    }

    email.setDebug(true);
    email.setHostName(parts[0]);
    //email.setSmtpPort( 465 );
    //email.setSSLOnConnect( true );
    email.setAuthenticator(new DefaultAuthenticator(parts[1], parts[2]));
    if (email.getFromAddress() == null && parts.length == 4) {
        email.setFrom(parts[3]);
    }
    if (email.getSubject() == null) {
        throw new EmailException("Missing subject.");
    }
    email.send();
}

From source file:io.marto.aem.utils.email.FreemarkerTemplatedMailer.java

/**
 * @param recipients    list of recipients who will be sent the email
 * @param sender        the from email address
 * @param subject       the subject line
 * @param template      the path to the template (the view)
 * @param model         the detail body of the email (the model)
 *
 * @throws EmailException when an email can't be sent because of email server issues or if the message can't be rendered for what ever reason.
 *///  w  w  w .j a v  a  2  s .com
public void sendEmail(final String[] recipients, String sender, final String subject, String template,
        Object model) throws EmailException {
    try {
        final HtmlEmail email = constructEmail(recipients, sender, subject, template, model);
        final MessageGateway<HtmlEmail> gateway = messageGatewayService.getGateway(HtmlEmail.class);
        if (gateway != null) {
            gateway.send(email);
        } else {
            throw new EmailException("Could not obtain message gateway for html emails");
        }
    } catch (EmailException e) {
        throw new EmailException(format("Fatal error trying to send user feedback to %s", join(recipients)), e);
    }
}

From source file:lucee.runtime.net.mail.HtmlEmailImpl.java

/**
 * Set the text content.// w  ww . ja  v a  2s  .  c o m
 *
 * @param aText A String.
 * @return An HtmlEmail.
 * @throws EmailException see javax.mail.internet.MimeBodyPart
 *  for definitions
 *
 */
public HtmlEmailImpl setTextMsg(String aText) throws EmailException {
    if (StringUtil.isEmpty(aText)) {
        throw new EmailException("Invalid message supplied");
    }
    this.text = aText;
    return this;
}

From source file:com.jaeksoft.searchlib.config.Mailer.java

private void setupTransport() throws EmailException {
    String hostname = ClientFactory.INSTANCE.getSmtpHostname().getValue();
    if (hostname == null || hostname.length() == 0)
        throw new EmailException("The SMTP hostname is not setup");
    email.setHostName(hostname);/*from w  ww  .ja  v a  2s.  co m*/
    String username = ClientFactory.INSTANCE.getSmtpUsername().getValue();
    String password = ClientFactory.INSTANCE.getSmtpPassword().getValue();
    if (username != null && password != null && username.length() > 0 && password.length() > 0)
        email.setAuthentication(username, password);
    boolean useSSL = ClientFactory.INSTANCE.getSmtpUseSsl().getValue();
    boolean useTLS = ClientFactory.INSTANCE.getSmtpUseTls().getValue();
    int port = ClientFactory.INSTANCE.getSmtpPort().getValue();
    email.setSmtpPort(port);
    email.setSSLOnConnect(useSSL);
    email.setStartTLSEnabled(useTLS);
    if (useSSL)
        email.setSslSmtpPort(Integer.toString(port));
    String senderEmail = ClientFactory.INSTANCE.getSmtpSenderEmail().getValue();
    String senderName = ClientFactory.INSTANCE.getSmtpSenderName().getValue();
    if (senderEmail == null || senderEmail.length() == 0)
        throw new EmailException("The SMTP sender e-mail is not setup");
    email.setFrom(senderEmail, senderName);
}

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
 *//* w  w  w.ja v  a2 s.co  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:lucee.runtime.net.mail.HtmlEmailImpl.java

/**
 * Set the HTML content.//from w w  w .  j  a  v  a2  s . co m
 *
 * @param aHtml A String.
 * @return An HtmlEmail.
 * @throws EmailException see javax.mail.internet.MimeBodyPart
 *  for definitions
 *
 */
public HtmlEmailImpl setHtmlMsg(String aHtml) throws EmailException {
    if (StringUtil.isEmpty(aHtml)) {
        throw new EmailException("Invalid message supplied");
    }
    this.html = aHtml;
    return this;
}

From source file:lucee.runtime.net.mail.HtmlEmailImpl.java

/**
 * Set the message./*from  www  . ja va 2  s  .c  o m*/
 *
 * <p>This method overrides the MultiPartEmail setMsg() method in
 * order to send an HTML message instead of a full text message in
 * the mail body. The message is formatted in HTML for the HTML
 * part of the message, it is let as is in the alternate text
 * part.
 *
 * @param msg A String.
 * @return An Email.
 * @throws EmailException see javax.mail.internet.MimeBodyPart
 *  for definitions
 *
 */
public Email setMsg(String msg) throws EmailException {
    if (StringUtil.isEmpty(msg)) {
        throw new EmailException("Invalid message supplied");
    }

    setTextMsg(msg);

    setHtmlMsg(new StringBuffer().append("<html><body><pre>").append(msg).append("</pre></body></html>")
            .toString());

    return this;
}

From source file:com.itcs.commons.email.impl.RunnableSendHTMLEmail.java

private void addAttachments(MultiPartEmail email, List<EmailAttachment> attachments) throws EmailException {

    if (attachments != null && attachments.size() > 0) {
        String maxStringValue = getSession().getProperty(MAX_ATTACHMENTS_SIZE_PROP_NAME);
        //            System.out.println("maxStringValue= " + maxStringValue);
        long maxAttachmentSize = DISABLE_MAX_ATTACHMENTS_SIZE;
        try {//from  w ww.j  a v a2  s  .c  om
            maxAttachmentSize = Long.parseLong(maxStringValue);
        } catch (NumberFormatException e) {
            Logger.getLogger(RunnableSendHTMLEmail.class.getName()).log(Level.WARNING,
                    "DISABLE_MAX_ATTACHMENTS_SIZE MailSession does not have property "
                            + MAX_ATTACHMENTS_SIZE_PROP_NAME);
        }

        long size = 0;
        for (EmailAttachment attach : attachments) {
            if (maxAttachmentSize != EmailClient.DISABLE_MAX_ATTACHMENTS_SIZE) {
                size += attach.getSize();
                if (size > maxAttachmentSize) {
                    throw new EmailException(
                            "Adjuntos exceden el tamao maximo permitido (" + maxAttachmentSize + "),"
                                    + " pruebe enviando menos archivos adjuntos, o de menor tamao.");
                }
            }
            if (attach.getData() != null) {
                try {
                    email.attach(new ByteArrayDataSource(attach.getData(), attach.getMimeType()),
                            attach.getName(), attach.getMimeType());
                } catch (IOException e) {
                    throw new EmailException("IOException Attachment has errors," + e.getMessage());
                } catch (EmailException e) {
                    throw new EmailException("EmailException Attachment has errors," + e.getMessage());
                }
            } else {
                email.attach(attach);
            }
        }

    }

}