Search in sources :

Example 66 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project CBEC-B2B by A-Cubic.

the class EmailUtils method sendNormalEmail.

/**
 * 发送邮件
 *
 * @param title
 *            标题
 * @param titleWithName
 *            是否在标题后添加 名称
 * @param content
 *            内容
 * @param contentWithName
 *            是否在内容后添加 名称
 * @param email
 *            接收者的邮箱【注册人】
 */
@Async
private void sendNormalEmail(String title, boolean titleWithName, String content, boolean contentWithName, String email) {
    String dName = "流连优选";
    MimeMessage mimeMessage = null;
    try {
        // 创建要发送的信息
        mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        // 设置 谁发送的
        helper.setFrom(new InternetAddress(username, dName, "UTF-8"));
        // 发给谁 【接收者的邮箱】
        helper.setTo(email);
        // 标题内容
        title = titleWithName ? title + "-" + dName : title;
        // 发送邮件的辩题
        helper.setSubject(title);
        if (contentWithName) {
            content += "<p style='text-align:right'>" + dName + "</p>";
            content += "<p style='text-align:right'>" + Util.curDate("yyyy-MM-dd HH:mm:ss") + "</p>";
        }
        // 发送的内容 是否为html
        helper.setText(content, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
    javaMailSender.send(mimeMessage);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Async(org.springframework.scheduling.annotation.Async)

Example 67 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project free-framework by a601942905git.

the class EmailService method sendHtmlMail.

/**
 * 发送html格式的邮件
 * @param to
 * @param subject
 * @param content
 */
public void sendHtmlMail(String to, String subject, String content) {
    MimeMessage message = sender.createMimeMessage();
    try {
        // true表示需要创建一个multipart message
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        sender.send(message);
        log.info("html邮件已经发送。");
    } catch (MessagingException e) {
        log.error("发送html邮件时发生异常!", e);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 68 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project free-framework by a601942905git.

the class EmailService method sendAttachmentsMail.

/**
 * 发送带附件的邮件
 * @param to
 * @param subject
 * @param content
 * @param filePath
 */
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
    MimeMessage message = sender.createMimeMessage();
    try {
        // true表示需要创建一个multipart message
        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);
        sender.send(message);
        log.info("带附件的邮件已经发送。");
    } catch (MessagingException e) {
        log.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 69 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project FP-PSP-SERVER by FundacionParaguaya.

the class EmailServiceImpl method sendSimpleMessage.

@Override
public void sendSimpleMessage(String to, String subject, String text) {
    try {
        MimeMessage mail = mailSender.createMimeMessage();
        mail.setFrom(appProperties.getSender().getFrom());
        MimeMessageHelper helper = new MimeMessageHelper(mail, true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text, true);
        mailSender.send(mail);
    } catch (Exception e) {
        LOG.error("Mail server connection failed ", e);
        throw new CustomParameterizedException(i18n.translate("email.serverError"));
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) MessagingException(javax.mail.MessagingException) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException)

Example 70 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project FP-PSP-SERVER by FundacionParaguaya.

the class EmailServiceImpl method sendMessageWithAttachment.

@Override
public void sendMessageWithAttachment(String to, String subject, String text, String pathToAttachment) {
    try {
        MimeMessage message = mailSender.createMimeMessage();
        message.setFrom(appProperties.getSender().getFrom());
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text);
        FileSystemResource file = new FileSystemResource(new File(pathToAttachment));
        helper.addAttachment("File", file);
        mailSender.send(message);
    } catch (MessagingException e) {
        LOG.error("Error sending mail ", e);
        throw new CustomParameterizedException(i18n.translate("email.errorSendingMail"));
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) CustomParameterizedException(py.org.fundacionparaguaya.pspserver.common.exceptions.CustomParameterizedException) 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