Java Mail API setContent()不是作为HTML写在邮件正文中的

我需要在邮件正文中添加一些 HTML内容.这是我到目前为止所尝试的.
message.setContent(
                      "<h1>You Have a Promotion</h1>","text/html");

            message.setContent(
                      "<h3>Your First Name :</h3>" + FirstNm,"text/html");

            message.setContent(
                      "<h3>Your Last Name :</h3>" + LastNm,"text/html");

            message.setContent(
                      "<h5>Your Employee ID :</h5>" + Employeeid,"text/html");

如果我得到Out,则只将最后一个字段显示邮件正文中,即员工ID.我想在邮件正文中显示所有三个字段.
谢谢.

解决方法

如果多次调用方法内容,它只会设置一次,它将覆盖以前的值.

试试这个 :-

message.setContent(
                      "<h1>You Have a Promotion</h1> <h3>Your First Name :</h3>" + FirstNm + 
                      "<h3>Your Last Name :</h3>" + LastNm + "<h5>Your Employee ID :</h5>" + Employeeid,"text/html");

下面是在多部分消息的情况下设置文本的代码

BodyPart messageBodyPart = new MimeBodyPart();
                // Fill the message
                messageBodyPart.setContent("<h1>You Have a Promotion</h1> <h3>Your First Name :</h3>" + FirstNm + 
                          "<h3>Your Last Name :</h3>" + LastNm + "<h5>Your Employee ID :</h5>" + Employeeid,"text/html");
                // Create a multipar message
                Multipart multipart = new MimeMultipart();
                // Set text message part
                multipart.addBodyPart(messageBodyPart);

                // Part two is attachment
                messageBodyPart = new MimeBodyPart();
                DataSource source = new FileDataSource("");//add file path
                messageBodyPart.setDataHandler(new DataHandler(source));
                messageBodyPart.setFileName("");//file name to be displayed
                multipart.addBodyPart(messageBodyPart);
                message.setContent(multipart);

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...