Search in sources :

Example 81 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project spring-boot-examples by ityouknow.

the class MailServiceImpl method sendAttachmentsMail.

/**
 * 发送带附件的邮件
 * @param to
 * @param subject
 * @param content
 * @param filePath
 */
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
    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);
        // helper.addAttachment("test"+fileName, file);
        mailSender.send(message);
        logger.info("带附件的邮件已经发送。");
    } catch (MessagingException e) {
        logger.error("发送带附件的邮件时发生异常!", e);
    }
}
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)

Example 82 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project spring-integration by spring-projects.

the class MailSendingMessageHandler method createMailMessageFromByteArrayMessage.

private MailMessage createMailMessageFromByteArrayMessage(Message<byte[]> message) {
    Assert.state(this.mailSender instanceof JavaMailSender, "this adapter requires a 'JavaMailSender' to send a 'MimeMailMessage'");
    String attachmentFileName = message.getHeaders().get(MailHeaders.ATTACHMENT_FILENAME, String.class);
    if (attachmentFileName == null) {
        throw new MessageMappingException(message, "Header '" + MailHeaders.ATTACHMENT_FILENAME + "' is required when mapping a Message with a byte array payload to a MailMessage.");
    }
    Integer multipartMode = message.getHeaders().get(MailHeaders.MULTIPART_MODE, Integer.class);
    if (multipartMode == null) {
        multipartMode = MimeMessageHelper.MULTIPART_MODE_MIXED;
    }
    MimeMessage mimeMessage = ((JavaMailSender) this.mailSender).createMimeMessage();
    try {
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, multipartMode);
        helper.addAttachment(attachmentFileName, new ByteArrayResource(message.getPayload()));
        return new MimeMailMessage(helper);
    } catch (MessagingException e) {
        throw new MessageMappingException(message, "failed to create MimeMessage", e);
    }
}
Also used : MessageMappingException(org.springframework.integration.mapping.MessageMappingException) MimeMailMessage(org.springframework.mail.javamail.MimeMailMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) ByteArrayResource(org.springframework.core.io.ByteArrayResource) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 83 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project flytecnologia-api by jullierme.

the class FlyEmailService method send.

public void send(FlyMessage flyMessage) throws MessagingException {
    /*SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

        simpleMailMessage.setFrom(message.getFrom());
        simpleMailMessage.setSubject(message.getSubject());
        simpleMailMessage.setText(message.getText());


        if(message.getTo() != null){
            simpleMailMessage.setTo(message.getTo()
                    .toArray(new String[message.getTo().size()]));
        }

        if(message.getCc() != null){
            simpleMailMessage.setCc(message.getCc()
                    .toArray(new String[message.getCc().size()]));
        }

        if(message.getBcc() != null){
            simpleMailMessage.setBcc(message.getBcc()
                    .toArray(new String[message.getBcc().size()]));
        }



        javaMailSender.send(simpleMailMessage);*/
    MimeMessage message = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
    helper.setSubject(flyMessage.getSubject());
    helper.setText(flyMessage.getText());
    helper.setFrom(new InternetAddress(flyMessage.getFrom()));
    if (flyMessage.getTo() != null) {
        helper.setTo(flyMessage.getTo().toArray(new String[flyMessage.getTo().size()]));
    }
    if (flyMessage.getCc() != null) {
        helper.setCc(flyMessage.getCc().toArray(new String[flyMessage.getCc().size()]));
    }
    if (flyMessage.getBcc() != null) {
        helper.setBcc(flyMessage.getBcc().toArray(new String[flyMessage.getBcc().size()]));
    }
    if (flyMessage.getFiles() != null) {
        flyMessage.getFiles().forEach((k, v) -> {
            try {
                helper.addAttachment(k, v);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        });
    }
    if (flyMessage.getInputStreamSources() != null) {
        flyMessage.getInputStreamSources().forEach((k, v) -> {
            try {
                helper.addAttachment(k, v);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        });
    }
    javaMailSender.send(message);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 84 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project tutorials by eugenp.

the class MailService method sendEmail.

@Async
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
    log.debug("Send e-mail[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}", isMultipart, isHtml, to, subject, content);
    // Prepare message using a Spring helper
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    try {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, CharEncoding.UTF_8);
        message.setTo(to);
        message.setFrom(jHipsterProperties.getMail().getFrom());
        message.setSubject(subject);
        message.setText(content, isHtml);
        javaMailSender.send(mimeMessage);
        log.debug("Sent e-mail to User '{}'", to);
    } catch (Exception e) {
        log.warn("E-mail could not be sent to user '{}'", to, e);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Async(org.springframework.scheduling.annotation.Async)

Example 85 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project pmph by BCSquad.

the class JavaMailSenderUtil method sendMail.

/**
 * <pre>
 * 功能描述:发送邮件
 * 使用示范:
 *
 * @param title 邮件标题
 * @param content 邮件标题内容
 * @param toMail 收件人
 * @return true/false
 * @throws Exception
 * </pre>
 */
public boolean sendMail(String title, String content, String[] toMail) throws Exception {
    Boolean isOk = false;
    // 创建邮件发送类
    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
    // 设置邮件
    MimeMessage mailMessage = null;
    try {
        mailMessage = JavaMailSenderConfig(senderImpl);
        // 设置邮件
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "UTF-8");
        // 收件人
        messageHelper.setTo(toMail);
        // 发件人
        messageHelper.setFrom(PropsUtil.getString(properties, "mail.smtp.username"));
        // 标题
        messageHelper.setSubject(title);
        // 内容,true 表示启动HTML格式的邮件
        messageHelper.setText(content, true);
        // 发送邮件
        senderImpl.send(mailMessage);
        isOk = true;
        logger.info("邮件发送成功...");
    } catch (MessagingException e) {
        logger.error("邮件发送时发生异常:{}", e.getMessage());
        logger.info("邮件进行重发");
        try {
            Thread.sleep(2000);
            senderImpl.send(mailMessage);
            isOk = true;
        } catch (InterruptedException ie) {
            logger.error("重发邮件时发生异常:{}", ie.getMessage());
        }
    }
    return isOk;
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

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