繁体   English   中英

使用 Spring 和 JavaMail 发送合并的纯文本/HTML 邮件

[英]Send combined plain text/HTML mail with Spring and JavaMail

我目前正在使用 JavaMail 和 Spring 以 HTML 格式发送电子邮件。 碰巧,HTML是由我手头的一些Velocity模板生成的,发送代码大致如下:

MimeMessagePreparator preparator = new MimeMessagePreparator() {

    @Override public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");

            message.setSubject(msgInfo.getSubject());
            message.setFrom(msgInfo.getFrom());
            message.setReplyTo(msgInfo.getFrom());
            message.setTo(address);
            message.setText(someText, true);
    }
}

mailSender.send(preparator);

这工作得很好,但它发送的邮件只有一个部分作为text/html 我需要的是使用纯文本部分以多部分替代方式发送它。 有没有办法使用 Spring 和 JavaMail 以自动方式执行此操作?


聚苯乙烯

在我以前用 Visual Basic 和CDONTS编程时,这是内置的,但我似乎找不到用 Java 来做的简单方法。 纯文本版本看起来不错并不重要,它必须存在。 我试图避免的是为此而必须维护完整的第二组 Velocity 模板。

为了同时发送 Text 和 HTML 部分,您需要使用不同的setText()方法:

public void setText(String plainText, String htmlText)

如果将纯文本设置为 HTML 内容,则可能需要解析 HTML 以删除 HTML 标记。

try {

    // Email data
    String Email_Id = "samarthaniket16@gmail.com"; // change to your
                                                    // email ID
    String password = "Your PSWD"; // change to your password
    String recipient_mail_id = "samarthaniket16@gmail.com"; // change to
                                                            // recipient
                                                            // email id
    String mail_subject = "Attendance Report";

    // Set mail properties
    Properties props = System.getProperties();
    String host_name = "smtp.gmail.com";
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host_name);
    props.put("mail.smtp.user", Email_Id);
    props.put("mail.smtp.password", password);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");

    Session session = Session.getDefaultInstance(props);
    MimeMessage message = new MimeMessage(session);

    try {
        // Set email data
        message.setFrom(new InternetAddress(Email_Id));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient_mail_id));
        message.setSubject(mail_subject);
        MimeMultipart multipart = new MimeMultipart();
        BodyPart messageBodyPart = new MimeBodyPart();

        // Set key values
        Map<String, String> input = new HashMap<String, String>();
        input.put("User", name.toString());
        input.put("Date", date1);
        input.put("Content In", "English");
        input.put("Absent", absentUser);

        // HTML mail content
        // String htmlText =
        // readEmailFromHtml("/home/techwalnut-pc/mailTemplate.html",
        // input);
        String htmlText = readEmailFromHtml(
                "/home/techwalnut-pc/Development/Aniket/Techwalnut/Project/Java/Spring/Attendance_system/mailTemplate.html",
                input);
        messageBodyPart.setContent(htmlText, "text/html");

        multipart.addBodyPart(messageBodyPart);
        message.setContent(multipart);

        // Conect to smtp server and send Email
        Transport transport = session.getTransport("smtp");
        transport.connect(host_name, Email_Id, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        System.out.println("Mail sent successfully...");

    } catch (MessagingException ex) {
    } catch (Exception ae) {
        ae.printStackTrace();
    }
} catch (Exception exception) {
   exception.printStackTrace();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM