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:org.wso2.security.tools.automation.manager.handler.MailHandler.java

/**
 * Send an email with an attachment//w  w  w .j av a 2  s. c  o  m
 *
 * @param to                 To whom the email is sent
 * @param subject            Email subject
 * @param body               Email body
 * @param inputStream        Input stream of an attachment
 * @param attachmentFileName Attachment file name
 * @throws MessagingException Exceptions thrown by the Messaging classes
 * @throws IOException        Signals that an I/O exception of some sort has occurred
 */
public void sendMail(String to, String subject, String body, InputStream inputStream, String attachmentFileName)
        throws MessagingException, IOException {
    LOGGER.info("Sending email");
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
    mimeMessageHelper.setSubject(subject);
    mimeMessageHelper.setTo(to);
    mimeMessageHelper.setText(body);
    mimeMessageHelper.addAttachment(attachmentFileName,
            new ByteArrayResource(IOUtils.toByteArray(inputStream)));
    mailSender.send(mimeMessageHelper.getMimeMessage());
}

From source file:siddur.solidtrust.wok.WokController.java

@Scheduled(cron = "0 5 1 2 * ?") //01:05 2th monthly
@RequestMapping("notify")
public void sendMail() throws Exception {
    Calendar today = Calendar.getInstance();
    int year = today.get(Calendar.YEAR);
    int month = today.get(Calendar.MONTH);
    String filename = year + "-" + month + ".xlsx";
    File file = new File(FileSystemUtil.getWokDir(), filename);
    generateFile(year, month, file);/*from  w  w w  . j  ava 2s . c om*/

    log4j.info("Send email for WOK records: " + file.getName());
    MimeMessage message = mailSender.createMimeMessage();

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

    helper.setFrom("gbg1_spsms_gtrak@pactera-pgs-mail.chinacloudapp.cn", "Solidtrust Admin");
    helper.setTo(SolidtrustConstants.WOK_EMAIL);
    helper.addCc(SolidtrustConstants.BEN_EMAIL);
    helper.addCc(SolidtrustConstants.MY_EMAIL);
    helper.setSubject(MessageFormat.format("WOK[{0}]", (year + "-" + month)));

    // use the true flag to indicate the text included is HTML
    helper.setText("<html><body>Here is WOK data of last month. Thanks.</body></html>", true);

    // let's include the infamous windows Sample file (this time copied to c:/)
    FileSystemResource res = new FileSystemResource(file);
    helper.addAttachment(file.getName(), res);

    mailSender.send(message);
}

From source file:ubic.gemma.util.MailEngineImpl.java

/**
 * Convenience method for sending messages with attachments.
 * /*from  w ww .ja  v  a2 s  .  c  o  m*/
 * @param emailAddresses
 * @param resource to be attached
 * @param bodyText
 * @param subject
 * @param attachmentName
 * @throws MessagingException
 * @author Ben Gill
 */
@Override
public void sendMessage(String[] emailAddresses, 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(emailAddresses);
    helper.setText(bodyText);
    helper.setSubject(subject);

    helper.addAttachment(attachmentName, resource);

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