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:me.j360.base.service.common.MimeMailService.java

/**
 * ??MIME?.//from   www  .  ja  v a2  s . c o  m
 */
public void sendNotificationMail(String userName) {

    try {
        MimeMessage msg = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg, true, DEFAULT_ENCODING);

        helper.setTo("xuminwlt2008@163.com");
        helper.setFrom("system@smart-sales.cn");
        helper.setSubject("");

        String content = generateContent(userName);
        helper.setText(content, true);

        File attachment = generateAttachment();
        helper.addAttachment("mailAttachment.txt", attachment);

        mailSender.send(msg);
        logger.info("HTML??xuminwlt2008@163.com");
    } catch (MessagingException e) {
        logger.error("", e);
    } catch (Exception e) {
        logger.error("??", e);
    }
}

From source file:com.it.j2ee.modules.email.MimeMailService.java

/**
 * ??MIME?./*w  w  w.j  a  v  a  2 s.c  o m*/
 */
public void sendNotificationMail(String userName) {

    try {
        MimeMessage msg = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg, true, DEFAULT_ENCODING);

        helper.setTo("springside3.demo@gmail.com");
        helper.setFrom("springside3.demo@gmail.com");
        helper.setSubject("");

        String content = generateContent(userName);
        helper.setText(content, true);

        File attachment = generateAttachment();
        helper.addAttachment("mailAttachment.txt", attachment);

        mailSender.send(msg);
        logger.info("HTML??springside3.demo@gmail.com");
    } catch (MessagingException e) {
        logger.error("", e);
    } catch (Exception e) {
        logger.error("??", e);
    }
}

From source file:com.oakhole.core.email.MimeMailService.java

/**
 * ??MIME?.//from   www.  j av  a  2s  .co  m
 */
public void sendNotificationMail() {

    try {
        MimeMessage msg = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg, true, DEFAULT_ENCODING);

        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        //
        String content = generateContent();
        helper.setText(content, true);
        //
        File attachment = generateAttachment();
        helper.addAttachment(attachment.getName(), attachment);

        mailSender.send(msg);
        logger.info("HTML??{}", to);
    } catch (MessagingException e) {
        logger.error("", e);
    } catch (Exception e) {
        logger.error("??", e);
    }
}

From source file:com.github.dactiv.common.spring.mail.JavaMailService.java

/**
 * ??/*ww  w. j a va  2 s  .  c o  m*/
 * 
 * @param sendTo ??
 * @param sendFrom ?
 * @param subject 
 * @param content 
 * @param attachment mapkey????value?????
 */
private void doSend(String sendTo, String sendFrom, String subject, String content,
        Map<String, File> attachment) {
    try {
        MimeMessage msg = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(msg, true, encoding);

        helper.setTo(sendTo);
        helper.setFrom(sendFrom);
        helper.setSubject(subject);
        helper.setText(content, true);

        if (!MapUtils.isEmpty(attachment)) {
            for (Entry<String, File> entry : attachment.entrySet()) {
                helper.addAttachment(entry.getKey(), entry.getValue());
            }
        }

        mailSender.send(msg);
        logger.info("???");
    } catch (MessagingException e) {
        logger.error("", e);
    } catch (Exception e) {
        logger.error("??", e);
    }
}

From source file:com.gst.infrastructure.reportmailingjob.service.ReportMailingJobEmailServiceImpl.java

@Override
public void sendEmailWithAttachment(ReportMailingJobEmailData reportMailingJobEmailData) {
    try {/*from   w  w  w  .  j a  v a 2s  . c  o  m*/
        // get all ReportMailingJobConfiguration objects from the database
        this.reportMailingJobConfigurationDataCollection = this.reportMailingJobConfigurationReadPlatformService
                .retrieveAllReportMailingJobConfigurations();

        JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
        javaMailSenderImpl.setHost(this.getGmailSmtpServer());
        javaMailSenderImpl.setPort(this.getGmailSmtpPort());
        javaMailSenderImpl.setUsername(this.getGmailSmtpUsername());
        javaMailSenderImpl.setPassword(this.getGmailSmtpPassword());
        javaMailSenderImpl.setJavaMailProperties(this.getJavaMailProperties());

        MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage();

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

        mimeMessageHelper.setTo(reportMailingJobEmailData.getTo());
        mimeMessageHelper.setText(reportMailingJobEmailData.getText());
        mimeMessageHelper.setSubject(reportMailingJobEmailData.getSubject());

        if (reportMailingJobEmailData.getAttachment() != null) {
            mimeMessageHelper.addAttachment(reportMailingJobEmailData.getAttachment().getName(),
                    reportMailingJobEmailData.getAttachment());
        }

        javaMailSenderImpl.send(mimeMessage);
    }

    catch (MessagingException e) {
        // handle the exception
        e.printStackTrace();
    }
}

From source file:com.enonic.cms.core.mail.AbstractSendMailService.java

public final void sendMail(AbstractMailTemplate template) {
    try {/*w  w  w  .j  a  v a2 s . co  m*/
        MessageSettings settings = new MessageSettings();

        setFromSettings(template, settings);

        settings.setBody(template.getBody());

        final MimeMessageHelper message = createMessage(settings,
                template.isHtml() || !template.getAttachments().isEmpty());
        message.setSubject(template.getSubject());

        final Map<String, InputStream> attachments = template.getAttachments();

        for (final Map.Entry<String, InputStream> attachment : attachments.entrySet()) {
            message.addAttachment(attachment.getKey(),
                    new ByteArrayResource(IOUtils.toByteArray(attachment.getValue())));
        }

        if (template.isHtml()) {
            message.setText("[You need html supported mail client to read this email]", template.getBody());
        } else {
            message.setText(template.getBody());
        }

        if (template.getMailRecipients().size() == 0) {
            this.log.info("No recipients specified, mail not sent.");
        }

        for (MailRecipient recipient : template.getMailRecipients()) {
            if (recipient.getEmail() != null) {
                final MailRecipientType type = recipient.getType();

                switch (type) {
                case TO_RECIPIENT:
                    message.addTo(recipient.getEmail(), recipient.getName());
                    break;
                case BCC_RECIPIENT:
                    message.addBcc(recipient.getEmail(), recipient.getName());
                    break;
                case CC_RECIPIENT:
                    message.addCc(recipient.getEmail(), recipient.getName());
                    break;
                default:
                    throw new RuntimeException("Unknown recipient type: " + type);
                }
            }
        }

        sendMessage(message);
    } catch (Exception e) {
        this.log.error("Failed to send mail", e);
    }
}

From source file:gov.nih.nci.cabig.caaers.esb.client.MessageNotificationService.java

public void sendMail(String[] to, String subject, String content, String attachment) throws Exception {

    MimeMessage message = caaersJavaMailSender.createMimeMessage();
    message.setSubject(subject);//from  ww w  .  j  a v a2  s .  c o m
    message.setFrom(new InternetAddress(configuration.get(Configuration.SYSTEM_FROM_EMAIL)));

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

    if (attachment != null) {
        File f = new File(attachment);
        FileSystemResource file = new FileSystemResource(f);
        helper.addAttachment(file.getFilename(), file);
    }

    caaersJavaMailSender.send(message);

}

From source file:com.autentia.wuija.mail.MailService.java

/**
 * send an e-mail/* ww  w .  j  av a  2s.  c o  m*/
 * 
 * @param to recipient e-mail
 * @param subject the subject of the e-mail
 * @param text the body of the e-mail
 * @param attachments an array of it
 * @throws EmailException if the e-mail cannot be prepare or send.
 */
public void send(String to, String subject, String text, File... attachments) {
    Assert.hasLength(to, "email 'to' needed");
    Assert.hasLength(subject, "email 'subject' needed");
    Assert.hasLength(text, "email 'text' needed");

    if (log.isDebugEnabled()) {
        final boolean usingPassword = StringUtils.isNotBlank(mailSender.getPassword());
        log.debug("Sending email to: '" + to + "' [through host: '" + mailSender.getHost() + ":"
                + mailSender.getPort() + "', username: '" + mailSender.getUsername() + "' usingPassword:"
                + usingPassword + "].");
        log.debug("isActive: " + active);
    }
    if (!active) {
        return;
    }

    final MimeMessage message = mailSender.createMimeMessage();

    try {
        // use the true flag to indicate you need a multipart message
        final MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setFrom(getFrom());
        helper.setText(text);

        if (attachments != null) {
            for (int i = 0; i < attachments.length; i++) {
                // let's attach each file
                final FileSystemResource file = new FileSystemResource(attachments[i]);
                helper.addAttachment(attachments[i].getName(), file);
                if (log.isDebugEnabled()) {
                    log.debug("File '" + file + "' attached.");
                }
            }
        }

    } catch (MessagingException e) {
        final String msg = "Cannot prepare email message : " + subject + ", to: " + to;
        log.error(msg, e);
        throw new MailPreparationException(msg, e);
    }
    this.mailSender.send(message);
}

From source file:com.newinit.email.MailServiceImpl.java

/**
 * envo de email con attachments/*w w w . j  a v  a2  s  .com*/
 *
 * @param to correo electrnico del destinatario
 * @param subject asunto del mensaje
 * @param text cuerpo del mensaje
 * @param attachments ficheros que se anexarn al mensaje
 */
@Override
public void send(String to, String subject, String text, File... attachments) {
    // chequeo de parmetros 
    Assert.hasLength(to, "email 'to' needed");
    Assert.hasLength(subject, "email 'subject' needed");
    Assert.hasLength(text, "email 'text' needed");

    // asegurando la trazabilidad
    if (log.isDebugEnabled()) {
        final boolean usingPassword = !"".equals(mailSender.getPassword());
        log.debug("Sending email to: '" + to + "' [through host: '" + mailSender.getHost() + ":"
                + mailSender.getPort() + "', username: '" + mailSender.getUsername() + "' usingPassword:"
                + usingPassword + "].");
        log.debug("isActive: " + active);
    }
    // el servicio esta activo?
    if (!active) {
        return;
    }

    // plantilla para el envo de email
    final MimeMessage message = mailSender.createMimeMessage();

    try {
        // el flag a true indica que va a ser multipart
        final MimeMessageHelper helper = new MimeMessageHelper(message, true);

        // settings de los parmetros del envo
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setFrom(getFrom());
        helper.setText(text);

        // adjuntando los ficheros
        if (attachments != null) {
            for (int i = 0; i < attachments.length; i++) {
                FileSystemResource file = new FileSystemResource(attachments[i]);
                helper.addAttachment(attachments[i].getName(), file);
                if (log.isDebugEnabled()) {
                    log.debug("File '" + file + "' attached.");
                }
            }
        }

    } catch (MessagingException e) {
        new RuntimeException(e);
    }

    // el envo
    this.mailSender.send(message);
}

From source file:com.hygenics.parser.Send.java

public void run() {

    try {/*from  w  w w. j  a v a2  s  . co  m*/
        log.info("Creating Message");
        MimeMessage message = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        InternetAddress addr = new InternetAddress(fromEmails);

        helper.setFrom(addr);

        for (String email : emails) {
            helper.addTo(new InternetAddress(email));
        }

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

        if (fpath != null) {
            log.info("Attaching File");
            File f = new File(fpath);

            if (f.exists()) {
                helper.addAttachment(fpath, f);
            }
        }

        log.info("Sending Email");
        mailSender.send(message);

    } catch (AddressException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}