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

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

Introduction

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

Prototype

public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource)
        throws MessagingException 

Source Link

Document

Add an attachment to the MimeMessage, taking the content from an org.springframework.core.io.InputStreamResource .

Usage

From source file:com.jaspersoft.jasperserver.api.engine.scheduling.quartz.ReportExecutionJobAlertImpl.java

protected static void attachException(MimeMessageHelper messageHelper, ExceptionInfo exceptionInfo)
        throws MessagingException {
    Throwable exception = exceptionInfo.getException();
    if (exception == null) {
        return;/*from   w  ww. ja v  a2 s .co m*/
    }

    ByteArrayOutputStream bufOut = new ByteArrayOutputStream();
    PrintStream printOut = new PrintStream(bufOut);
    exception.printStackTrace(printOut);
    printOut.flush();

    String attachmentName = "exception_" + System.identityHashCode(exception) + ".txt";
    messageHelper.addAttachment(attachmentName, new ByteArrayResource(bufOut.toByteArray()));
}

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!");
    }//w w w.ja  v  a 2 s. c o  m
    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./*  w  ww  .ja v a2 s.  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:com.rxx.common.util.MailUtil.java

/**
 * html//from   www. j a  v  a2 s.c  o m
 *
 * @param host 
 * @param port
 * @param userName
 * @param password
 * @param title
 * @param contenthtml
 * @param fileslist<Map<key:,value:>>
 * @param toUser
 * @throws javax.mail.MessagingException
 */
public static void sendAttached(String host, int port, String userName, String password, String title,
        String content, List<Map<String, String>> files, String[] toUser)
        throws MessagingException, javax.mail.MessagingException {
    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();

    // mail server
    senderImpl.setHost(host);
    // ,html
    MimeMessage mailMessage = senderImpl.createMimeMessage();
    // boolean,MimeMessageHelpertrue
    // multipart true html
    MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");

    // 
    messageHelper.setTo(toUser);
    messageHelper.setFrom(userName);
    messageHelper.setSubject(title);
    // true HTML
    messageHelper.setText(content, true);

    for (Map<String, String> filePath : files) {
        Iterator<String> it = filePath.keySet().iterator();
        String fileName = it.next();
        FileSystemResource file = new FileSystemResource(new File(filePath.get(fileName)));
        // 
        messageHelper.addAttachment(fileName, file);
    }

    senderImpl.setUsername(userName); // ,username
    senderImpl.setPassword(password); // , password
    Properties prop = new Properties();
    prop.put("mail.smtp.auth", "true"); // true,
    prop.put("mail.smtp.timeout", "25000");
    senderImpl.setJavaMailProperties(prop);
    // 
    senderImpl.send(mailMessage);
}

From source file:org.trpr.platform.integration.impl.email.SpringMailSender.java

/**
 * Interface method implementation. Sends an email using the specified values and the configured mail sender.
 * @see org.trpr.platform.integration.spi.email.MailSender#sendMail(java.lang.String, java.lang.String[], java.lang.String, java.net.URL)
 *///from www.  jav a2  s.  c om
public void sendMail(final String senderAddress, final String subject, final String[] recipients,
        final String message, final URL attachmentURL) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws Exception {
            InternetAddress[] recipientAddresses = new InternetAddress[recipients.length];
            for (int i = 0; i < recipientAddresses.length; i++) {
                recipientAddresses[i] = new InternetAddress(recipients[i]);
            }
            mimeMessage.setRecipients(Message.RecipientType.TO, recipientAddresses);
            mimeMessage.setFrom(new InternetAddress(senderAddress));
            mimeMessage.setSubject(subject);
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); // multi-part flag is set to true for accommodating attachments
            if (attachmentURL != null) {
                helper.addAttachment(attachmentURL.getFile(), new FileSystemResource(attachmentURL.toString()));
            }
            helper.setText(message);
        }
    };
    this.mailSender.send(preparator);
}

From source file:ca.airspeed.demo.testingemail.EmailService.java

public void sendWithAttachments(InternetAddress to, String subject, String textBody,
        List<FileSystemResource> fileList) throws MessagingException {
    notNull(mailSender, String.format("Check your configuration, I need an instance of %s.",
            JavaMailSender.class.getCanonicalName()));
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setTo(to);//from   w w  w. jav  a 2s  . com
    helper.setFrom(senderAddress);
    helper.setSubject(subject);
    helper.setText(textBody);
    for (FileSystemResource resource : fileList) {
        helper.addAttachment(resource.getFilename(), resource.getFile());
    }
    mailSender.send(message);
}

From source file:com.gisgraphy.service.MailEngine.java

/**
 * Convenience method for sending messages with attachments.
 * /*from  w  w  w .j  a v a 2  s. c om*/
 * @param recipients
 *                array of e-mail addresses
 * @param sender
 *                e-mail address of sender
 * @param resource
 *                attachment from classpath
 * @param bodyText
 *                text in e-mail
 * @param subject
 *                subject of e-mail
 * @param attachmentName
 *                name for attachment
 * @throws MessagingException
 *                 thrown when can't communicate with SMTP server
 */
public void sendMessage(String[] recipients, String sender, ClassPathResource resource, String bodyText,
        String subject, String attachmentName) throws MessagingException {
    MimeMessage message = ((JavaMailSenderImpl) mailSender).createMimeMessage();

    // use the true flag to indicate you need a multipart message
    MimeMessageHelper helper = new MimeMessageHelper(message, true);

    helper.setTo(recipients);
    helper.setFrom(sender);
    helper.setText(bodyText);
    helper.setSubject(subject);

    helper.addAttachment(attachmentName, resource);

    ((JavaMailSenderImpl) mailSender).send(message);
}

From source file:com.yoncabt.ebr.executor.YoncaMailSender.java

public void send(String to, String text, Map<String, byte[]> attachments) throws MessagingException {

    MimeMessage mm = mailSender.createMimeMessage();
    MimeMessageHelper mmh = new MimeMessageHelper(mm, true);
    mmh.setTo(to);//from w w  w .  ja v  a  2  s .c  om
    mmh.setText(text);
    for (Map.Entry<String, byte[]> entrySet : attachments.entrySet()) {
        String key = entrySet.getKey();
        byte[] value = entrySet.getValue();
        ByteArrayResource isr = new ByteArrayResource(value);
        mmh.addAttachment(key, isr);

    }
    mailSender.send(mm);
}

From source file:com.pamarin.income.controller.SuggestionCtrl.java

private void sendEmail2Admin(final File attachFile) {
    mailSender.send(new MailCallback() {

        @Override// w  w w  .  j a  v  a 2s .c  om
        public void execute(MimeMessageHelper helper) throws Exception {
            if (attachFile != null) {
                FileSystemResource file = new FileSystemResource(attachFile);
                helper.addAttachment(attachFile.getName(), file);
            }

            helper.setSubject("?" + app.getName());
            helper.setText(getSuggestion().getType() + " : " + getSuggestion().getMessage());
            helper.setTo(destinationReceiveEmail);
        }
    });
}

From source file:org.musicrecital.service.MailEngine.java

/**
 * Convenience method for sending messages with attachments.
 * /*from   w  ww  .  ja v  a  2 s.  co m*/
 * @param recipients array of e-mail addresses
 * @param sender e-mail address of sender
 * @param resource attachment from classpath
 * @param bodyText text in e-mail
 * @param subject subject of e-mail
 * @param attachmentName name for attachment
 * @throws MessagingException thrown when can't communicate with SMTP server
 */
public void sendMessage(String[] recipients, String sender, ClassPathResource resource, String bodyText,
        String subject, String attachmentName) throws MessagingException {
    MimeMessage message = ((JavaMailSenderImpl) mailSender).createMimeMessage();

    // use the true flag to indicate you need a multipart message
    MimeMessageHelper helper = new MimeMessageHelper(message, true);

    helper.setTo(recipients);

    // use the default sending if no sender specified
    if (sender == null) {
        helper.setFrom(defaultFrom);
    } else {
        helper.setFrom(sender);
    }

    helper.setText(bodyText);
    helper.setSubject(subject);

    helper.addAttachment(attachmentName, resource);

    ((JavaMailSenderImpl) mailSender).send(message);
}