java - 无法获取 Java 邮件的正确格式

标签 java jakarta-mail

我正在使用 Java 发送电子邮件,如下所示:

StringBuilder stringBuilder=new StringBuilder("<p>Priority:"+escalationDTO.getEscalationPriority()+"</p><br/><p>Status="+escalationDTO.getEscalationStatus()+"</p><br/><p>Type="+escalationDTO.getEscalationType()+"</p><br/><p>Description="+escalationDTO.getEscalationDescription()+"</p><br/><p>StartDate="+new Date(new java.util.Date().getTime())+"</p><br/><p>EndDate="+sqldate1+"</p>"  );

但是我在邮箱中得到的输出如下:

<p>Priority:P1</p><br/><p>Status=closed</p><br/><p>Type=C</p><br/><p>Description=werrwe</p><br/><p>StartDate=2012-04-24</p><br/><p>EndDate=2010-08-09</p>

我不希望 HTML 标 checkout 现在电子邮件中。每个<p>元素应该导致换行。

这是我的邮件类(class):

package helper;

/**
 * Created by IntelliJ IDEA.
 * User: Milind
 * Date: 4/24/12
 * Time: 4:05 PM
 * To change this template use File | Settings | File Templates.
 */

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {

    private String from;
    private String to;
    private String subject;
    private String text;

    public SendMail(String subject1,String body) {
       from = "Admin@zedo.com";
       to = "abc@zedo.com";
       subject = subject1;
       text = body;
       System.out.println(subject);
    }

    public void send(){

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.bb.zedo.com");
        props.put("mail.smtp.port", "25");

        Session mailSession = Session.getDefaultInstance(props);
        Message simpleMessage = new MimeMessage(mailSession);

        InternetAddress fromAddress = null;
        InternetAddress toAddress = null;
        try {
            fromAddress = new InternetAddress(from);
            toAddress = new InternetAddress(to);
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            simpleMessage.setFrom(fromAddress);
            simpleMessage.setRecipient(RecipientType.TO, toAddress);
            simpleMessage.setSubject(subject);
            simpleMessage.setText(text);
            Transport.send(simpleMessage);

        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

最佳答案

确保将 MIME 消息的内容设置为“text/html”。

MimeMessage message = new MimeMessage(mailSession);
message.setContent("<h1>Hello world</h1>", "text/html");

关于java - 无法获取 Java 邮件的正确格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10297828/

相关文章:

java - 使用线程将电子邮件复制到 javamail 中的文件夹时遇到问题

java - 最后捕获或返回实例或空引用

Javamail 从 Gmail 获取抄送

java - 不在 Android Studio 中工作的教师应用百分比

java - 如何使用 javax.mail 库将 byte[] 转换为消息

java - 如何停止烦人的 "The debugger does not support Hot Fix action"调试器控制台弹出窗口?

java - Android - Google map V2 - 计算当前不在 View 中的标记是否位于屏幕的北、东、南或西

java - 日语文本作为邮件正文

java - 有关不同编程语言中的 Hmac SHA256 的问题

Java - GridBagLayout/Insets 将 JLabel 放置在另一个下