Java 邮件 API setContent() 没有写在邮件正文中 HTML
Java Mail API setContent() not written in the mail body as 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");
如果我得到输出,则只在邮件正文中显示最后一个字段,即员工 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);
我需要在邮件正文中添加一些 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。我想在邮件正文中显示所有三个字段。 谢谢。
只设置方法的内容一次,多次调用会覆盖之前的值
试试这个:-
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);