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

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

Introduction

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

Prototype

public void setCc(String[] cc) throws MessagingException 

Source Link

Usage

From source file:com.netsteadfast.greenstep.util.MailClientUtils.java

public static void send(String from, String to, String cc[], String bcc[], String fileNames[], File files[],
        String subject, String text) throws MailException, Exception {

    if (mailSender == null) {
        throw new Exception("null mailSender!");
    }//from   w w w  .  ja v  a2s .c  om
    if (StringUtils.isBlank(from) || StringUtils.isBlank(to)) {
        throw new Exception("from and to is required!");
    }
    if (fileNames != null && files != null) {
        if (fileNames.length != files.length) {
            throw new Exception("File parameter error!");
        }
    }
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true, Constants.BASE_ENCODING);
    helper.setFrom(from);
    helper.setTo(to.endsWith(";") ? to.substring(0, to.length() - 1) : to);
    helper.setSubject(subject);
    helper.setText(text, true);
    if (null != cc && cc.length > 0) {
        helper.setCc(cc);
    }
    if (null != bcc && bcc.length > 0) {
        helper.setBcc(bcc);
    }
    for (int i = 0; fileNames != null && i < fileNames.length; i++) {
        helper.addAttachment(fileNames[i], files[i]);
    }
    mailSender.send(message);
}

From source file:org.apache.niolex.commons.mail.EmailUtil.java

/**
 * Send an email./*from www.j a va  2s . c o  m*/
 * ????
 *
 * @param from the email sender
 * @param tos the email receivers array
 * @param ccs the carbon copiers array
 * @param title the email title
 * @param text the email body
 * @param attachments the email attachments list
 * @param priority priority from 1-5 the smaller is higher
 * @param isHtml is the text in HTML format or not
 * @param encoding the encoding of email, i.e. "GBK"?"UTF-8"
 * @throws MailException
 * @throws MessagingException
 */
public static void sendMail(String from, String[] tos, String[] ccs, String title, String text,
        List<Pair<String, InputStreamSource>> attachments, String priority, boolean isHtml, String encoding)
        throws MailException, MessagingException {
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, encoding);

    messageHelper.setFrom(from);
    messageHelper.setBcc(from);

    if (ArrayUtils.isEmpty(tos)) {
        throw new IllegalArgumentException("<tos> can not be null or empty!");
    } else {
        messageHelper.setTo(tos);
    }

    if (!ArrayUtils.isEmpty(ccs)) {
        messageHelper.setCc(ccs);
    }

    messageHelper.setSubject(title);
    messageHelper.setText(text, isHtml);

    if (attachments != null) {
        for (Pair<String, InputStreamSource> pair : attachments) {
            messageHelper.addAttachment(pair.a, pair.b);
        }
    }

    mimeMessage = messageHelper.getMimeMessage();
    if (priority != null) {
        mimeMessage.addHeader("X-Priority", priority);
    }

    mailSender.send(mimeMessage);
}

From source file:cherry.foundation.mail.MailSendHandlerImpl.java

private void send(final SimpleMailMessage msg, final AttachmentPreparator preparator) {
    if (preparator == null) {
        mailSender.send(msg);//w w w .j av a 2s. co  m
    } else {
        mailSender.send(new MimeMessagePreparator() {
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
                helper.setTo(msg.getTo());
                helper.setCc(msg.getCc());
                helper.setBcc(msg.getBcc());
                helper.setFrom(msg.getFrom());
                helper.setSubject(msg.getSubject());
                helper.setText(msg.getText());
                preparator.prepare(new Attachment(helper));
            }
        });
    }
}

From source file:com.registryKit.user.emailMessageManager.java

@Async
public void sendEmail(mailMessage messageDetails) throws Exception {

    MimeMessage msg = mailSender.createMimeMessage();

    try {/*from w  ww.j av a2  s . co m*/

        MimeMessageHelper helper = new MimeMessageHelper(msg, true);

        helper.setFrom(messageDetails.getfromEmailAddress());
        helper.setTo(messageDetails.gettoEmailAddress());

        if (messageDetails.getccEmailAddress() != null) {
            helper.setCc(messageDetails.getccEmailAddress());
        }

        helper.setSubject(messageDetails.getmessageSubject());

        helper.setText("", messageDetails.getmessageBody());
        helper.setReplyTo(messageDetails.getfromEmailAddress());

        mailSender.send(msg);

    } catch (Exception e) {
        throw new Exception(e);
    }

}

From source file:com.ut.healthelink.service.impl.emailMessageManagerImpl.java

@Async
public void sendEmail(mailMessage messageDetails) throws Exception {

    MimeMessage msg = mailSender.createMimeMessage();

    try {//from w ww.  j a va 2s.  co m
        MimeMessageHelper helper = new MimeMessageHelper(msg, true);

        helper.setFrom(messageDetails.getfromEmailAddress());
        helper.setTo(messageDetails.gettoEmailAddress());

        if (messageDetails.getccEmailAddress() != null) {
            helper.setCc(messageDetails.getccEmailAddress());
        }

        helper.setSubject(messageDetails.getmessageSubject());

        helper.setText("", messageDetails.getmessageBody());
        helper.setReplyTo(messageDetails.getfromEmailAddress());

        mailSender.send(msg);

    } catch (Exception e) {
        throw new Exception(e);
    }

}

From source file:uk.org.rbc1b.roms.scheduled.EmailScheduledService.java

/**
 * Generates and sends out emails.//w  w  w.  j  av  a  2 s  .  co m
 * @param email the email to send out
 * @throws MessagingException if problem with creating an email sender
 */
private void sendEmail(Email email) throws MessagingException {
    MimeMessage mimeMessage = this.mailGateway.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
    if (email.getReplyTo() != null) {
        helper.setReplyTo(email.getReplyTo());
    }
    helper.setTo(email.getRecipient());
    if (email.getCc() != null) {
        helper.setCc(email.getCc());
    }
    helper.setSubject(email.getSubject());
    helper.setText(email.getText(), email.isHtml());
    this.mailGateway.send(mimeMessage);
}

From source file:gov.nih.nci.cabig.caaers.tools.mail.CaaersJavaMailSender.java

/**
 * This method is used to send an email/*from   w w  w.java2  s.  c  o  m*/
 */
public void sendMail(String[] to, String[] cc, String subject, String content, String[] attachmentFilePaths) {
    if (to == null || to.length == 0 || to[0] == null) {
        return;
    }
    try {
        MimeMessage message = createMimeMessage();
        message.setSubject(subject);

        // use the true flag to indicate you need a multipart message
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(to);
        if (cc != null && cc.length > 0) {
            helper.setCc(cc);
        }
        helper.setText(content);

        for (String attachmentPath : attachmentFilePaths) {
            if (StringUtils.isNotEmpty(attachmentPath)) {
                File f = new File(attachmentPath);
                FileSystemResource file = new FileSystemResource(f);
                helper.addAttachment(file.getFilename(), file);
            }
        }
        send(message);

    } catch (Exception e) {
        if (SUPRESS_MAIL_SEND_EXCEPTION)
            return; //supress the excetion related to email sending
        throw new CaaersSystemException("Error while sending email to " + to[0], e);
    }
}

From source file:org.brushingbits.jnap.email.Email.java

public void prepare(MimeMessage mimeMessage) throws Exception {
    final EmailAccountInfo acc = getAccountInfo();
    boolean multipart = StringUtils.isNotBlank(getHtmlText())
            || (getInlineResources() != null && getInlineResources().size() > 0)
            || (getAttachments() != null && getAttachments().size() > 0);
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, multipart);
    helper.setTo(getTo());//from w ww.  j a v  a 2  s.  c  o  m
    if (getCc() != null) {
        helper.setCc(getCc());
    }
    if (getBcc() != null) {
        helper.setBcc(getBcc());
    }
    helper.setSentDate(new Date());
    helper.setSubject(i18nTextProvider.getText(getSubject()));

    // sender info
    //      if (acc != null && StringUtils.isNotBlank(acc.getFromName())) {
    //         helper.setFrom(getFrom(), i18nTextProvider.getText(acc.getFromName()));
    //      } else {
    //         helper.setFrom(getFrom());
    //      }
    if (acc != null && StringUtils.isNotBlank(acc.getReplyToEmailAddress())) {
        if (StringUtils.isNotBlank(acc.getReplyToName())) {
            helper.setReplyTo(acc.getReplyToEmailAddress(), acc.getReplyToName());
        } else {
            helper.setReplyTo(acc.getReplyToEmailAddress());
        }
    }

    final boolean hasHtmlText = StringUtils.isNotBlank(getHtmlText());
    final boolean hasText = StringUtils.isNotBlank(getText());
    if (hasHtmlText && hasText) {
        helper.setText(getText(), getHtmlText());
    } else if (hasHtmlText || hasText) {
        helper.setText(hasHtmlText ? getHtmlText() : getText());
    }

    // add inline resources
    final Map<String, Resource> inlineRes = this.getInlineResources();
    if (inlineRes != null) {
        for (String cid : inlineRes.keySet()) {
            helper.addInline(cid, inlineRes.get(cid));
        }
    }
    // add attachments
    final Map<String, Resource> attachments = this.getAttachments();
    if (attachments != null) {
        for (String attachmentName : attachments.keySet()) {
            helper.addAttachment(attachmentName, attachments.get(attachmentName));
        }
    }
}

From source file:org.appverse.web.framework.backend.api.services.integration.impl.live.MailIntegrationServiceImpl.java

@Override
@SuppressWarnings("unchecked")
public void sendMail(String from, String[] to, String[] copyTo, String subject, String templateLocation,
        Map model) throws Exception {
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(from);/*from  w  w w  .ja va2 s.  c o m*/
    helper.setTo(to);
    if (copyTo != null) {
        helper.setCc(copyTo);
    }
    helper.setSubject(subject);
    String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, ENCODING, templateLocation,
            model);
    helper.setText(text, true);
    try {
        this.mailSender.send(message);
    } catch (MailSendException sendMailException) {
        Exception[] messageExceptions = sendMailException.getMessageExceptions();
        for (Exception messageException : messageExceptions) {
            if (messageException instanceof SendFailedException) {
                SendFailedException sendFailedException = (SendFailedException) messageException;
                if (sendFailedException.getMessage().equals(INVALID_ADDRESSES)) {
                    Address[] invalidAddresses = sendFailedException.getInvalidAddresses();
                    List<String> invalidAddressList = new ArrayList<String>();
                    for (Address invalidAddress : invalidAddresses) {
                        String invalidAddressString = invalidAddress.toString();
                        invalidAddressList.add(invalidAddressString);
                    }
                    List<String> validToAdressesList = new ArrayList<String>();
                    if (to != null && to.length > 0) {
                        for (String address : to) {
                            if (!invalidAddressList.contains(address)) {
                                validToAdressesList.add(address);
                            }
                        }
                    }

                    List<String> validCopyToAdressesList = new ArrayList<String>();
                    if (copyTo != null && copyTo.length > 0) {
                        for (String address : copyTo) {
                            if (!invalidAddressList.contains(address)) {
                                validCopyToAdressesList.add(address);
                            }
                        }
                    }

                    String[] validToAdressesArray = new String[validToAdressesList.size()];
                    validToAdressesList.toArray(validToAdressesArray);
                    helper.setTo(validToAdressesList.toArray(validToAdressesArray));

                    String[] validCopyToAdressesArray = new String[validCopyToAdressesList.size()];
                    validCopyToAdressesList.toArray(validCopyToAdressesArray);
                    helper.setCc(validCopyToAdressesList.toArray(validCopyToAdressesArray));
                    for (String invalidAddress : invalidAddressList) {
                        logger.error("Mail not sended to " + invalidAddress + "due its a invalid address");
                    }
                    this.mailSender.send(message);
                }
            }
        }
    }
}

From source file:org.appverse.web.framework.backend.api.services.integration.impl.live.MailIntegrationServiceImpl.java

@SuppressWarnings("unchecked")
@Override/*from ww w .jav  a 2s  .  co m*/
public void sendMail(String from, String[] to, String[] copyTo, String subject, String templateLocation,
        Map model, ArrayList<AttachmentDTO> attachments) throws Exception {
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setFrom(from);
    helper.setTo(to);
    if (copyTo != null) {
        helper.setCc(copyTo);
    }
    helper.setSubject(subject);
    String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, ENCODING, templateLocation,
            model);
    helper.setText(text, true);
    for (AttachmentDTO attachmentDTO : attachments) {
        helper.addAttachment(attachmentDTO.getAttachmentName(), attachmentDTO.getAttachment());
    }
    try {
        this.mailSender.send(message);
    } catch (MailSendException sendMailException) {
        Exception[] messageExceptions = sendMailException.getMessageExceptions();
        for (Exception messageException : messageExceptions) {
            if (messageException instanceof SendFailedException) {
                SendFailedException sendFailedException = (SendFailedException) messageException;
                if (sendFailedException.getMessage().equals(INVALID_ADDRESSES)) {
                    Address[] invalidAddresses = sendFailedException.getInvalidAddresses();
                    List<String> invalidAddressList = new ArrayList<String>();
                    for (Address invalidAddress : invalidAddresses) {
                        String invalidAddressString = invalidAddress.toString();
                        invalidAddressList.add(invalidAddressString);
                    }
                    List<String> validToAdressesList = new ArrayList<String>();
                    if (to != null && to.length > 0) {
                        for (String address : to) {
                            if (!invalidAddressList.contains(address)) {
                                validToAdressesList.add(address);
                            }
                        }
                    }

                    List<String> validCopyToAdressesList = new ArrayList<String>();
                    if (copyTo != null && copyTo.length > 0) {
                        for (String address : copyTo) {
                            if (!invalidAddressList.contains(address)) {
                                validCopyToAdressesList.add(address);
                            }
                        }
                    }

                    String[] validToAdressesArray = new String[validToAdressesList.size()];
                    validToAdressesList.toArray(validToAdressesArray);
                    helper.setTo(validToAdressesList.toArray(validToAdressesArray));

                    String[] validCopyToAdressesArray = new String[validCopyToAdressesList.size()];
                    validCopyToAdressesList.toArray(validCopyToAdressesArray);
                    helper.setCc(validCopyToAdressesList.toArray(validCopyToAdressesArray));
                    for (String invalidAddress : invalidAddressList) {
                        logger.error("Mail not sended to " + invalidAddress + "due its a invalid address");
                    }
                    this.mailSender.send(message);
                }
            }
        }
    }
}