Search in sources :

Example 56 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project waltz by khartec.

the class WaltzEmailer method sendEmail.

public void sendEmail(String subject, String body, String[] to) {
    Checks.checkNotEmpty(subject, "subject cannot be empty");
    Checks.checkNotEmpty(body, "body cannot be empty");
    Checks.checkNotEmpty(to, "to cannot be empty");
    Checks.checkAll(to, StringUtilities::notEmpty, "email address cannot be empty");
    MimeMessagePreparator preparator = mimeMessage -> {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
        message.setSubject(subject);
        message.setFrom(fromEmail);
        message.setBcc(to);
        message.addAttachment("waltz.png", IOUtilities.getFileResource("/images/waltz.png"));
        message.addAttachment("client-logo", IOUtilities.getFileResource("/templates/images/client-logo.png"));
        Map model = new HashMap();
        model.put("body", body);
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
        try (InputStreamReader templateReader = new InputStreamReader(IOUtilities.getFileResource(DEFAULT_EMAIL_TEMPLATE_LOCATION).getInputStream())) {
            Template template = new Template("template", templateReader, cfg);
            String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
            message.setText(text, true);
        }
    };
    this.mailSender.send(preparator);
}
Also used : MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Autowired(org.springframework.beans.factory.annotation.Autowired) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) HashMap(java.util.HashMap) IOUtilities(com.khartec.waltz.common.IOUtilities) InputStreamReader(java.io.InputStreamReader) Checks(com.khartec.waltz.common.Checks) Value(org.springframework.beans.factory.annotation.Value) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) Configuration(freemarker.template.Configuration) Service(org.springframework.stereotype.Service) Map(java.util.Map) FreeMarkerTemplateUtils(org.springframework.ui.freemarker.FreeMarkerTemplateUtils) Template(freemarker.template.Template) StringUtilities(com.khartec.waltz.common.StringUtilities) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) Configuration(freemarker.template.Configuration) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) StringUtilities(com.khartec.waltz.common.StringUtilities) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) HashMap(java.util.HashMap) Map(java.util.Map) Template(freemarker.template.Template)

Example 57 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project zhcet-web by zhcet-amu.

the class EmailService method constructEmail.

private MimeMessage constructEmail(String emailAddress, String subject, String body, String[] bcc) throws MessagingException {
    MimeMessage mimeMessage = sender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
    helper.setText(body);
    setBasicInfo(helper, senderEmail, emailAddress, subject, bcc);
    return mimeMessage;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 58 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project paascloud-master by paascloud.

the class OptSendMailServiceImpl method getMimeMessage.

private MimeMessage getMimeMessage(String subject, String text, Set<String> to) {
    Preconditions.checkArgument(!PubUtils.isNull(subject, text, to), "参数异常, 邮件信息不完整");
    String[] toArray = setToArray(to);
    Preconditions.checkArgument(PublicUtil.isNotEmpty(toArray), "请输入收件人邮箱");
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper helper;
    try {
        helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(from);
        helper.setTo(toArray);
        helper.setSubject(subject);
        helper.setText(text, true);
    } catch (MessagingException e) {
        log.error("生成邮件消息体, 出现异常={}", e.getMessage(), e);
        throw new OpcBizException(ErrorCodeEnum.OPC10040005);
    }
    return mimeMessage;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) OpcBizException(com.paascloud.provider.exceptions.OpcBizException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 59 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project books by aidanwhiteley.

the class MailClient method sendEmailsToAdminsForNewUsers.

public boolean sendEmailsToAdminsForNewUsers(List<User> newUsers) {
    boolean emailSent = true;
    MimeMessagePreparator messagePreparator = mimeMessage -> {
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
        messageHelper.setFrom(registrationAdminEmailFrom);
        messageHelper.setTo(registrationAdminEmailTo);
        messageHelper.setSubject(registrationAdminEmailTitle);
        messageHelper.setText(prepareAdminNewUsersNotificationEmailContent(newUsers));
    };
    try {
        mailSender.send(messagePreparator);
    } catch (MailException me) {
        emailSent = false;
        LOGGER.error("Failed to send user registration emails for {}", newUsers, me);
    }
    return emailSent;
}
Also used : Value(org.springframework.beans.factory.annotation.Value) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) List(java.util.List) Logger(org.slf4j.Logger) User(com.aidanwhiteley.books.domain.User) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) Service(org.springframework.stereotype.Service) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) MailException(org.springframework.mail.MailException) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) MailException(org.springframework.mail.MailException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 60 with MimeMessageHelper

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

the class SendEmailImpl method sendEmailAndPorxy.

private void sendEmailAndPorxy(EmailServer emailServer, String mailContent, String[] toEmail, String subject, boolean isHtml, Map<String, File> porxyMap) 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);
    System.out.println(porxyMap);
    // 添加附件(授权书)
    for (Map.Entry<String, File> entry : porxyMap.entrySet()) {
        message.addAttachment(entry.getKey(), entry.getValue());
    }
    mailSender.send(mimeMessage);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) HashMap(java.util.HashMap) Map(java.util.Map) 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