我需要在邮件正文中添加一些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");
如果我得到了输出,则在邮件正文中仅显示最后一个字段,即雇员ID。我想在邮件正文中显示所有三个字段。 谢谢。
答案 0 :(得分:7)
仅在方法多次调用时才设置方法的内容,否则它将覆盖以前的值。
尝试一下:-
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);