Example usage for org.springframework.mail.javamail MimeMessageHelper setPriority

List of usage examples for org.springframework.mail.javamail MimeMessageHelper setPriority

Introduction

In this page you can find the example usage for org.springframework.mail.javamail MimeMessageHelper setPriority.

Prototype

public void setPriority(int priority) throws MessagingException 

Source Link

Document

Set the priority ("X-Priority" header) of the message.

Usage

From source file:hornet.framework.mail.MailServiceImpl.java

/**
 * Adds the extra smtp field.//from w ww  . jav a2s  .c  o  m
 *
 * @param paramMap
 *            the param map
 * @param helper
 *            the helper
 * @throws MessagingException
 *             the messaging exception
 */
protected void addExtraSmtpField(final Map<String, Object> paramMap, final MimeMessageHelper helper)
        throws MessagingException {

    if (paramMap != null) {
        if (paramMap.containsKey(SMTP_HEADER_REPLYTO)) {
            helper.setReplyTo((String) paramMap.get(SMTP_HEADER_REPLYTO));
        }
        if (paramMap.containsKey(SMTP_HEADER_BCC)) {
            helper.setBcc((String) paramMap.get(SMTP_HEADER_BCC));
        }
        if (paramMap.containsKey(SMTP_HEADER_CC)) {
            helper.setCc((String) paramMap.get(SMTP_HEADER_CC));
        }
        if (paramMap.containsKey(SMTP_HEADER_PRIORITY)) {
            helper.setPriority((Integer) paramMap.get(SMTP_HEADER_PRIORITY));
        }
        if (paramMap.containsKey(SMTP_HEADER_RETURNPATH)) {
            ((SMTPMessage) helper.getMimeMessage())
                    .setEnvelopeFrom((String) paramMap.get(SMTP_HEADER_RETURNPATH));
        }
        if (paramMap.containsKey(SMTP_ATTACHMENT_CONTENU)) {
            helper.addAttachment((String) paramMap.get(SMTP_ATTACHMENT_NOM),
                    (InputStreamSource) paramMap.get(SMTP_ATTACHMENT_CONTENU));
        }
    }
}

From source file:com.sfs.dao.EmailMessageDAOImpl.java

/**
 * Send an email message using the configured Spring sender. On success
 * record the sent message in the datastore for reporting purposes
 *
 * @param emailMessage the email message
 *
 * @throws SFSDaoException the SFS dao exception
 *//*w w  w  .  j  a v  a 2  s. c  o  m*/
public final void send(final EmailMessageBean emailMessage) throws SFSDaoException {

    // Check to see whether the required fields are set (to, from, message)
    if (StringUtils.isBlank(emailMessage.getTo())) {
        throw new SFSDaoException("Error recording email: Recipient " + "address required");
    }
    if (StringUtils.isBlank(emailMessage.getFrom())) {
        throw new SFSDaoException("Error recording email: Email requires " + "a return address");
    }
    if (StringUtils.isBlank(emailMessage.getMessage())) {
        throw new SFSDaoException("Error recording email: No email " + "message specified");
    }
    if (javaMailSender == null) {
        throw new SFSDaoException("The EmailMessageDAO has not " + "been configured");
    }

    // Prepare the email message
    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = null;
    if (emailMessage.getHtmlMessage()) {
        try {
            helper = new MimeMessageHelper(message, true, "UTF-8");
        } catch (MessagingException me) {
            throw new SFSDaoException("Error preparing email for sending: " + me.getMessage());
        }
    } else {
        helper = new MimeMessageHelper(message);
    }
    if (helper == null) {
        throw new SFSDaoException("The MimeMessageHelper cannot be null");
    }
    try {
        if (StringUtils.isNotBlank(emailMessage.getTo())) {
            StringTokenizer tk = new StringTokenizer(emailMessage.getTo(), ",");
            while (tk.hasMoreTokens()) {
                String address = tk.nextToken();
                helper.addTo(address);
            }
        }
        if (StringUtils.isNotBlank(emailMessage.getCC())) {
            StringTokenizer tk = new StringTokenizer(emailMessage.getCC(), ",");
            while (tk.hasMoreTokens()) {
                String address = tk.nextToken();
                helper.addCc(address);
            }
        }
        if (StringUtils.isNotBlank(emailMessage.getBCC())) {
            StringTokenizer tk = new StringTokenizer(emailMessage.getBCC(), ",");
            while (tk.hasMoreTokens()) {
                String address = tk.nextToken();
                helper.addBcc(address);
            }
        }
        if (StringUtils.isNotBlank(emailMessage.getFrom())) {
            if (StringUtils.isNotBlank(emailMessage.getFromName())) {
                try {
                    helper.setFrom(emailMessage.getFrom(), emailMessage.getFromName());
                } catch (UnsupportedEncodingException uee) {
                    dataLogger.error("Error setting email from", uee);
                }
            } else {
                helper.setFrom(emailMessage.getFrom());
            }
        }
        helper.setSubject(emailMessage.getSubject());
        helper.setPriority(emailMessage.getPriority());
        if (emailMessage.getHtmlMessage()) {
            final String htmlText = emailMessage.getMessage();
            String plainText = htmlText;
            try {
                ConvertHtmlToText htmlToText = new ConvertHtmlToText();
                plainText = htmlToText.convert(htmlText);
            } catch (Exception e) {
                dataLogger.error("Error converting HTML to plain text: " + e.getMessage());
            }
            helper.setText(plainText, htmlText);
        } else {
            helper.setText(emailMessage.getMessage());
        }
        helper.setSentDate(emailMessage.getSentDate());

    } catch (MessagingException me) {
        throw new SFSDaoException("Error preparing email for sending: " + me.getMessage());
    }

    // Append any attachments (if an HTML email)
    if (emailMessage.getHtmlMessage()) {
        for (String id : emailMessage.getAttachments().keySet()) {
            final Object reference = emailMessage.getAttachments().get(id);

            if (reference instanceof File) {
                try {
                    FileSystemResource res = new FileSystemResource((File) reference);
                    helper.addInline(id, res);
                } catch (MessagingException me) {
                    dataLogger.error("Error appending File attachment: " + me.getMessage());
                }
            }
            if (reference instanceof URL) {
                try {
                    UrlResource res = new UrlResource((URL) reference);
                    helper.addInline(id, res);
                } catch (MessagingException me) {
                    dataLogger.error("Error appending URL attachment: " + me.getMessage());
                }
            }
        }
    }

    // If not in debug mode send the email
    if (!debugMode) {
        deliver(emailMessage, message);
    }
}

From source file:org.craftercms.social.services.system.EmailService.java

public void sendEmail(final Profile toSend, final StringWriter writer, final String subject,
        final String contextId) throws SocialException {
    Map<String, Object> emailSettings = getEmailSettings(contextId);
    JavaMailSender sender = getSender(contextId);
    MimeMessage message = sender.createMimeMessage();
    String realSubject = subject;
    if (StringUtils.isBlank(realSubject)) {
        realSubject = generateSubjectString(emailSettings.get("subject").toString());
    }/* w  w w  .jav  a2 s  . c  o  m*/
    try {
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(toSend.getEmail());
        helper.setReplyTo(emailSettings.get("replyTo").toString());
        helper.setFrom(emailSettings.get("from").toString());
        helper.setSubject(realSubject);

        helper.setPriority(NumberUtils.toInt(emailSettings.get("priority").toString(), 4));
        helper.setText(writer.toString(), true);
        message.setHeader("Message-ID", String.format("[%s]-%s-%s-%s", RandomStringUtils.randomAlphanumeric(5),
                contextId, realSubject, toSend.getId()));
        sender.send(message);
    } catch (MessagingException e) {
        throw new SocialException("Unable to send Email to " + toSend.getEmail(), e);
    }
}