Search in sources :

Example 96 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project jprime by bgjug.

the class MailService method sendInvoice.

public void sendInvoice(String to, String subject, String messageText, byte[] pdf, String pdfFilename) throws MessagingException {
    ByteArrayResource bais = null;
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
    helper.setFrom(emailAddress);
    helper.setTo(to);
    helper.setSubject(subject);
    helper.setText(messageText, true);
    bais = new ByteArrayResource(pdf);
    helper.addAttachment(pdfFilename, bais);
    mailSender.send(mimeMessage);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) ByteArrayResource(org.springframework.core.io.ByteArrayResource) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 97 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project Gatekeeper by FINRAOS.

the class EmailService method generateMimeMessage.

private MimeMessage generateMimeMessage(String to, String from, String cc, String subject) throws MessagingException {
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, charset);
    InternetAddress fAddress = new InternetAddress(from);
    message.setTo(InternetAddress.parse(to, true));
    if (cc != null) {
        message.setCc(InternetAddress.parse(cc, true));
    }
    message.setFrom(fAddress);
    message.setSubject(subject);
    return mimeMessage;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 98 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project Gatekeeper by FINRAOS.

the class EmailService method sendEmailWithAttachment.

/**
 * Generates and sends an email
 *
 * @param to                 - Comma separated list of users to send email
 * @param cc                 - Comma separated list of users to cc
 * @param emailSubject       - String containing subject of email
 * @param template           - Template name for email
 * @param contentMap         - Key-value pairing used by template
 * @param attachmentName     - Name of attachment
 * @param attachmentTemplate - Template name for email attachment
 * @param attachmentMap      - Key-value pairing used by attachment template
 * @return - the mimeMessage object
 * @throws Exception
 */
public MimeMessage sendEmailWithAttachment(String to, String from, String cc, String emailSubject, String template, Map<String, Object> contentMap, String attachmentName, String attachmentTemplate, Map<String, Object> attachmentMap, String mimeType) throws Exception {
    logger.info("Sending email to " + to + " with subject " + emailSubject + " and message: " + template);
    MimeMessage mimeMessage = generateMimeMessage(to, from, cc, emailSubject);
    MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, charset);
    message.setText(generateEmailMessage(template, contentMap), true);
    if (attachmentName != null) {
        DataSource data = new ByteArrayDataSource(generateEmailMessage("attachments/" + attachmentTemplate, attachmentMap), mimeType);
        message.addAttachment(attachmentName, data);
    }
    javaMailSender.send(mimeMessage);
    return mimeMessage;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) DataSource(javax.activation.DataSource)

Example 99 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project xm-ms-entity by xm-online.

the class MailService method sendEmail.

// package level for testing
void sendEmail(String to, String subject, String content, String from) {
    log.debug("Send email[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}", false, true, to, subject, content);
    // Prepare message using a Spring helper
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    try {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, false, CharEncoding.UTF_8);
        message.setTo(to);
        message.setFrom(from);
        message.setSubject(subject);
        message.setText(content, true);
        javaMailSender.send(mimeMessage);
        log.debug("Sent email to User '{}'", to);
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.warn("Email could not be sent to user '{}'", to, e);
        } else {
            log.warn("Email could not be sent to user '{}': {}", to, e.getMessage());
        }
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException)

Example 100 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project Spring-Family by Sierou-Java.

the class MainServiceImpl method sendAttachmentsMail.

@Override
public Boolean sendAttachmentsMail(String to, String subject, String content, String filePath) {
    Boolean flag = false;
    MimeMessage message = mailSender.createMimeMessage();
    try {
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
        helper.addAttachment(fileName, file);
        mailSender.send(message);
        logger.info("带附件的邮件已经发送。");
    } catch (MessagingException e) {
        logger.error("发送带附件的邮件时发生异常!", e);
    }
    return flag;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) FileSystemResource(org.springframework.core.io.FileSystemResource) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) File(java.io.File)

Aggregations

MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)111 MimeMessage (javax.mail.internet.MimeMessage)103 MessagingException (javax.mail.MessagingException)47 Async (org.springframework.scheduling.annotation.Async)20 File (java.io.File)16 MailException (org.springframework.mail.MailException)16 FileSystemResource (org.springframework.core.io.FileSystemResource)12 Date (java.util.Date)11 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)11 InternetAddress (javax.mail.internet.InternetAddress)10 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)9 MimeMessagePreparator (org.springframework.mail.javamail.MimeMessagePreparator)9 MailSendException (org.springframework.mail.MailSendException)7 IOException (java.io.IOException)6 Template (freemarker.template.Template)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Properties (java.util.Properties)5 DataSource (javax.activation.DataSource)5 Test (org.junit.Test)5