Search in sources :

Example 61 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project portal by ixinportal.

the class SendEmailImpl method sendEmail.

private void sendEmail(EmailServer emailServer, String mailContent, String[] toEmail, String subject, boolean isHtml) throws Exception {
    if (emailServer.getIsAuth()) {
        props.put("mail.smtp.auth", true);
    }
    props.put("mail.transport.protocol", "smtp");
    mailSender.setJavaMailProperties(props);
    mailSender.setUsername(emailServer.getAccountName());
    mailSender.setPassword(emailServer.getAccountPasswd());
    mailSender.setPort(emailServer.getServerPort());
    mailSender.setHost(emailServer.getServerHost());
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, // 防止出现中文乱码,
    "UTF-8");
    // 设置发送方地址
    message.setFrom(emailServer.getAccountName());
    // 设置接收方的email地址
    message.setTo(toEmail);
    // 设置邮件主题
    message.setSubject(subject);
    message.setText(mailContent, isHtml);
    mailSender.send(mimeMessage);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 62 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project opennms by OpenNMS.

the class JavaSendMailer method buildMimeMessage.

/**
 * Builds the mime message.
 *
 * @param msg the sendmail message
 * @return the mime message
 */
public MimeMessage buildMimeMessage(final SendmailMessage msg) {
    MimeMessage mimeMsg = new MimeMessage(m_session);
    if (m_config.getSendmailMessage() != msg) {
        m_config.setSendmailMessage(msg);
    }
    if (m_config.getSendmailMessage() != null) {
        final SendmailMessage configMsg = m_config.getSendmailMessage();
        try {
            final String charset = m_config.getSendmailProtocol() != null ? m_config.getSendmailProtocol().getCharSet() : Charset.defaultCharset().name();
            final MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, false, charset);
            helper.setFrom(configMsg.getFrom());
            helper.setTo(configMsg.getTo());
            helper.setSubject(configMsg.getSubject());
        } catch (final MessagingException e) {
            LOG.warn("found a problem building message: {}", e.getMessage());
        }
    } else {
        LOG.warn("Missing sendmail message configuration. This MIME message will probably be wrong.");
    }
    return mimeMsg;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) SendmailMessage(org.opennms.netmgt.config.javamail.SendmailMessage) MessagingException(javax.mail.MessagingException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 63 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project fake-smtp-server by gessnerfl.

the class TestDataCreator method createHtmlEmail.

private static void createHtmlEmail(int i) {
    try {
        JavaMailSender sender = getEmailSender();
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        helper.setTo("receiver@exmaple.com");
        helper.setFrom("sender@example.com");
        helper.setSubject("Test-Mail " + i);
        helper.setText("<html><head></head><body>This is the test mail number " + i + "</body>", true);
        sender.send(message);
    } catch (MessagingException e) {
        throw new RuntimeException("Failed to create mail", e);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 64 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project PublicCMS-preview by sanluan.

the class SendTask method send.

/**
 * @param toAddress
 * @param fromAddress
 * @param title
 * @param content
 * @param isHtml
 * @return whether to send successfully
 * @throws MessagingException
 */
private boolean send(short siteId, String toAddress, String title, String content, boolean isHtml) throws MessagingException {
    Map<String, String> config = configComponent.getConfigData(siteId, CONFIG_CODE);
    if (CommonUtils.notEmpty(config) && CommonUtils.notEmpty(config.get(CONFIG_FROMADDRESS))) {
        JavaMailSender mailSender = getMailSender(siteId, config);
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(message, false, DEFAULT_CHARSET_NAME);
        messageHelper.setTo(toAddress);
        messageHelper.setFrom(config.get(CONFIG_FROMADDRESS));
        messageHelper.setSubject(title);
        messageHelper.setText(content, isHtml);
        pool.execute(new SendTask(mailSender, message));
        return true;
    }
    return false;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 65 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project gravitee-management-rest-api by gravitee-io.

the class EmailServiceImpl method addResourcesInMessage.

private String addResourcesInMessage(final MimeMessageHelper mailMessage, final String htmlText) throws Exception {
    final Document document = Jsoup.parse(htmlText);
    final List<String> resources = new ArrayList<>();
    final Elements imageElements = document.getElementsByTag("img");
    resources.addAll(imageElements.stream().filter(imageElement -> imageElement.hasAttr("src")).map(imageElement -> {
        final String src = imageElement.attr("src");
        imageElement.attr("src", "cid:" + src);
        return src;
    }).collect(Collectors.toList()));
    final String html = document.html();
    mailMessage.setText(html, true);
    for (final String res : resources) {
        final FileSystemResource templateResource = new FileSystemResource(new File(templatesPath, res));
        mailMessage.addInline(res, templateResource, MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(res));
    }
    return html;
}
Also used : Async(org.springframework.scheduling.annotation.Async) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) Objects.isNull(java.util.Objects.isNull) Template(freemarker.template.Template) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) Logger(org.slf4j.Logger) EmailService(io.gravitee.management.service.EmailService) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) FileSystemResource(org.springframework.core.io.FileSystemResource) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) String.format(java.lang.String.format) Component(org.springframework.stereotype.Component) List(java.util.List) FreeMarkerTemplateUtils.processTemplateIntoString(org.springframework.ui.freemarker.FreeMarkerTemplateUtils.processTemplateIntoString) Configuration(freemarker.template.Configuration) Document(org.jsoup.nodes.Document) EmailNotification(io.gravitee.management.service.EmailNotification) Jsoup(org.jsoup.Jsoup) Elements(org.jsoup.select.Elements) ArrayList(java.util.ArrayList) FreeMarkerTemplateUtils.processTemplateIntoString(org.springframework.ui.freemarker.FreeMarkerTemplateUtils.processTemplateIntoString) FileSystemResource(org.springframework.core.io.FileSystemResource) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) 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