【问题标题】:how send html tag in java email class?如何在 java 电子邮件类中发送 html 标签?
【发布时间】:2017-09-21 13:37:35
【问题描述】:

我想将以下提到的结果发送到电子邮件,并希望通过电子邮件仔细接收它们。因此我决定使用 html 代码,但没有结果。

caputure.png 图片

代码:

public static void sendresult (ShowResultModel showuserresulttable) {

    final String username = *****
    final String password = ******;

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("mekteb48@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("iismayilov028@gmail.com"));

        message.setSubject("Exammer result :" + showuserresulttable.getUsername() );

        DecimalFormat formatter = new DecimalFormat("#0.00");

         String MESSAGE = " ";
        double resultpercent  =  showuserresulttable.getScore() /(double) showuserresulttable.getTotalanswer()*100;

        MESSAGE+="<b>Username:</b> " + showuserresulttable.getUsername() + "<br><hr>";
        MESSAGE+="<b>Subject</b>" + showuserresulttable.getSubject() + "<br><hr>";
        MESSAGE+="<b>Variant :</b>" + showuserresulttable.getVariant() +   "<br><hr>";
        MESSAGE+="<b>Score of result:</b>" + showuserresulttable.getScore() + "<br><hr>";
        MESSAGE+="<b>Total question :</b>" + showuserresulttable.getTotalanswer()+ "<br><hr>";
        MESSAGE+="<b> Score percent:</b>" + resultpercent + "<br><hr>";
        MESSAGE+="<b>Spendtime:</b>" + showuserresulttable.getSpendtime() + "second" + "<br><hr>";
        MESSAGE+="<b>Examdate:</b>" + showuserresulttable.getExamdate() ;

        message.setText(MESSAGE);
        Transport.send(message);

        System.out.println("Mail gonderildi!");
    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

} 

【问题讨论】:

标签: java


【解决方案1】:

您应该使用 message.setText(MESSAGE, true); 而不是 message.setText(MESSAGE); here true 代表 html(是否为 HTML 邮件应用内容类型“text/html”,使用默认内容类型(“text/plain”)否则)

此外,如果您使用的是 thymeleaf 或 freemarker,那么您可以使用那里的模板。

【讨论】:

  • 我尝试了你的建议,但是当我在课堂上写 true 时,我得到红色下划线错误,“无法应用部分设置文本字符串”,该怎么办?
  • 你是使用 spring javacv 邮件依赖还是其他用户?
  • 我不使用弹簧。我已经在 ide intellejis 中编码,导入下面提到的 jars:import java.text.DecimalFormat;导入 java.util.List;导入 java.util.Properties;导入 javax.mail.Message;导入 javax.mail.MessagingException;导入 javax.mail.PasswordAuthentication;导入 javax.mail.Session;导入 javax.mail.Transport;导入 javax.mail.internet.Internet 地址;导入 javax.mail.internet.MimeMessage;
  • 请添加此行。 'message.setContent(MESSAGE, "text/html; charset=utf-8");'也请删除 message.setText (message):;行
  • 请添加此行。 'message.setContent(MESSAGE, "text/html; charset=utf-8");'也请删除 message.setText (message):;行
猜你喜欢
  • 2016-07-16
  • 2020-06-12
  • 2011-01-01
  • 2010-10-27
  • 2010-09-24
  • 2013-05-26
相关资源
最近更新 更多