Example usage for org.apache.commons.mail SimpleEmail setHeaders

List of usage examples for org.apache.commons.mail SimpleEmail setHeaders

Introduction

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

Prototype

public void setHeaders(final Map<String, String> map) 

Source Link

Document

Used to specify the mail headers.

Usage

From source file:de.knurt.fam.core.util.mail.UserMailSender.java

private static boolean send(UserMail um) {
    boolean sendSucc = false;
    UserMailSender dse = getInstance();/*from w  ww. j  av a  2s .  co m*/
    if (um.hasBeenSent() == false) {
        if (um.mustBeSendNow()) {
            // prepare
            SimpleEmail email = new SimpleEmail();
            email.setHostName(dse.hostName);
            email.setSmtpPort(dse.smtpPort);
            // mail server using pass
            if (dse.authName != null) {
                email.setAuthentication(dse.authName, dse.authPass);
            }
            Map<String, String> headers = new Hashtable<String, String>();
            // headers.put("Subject", um.getSubject());
            email.setSubject(um.getSubject());
            headers.put("Content-Type", "text/plain; charset=utf-8");
            headers.put("Content-Transfer-Encoding", "base64");
            email.setHeaders(headers);
            boolean creatingSucc = false;
            try {
                email.addTo(um.getTo(), um.getUsername());
                email.setFrom(dse.fromMail, dse.fromName);
                email.setMsg(um.getMsg());
                creatingSucc = true;
            } catch (EmailException ex) {
                FamLog.logException(UserMailSender.class, ex,
                        "creating mail failed::" + um.getTo() + "::" + um.getUsername() + "::" + um.getId(),
                        200904031116l);
            }

            if (creatingSucc && FamConnector.isDev() == false) {
                try {
                    email.send();
                    sendSucc = true;
                } catch (EmailException ex) {
                    FamLog.exception("sending a mail failed: " + ex.getMessage() + "-" + dse.fromMail + "-"
                            + dse.fromName, ex, 200904031018l);
                }
            } else { // just dev mode - do not send any mails
                sendSucc = true;
            }
        }
    } else {
        FamLog.logException(UserMailSender.class,
                new DataIntegrityViolationException("try to send a mail twice"), "try to send a mail twice",
                200908201836l);
    }
    return sendSucc;
}