Example usage for org.apache.commons.mail HtmlEmail sendMimeMessage

List of usage examples for org.apache.commons.mail HtmlEmail sendMimeMessage

Introduction

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

Prototype

public String sendMimeMessage() throws EmailException 

Source Link

Document

Sends the previously created MimeMessage to the SMTP server.

Usage

From source file:fr.gouv.culture.thesaurus.util.MailUtil.java

/**
 * /*from  w w w. j a  v  a  2 s .c o m*/
 * Envoi l'email.
 * 
 * @throws EmailException
 *             Erreur lors de l'envoi de l'email.
 */
public void send() throws EmailException {

    if (hostProperty == null) {
        log.error("Session email non initialise : envoi des emails impossible.");
        return;
    }

    HtmlEmail email = new HtmlEmail();
    email.setHostName(hostProperty);

    // To
    if (to != null) {
        for (String adresse : to) {
            email.addTo(adresse);
        }
    }

    // Cc
    if (cc != null) {
        for (String adresse : cc) {
            email.addCc(adresse);
        }
    }

    // Cci
    if (cci != null) {
        for (String adresse : cci) {
            email.addBcc(adresse);
        }
    }

    // Subject
    email.setSubject(subjectPrefix != null ? subjectPrefix + subject : subject);

    // From
    email.setFrom(StringUtils.isNotEmpty(from) ? from : defaultFrom);

    // Message & Html
    if (message != null) {
        email.setTextMsg(message);
    }
    if (html != null) {
        email.setHtmlMsg(html);
    }

    if (StringUtils.isNotEmpty(this.charset)) {
        email.setCharset(this.charset);
    }

    email.buildMimeMessage();

    // Attachments
    for (AttachmentBean attachement : attachments) {
        email.attach(attachement.getDataSource(), attachement.getName(), attachement.getDescription());
    }

    email.sendMimeMessage();

}

From source file:com.moss.error_reporting.server.Notifier.java

public void sendEmailNotification(ReportId id, ErrorReport report) {
    try {/*  w  w w .j  ava2  s.  co m*/
        HtmlEmail email = new HtmlEmail();
        prepEmail(email, id);

        StringBuffer body = new StringBuffer();
        body.append("<html><body>");

        List<ErrorReportChunk> chunks = report.getReportChunks();
        for (int x = 0; x < chunks.size(); x++) {

            ErrorReportChunk chunk = chunks.get(x);
            body.append("<div style=\"padding:5px;text-align:center;border:1px solid black;\">");
            body.append("<span style=\"font-weight:bold;\">" + chunk.getName() + "</span>");
            body.append("<span style=\"font-style:italic;\">[" + chunk.getMimeType() + "</span>]");
            body.append("</div>");
            String mimeType = chunk.getMimeType();
            if (mimeType != null && mimeType.equals("text/plain")) {
                body.append(
                        "<div style=\"border:1px solid black;border-top:0px;padding:5px;background:#dddddd;\"><pre>");
                boolean needsTruncation = chunk.getData().length > MAX_MESSAGE_LENGTH;
                int length = needsTruncation ? MAX_MESSAGE_LENGTH : chunk.getData().length;

                body.append(new String(chunk.getData(), 0, length));
                if (needsTruncation) {
                    body.append("\n");
                    body.append((chunk.getData().length - MAX_MESSAGE_LENGTH) + " more bytes");
                }
                body.append("</pre></div>");
            } else if (mimeType != null && mimeType.equals("application/zip")) {
                body.append(
                        "<div style=\"border:1px solid black;border-top:0px;padding:5px;background:#dddddd;\"><pre>");
                ZipInputStream in = new ZipInputStream(new ByteArrayInputStream(chunk.getData()));
                try {
                    new ZipToTextTool().run(in, body);
                } catch (IOException e) {
                    e.printStackTrace();
                    body.append("Error Reading Zip:" + e.getMessage());
                }
                body.append("</pre></div>");
            }
            if (x != chunks.size() - 1 && chunks.size() > 1)
                body.append("<br/><br/>");

        }
        body.append("</body></html>");

        //         email.setHtmlMsg("<html><body>Hello World</body></html>");
        email.setHtmlMsg(body.toString());
        //         email.setTextMsg("HTML EMAIL");
        email.buildMimeMessage();
        email.sendMimeMessage();

    } catch (EmailException e) {
        e.printStackTrace();
    }
}