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

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

Introduction

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

Prototype

public void addHeader(final String name, final String value) 

Source Link

Document

Adds a header ( name, value ) to the headers Map.

Usage

From source file:mailbox.EmailHandler.java

private static void reply(IMAPMessage origin, String username, String emailAddress, String msg) {
    final HtmlEmail email = new HtmlEmail();

    try {/*from  www .  ja v  a2  s . co  m*/
        email.setFrom(Config.getEmailFromSmtp(), Config.getSiteName());
        email.addTo(emailAddress, username);
        String subject;
        if (!origin.getSubject().toLowerCase().startsWith("re:")) {
            subject = "Re: " + origin.getSubject();
        } else {
            subject = origin.getSubject();
        }
        email.setSubject(subject);
        email.setTextMsg(msg);
        email.setCharset("utf-8");
        email.setSentDate(new Date());
        email.addHeader("In-Reply-To", origin.getMessageID());
        email.addHeader("References", origin.getMessageID());
        Mailer.send(email);
        String escapedTitle = email.getSubject().replace("\"", "\\\"");
        String logEntry = String.format("\"%s\" %s", escapedTitle, email.getToAddresses());
        play.Logger.of("mail").info(logEntry);
    } catch (Exception e) {
        Logger.warn("Failed to send an email: " + email + "\n" + ExceptionUtils.getStackTrace(e));
    }
}

From source file:controllers.ProjectApp.java

private static void sendTransferRequestMail(ProjectTransfer pt) {
    HtmlEmail email = new HtmlEmail();
    try {/* ww  w . jav a  2s.c om*/
        String acceptUrl = pt.getAcceptUrl();
        String message = Messages.get("transfer.message.hello", pt.destination) + "\n\n"
                + Messages.get("transfer.message.detail", pt.project.name, pt.newProjectName, pt.project.owner,
                        pt.destination)
                + "\n" + Messages.get("transfer.message.link") + "\n\n" + acceptUrl + "\n\n"
                + Messages.get("transfer.message.deadline") + "\n\n" + Messages.get("transfer.message.thank");

        email.setFrom(Config.getEmailFromSmtp(), pt.sender.name);
        email.addTo(Config.getEmailFromSmtp(), "Yobi");

        User to = User.findByLoginId(pt.destination);
        if (!to.isAnonymous()) {
            email.addBcc(to.email, to.name);
        }

        Organization org = Organization.findByName(pt.destination);
        if (org != null) {
            List<OrganizationUser> admins = OrganizationUser.findAdminsOf(org);
            for (OrganizationUser admin : admins) {
                email.addBcc(admin.user.email, admin.user.name);
            }
        }

        email.setSubject(
                String.format("[%s] @%s wants to transfer project", pt.project.name, pt.sender.loginId));
        email.setHtmlMsg(Markdown.render(message));
        email.setTextMsg(message);
        email.setCharset("utf-8");
        email.addHeader("References", "<" + acceptUrl + "@" + Config.getHostname() + ">");
        email.setSentDate(pt.requested);
        Mailer.send(email);
        String escapedTitle = email.getSubject().replace("\"", "\\\"");
        String logEntry = String.format("\"%s\" %s", escapedTitle, email.getBccAddresses());
        play.Logger.of("mail").info(logEntry);
    } catch (Exception e) {
        Logger.warn("Failed to send a notification: " + email + "\n" + ExceptionUtils.getStackTrace(e));
    }
}

From source file:com.duroty.application.chat.manager.ChatManager.java

/**
 * DOCUMENT ME!//from  www . j  a va2s .  c  o m
 *
 * @param hsession DOCUMENT ME!
 * @param userSender DOCUMENT ME!
 * @param userRecipient DOCUMENT ME!
 */
private void sendMail(Session hsession, javax.mail.Session msession, Users userSender, Users userRecipient,
        String message) {
    try {
        String sender = userSender.getUseUsername();
        String recipient = userRecipient.getUseUsername();

        Identity identitySender = getIdentity(hsession, userSender);
        Identity identityRecipient = getIdentity(hsession, userRecipient);

        HtmlEmail email = new HtmlEmail();
        InternetAddress _from = new InternetAddress(identitySender.getIdeEmail(), identitySender.getIdeName());
        InternetAddress _replyTo = new InternetAddress(identitySender.getIdeReplyTo(),
                identitySender.getIdeName());
        InternetAddress[] _to = MessageUtilities.encodeAddresses(identityRecipient.getIdeEmail(), null);

        if (_from != null) {
            email.setFrom(_from.getAddress(), _from.getPersonal());
        }

        if (_replyTo != null) {
            email.addReplyTo(_replyTo.getAddress(), _replyTo.getPersonal());
        }

        if ((_to != null) && (_to.length > 0)) {
            HashSet aux = new HashSet(_to.length);
            Collections.addAll(aux, _from);
            Collections.addAll(aux, _to);
            email.setTo(aux);
        }

        email.setCharset(charset);
        email.setSubject("Chat " + sender + " >> " + recipient);
        email.setHtmlMsg(message);

        calendar.setTime(new Date());

        String minute = "30";

        if (calendar.get(Calendar.MINUTE) >= 30) {
            minute = "60";
        }

        String value = String.valueOf(calendar.get(Calendar.YEAR))
                + String.valueOf(calendar.get(Calendar.MONTH)) + String.valueOf(calendar.get(Calendar.DATE))
                + String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)) + minute
                + String.valueOf(userSender.getUseIdint() + userRecipient.getUseIdint());
        String reference = "<" + value + ".JavaMail.duroty@duroty" + ">";
        email.addHeader(RFC2822Headers.IN_REPLY_TO, reference);
        email.addHeader(RFC2822Headers.REFERENCES, reference);

        email.addHeader("X-DBox", "CHAT");

        Date now = new Date();
        email.setSentDate(now);

        email.setMailSession(msession);
        email.buildMimeMessage();

        MimeMessage mime = email.getMimeMessage();

        int size = MessageUtilities.getMessageSize(mime);

        if (controlQuota(hsession, userSender, size)) {
            //messageable.saveSentMessage(null, mime, userSender);
            Thread thread = new Thread(new SendMessageThread(email));
            thread.start();
        }
    } catch (UnsupportedEncodingException e) {
    } catch (MessagingException e) {
    } catch (EmailException e) {
    } catch (Exception e) {
    }
}

From source file:nl.b3p.kaartenbalie.struts.Mailer.java

public ActionMessages send(ActionMessages errors)
        throws AddressException, MessagingException, IOException, Exception {

    HtmlEmail email = new HtmlEmail();

    String[] ds = null;//from   w  w  w.  ja v  a2s  .c  o m
    if (mailTo != null && mailTo.trim().length() != 0) {
        ds = mailTo.split(",");
        for (int i = 0; i < ds.length; i++) {
            email.addTo(ds[i]);
        }
    }
    if (mailCc != null && mailCc.trim().length() != 0) {
        ds = mailCc.split(",");
        for (int i = 0; i < ds.length; i++) {
            email.addCc(ds[i]);
        }
    }
    if (mailBcc != null && mailBcc.trim().length() != 0) {
        ds = mailBcc.split(",");
        for (int i = 0; i < ds.length; i++) {
            email.addBcc(ds[i]);
        }
    }

    email.setFrom(mailFrom);
    email.setSubject(subject);
    email.setHostName(mailHost);

    if (isReturnReceipt()) {
        email.addHeader("Disposition-Notification-To", mailFrom);
    }

    if (attachmentName == null) {
        attachmentName = "attachment";
    }
    if (attachment != null) {
        URL attachUrl = null;
        try {
            attachUrl = new URL(attachment);
            email.attach(attachUrl, attachmentName, attachmentName);
        } catch (MalformedURLException mfue) {
        }
    }
    if (attachmentDataSource != null) {
        email.attach(attachmentDataSource, attachmentName, attachmentName);
    }

    email.setMsg(createHTML());

    // send the email
    email.send();

    return errors;
}

From source file:tilda.utils.MailUtil.java

/**
 * //from   ww  w .  j  a  v a  2  s.c  om
 * @param SmtpInfo A string such as smtp.mydomain.com:422:ssl to connect to an SMTP server
 * @param From the user ID used to send emails from
 * @param Password The password for the account we send emails from
 * @param To Destination email(s)
 * @param Cc CC email(s)
 * @param Bcc BCC emails(s)
 * @param Subject The Subject
 * @param Message The message (HTML allowed)
 * @param Urgent Whether to send the message as urgent or not.
 * @return
 */
public static boolean send(String SmtpInfo, String From, String Password, String[] To, String[] Cc,
        String[] Bcc, String Subject, String Message, boolean Urgent) {
    String LastAddress = null;
    try {
        HtmlEmail email = new HtmlEmail();

        String[] parts = SmtpInfo.split(":");
        email.setHostName(parts[0]);
        if (parts.length > 1) {
            if (parts.length > 2 && parts[2].equalsIgnoreCase("ssl") == true) {
                email.setSslSmtpPort(parts[1]);
                email.setSSLOnConnect(true);
            } else {
                email.setSmtpPort(Integer.parseInt(parts[1]));
            }
        }

        email.setAuthentication(From, Password);
        email.setSubject(Subject);

        LOG.debug("Sending an email '" + email.getSubject() + "'.");
        if (To != null)
            for (String s : To) {
                if (TextUtil.isNullOrEmpty(s) == true)
                    continue;
                LastAddress = s;
                email.addTo(s);
            }
        if (Cc != null)
            for (String s : Cc) {
                if (TextUtil.isNullOrEmpty(s) == true)
                    continue;
                LastAddress = s;
                email.addCc(s);
            }
        if (Bcc != null)
            for (String s : Bcc) {
                if (TextUtil.isNullOrEmpty(s) == true)
                    continue;
                LastAddress = s;
                email.addBcc(s);
            }
        if (LastAddress == null) {
            LOG.debug("No recipient. Not sending anything.");
            return true;
        }
        email.setFrom(From);
        LastAddress = From;
        email.setHtmlMsg(Message);
        if (Urgent == true)
            email.addHeader("X-Priority", "1");
        LastAddress = null;
        email.send();
        return true;
    } catch (EmailException E) {
        if (LastAddress != null)
            LOG.debug("Email address '" + LastAddress + "' seems to be invalid.");
        LOG.error(E);
        return false;
    }
}