Search in sources :

Example 21 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project bamboobsc by billchen198318.

the class MailClientUtils method send.

public static void send(String from, String to, String[] cc, String[] bcc, String[] fileNames, File[] files, String subject, String text) throws MailException, Exception {
    if (mailSender == null) {
        throw new Exception("null mailSender!");
    }
    if (StringUtils.isBlank(from) || StringUtils.isBlank(to)) {
        throw new Exception("from and to is required!");
    }
    if (fileNames != null && files != null) {
        if (fileNames.length != files.length) {
            throw new Exception("File parameter error!");
        }
    }
    MimeMessage message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, true, Constants.BASE_ENCODING);
    helper.setFrom(from);
    helper.setTo(to.endsWith(";") ? to.substring(0, to.length() - 1) : to);
    helper.setSubject(subject);
    helper.setText(text, true);
    if (null != cc && cc.length > 0) {
        helper.setCc(cc);
    }
    if (null != bcc && bcc.length > 0) {
        helper.setBcc(bcc);
    }
    for (int i = 0; fileNames != null && i < fileNames.length; i++) {
        helper.addAttachment(fileNames[i], files[i]);
    }
    mailSender.send(message);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) MailException(org.springframework.mail.MailException)

Example 22 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project uplace.es by Uplace.

the class MailService method sendEmail.

@Async
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) throws MessagingException {
    log.debug("Send email[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}", isMultipart, isHtml, to, subject, content);
    // Prepare message using a Spring helper
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    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 email to User '{}'", to);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Async(org.springframework.scheduling.annotation.Async)

Example 23 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 24 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 25 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)

Aggregations

MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)26 MimeMessage (javax.mail.internet.MimeMessage)24 MessagingException (javax.mail.MessagingException)12 MailException (org.springframework.mail.MailException)9 Date (java.util.Date)7 Properties (java.util.Properties)5 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)5 Async (org.springframework.scheduling.annotation.Async)5 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)3 MimeMessagePreparator (org.springframework.mail.javamail.MimeMessagePreparator)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SendmailMessage (org.opennms.netmgt.config.javamail.SendmailMessage)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)2 Service (org.springframework.stereotype.Service)2 Checks (com.khartec.waltz.common.Checks)1 IOUtilities (com.khartec.waltz.common.IOUtilities)1 StringUtilities (com.khartec.waltz.common.StringUtilities)1 OpcBizException (com.paascloud.provider.exceptions.OpcBizException)1