在我的程序中,我必须使用 java 发送邮件。程序正确发送邮件,但服务器自动添加一个标志。结果是收到的电子邮件包含正确的正文,但签名带有 html 标签。
Correct body.....
</pre>
<html>
<i>
Sent by me
<i>
<br>
<br>
</html>
我使用以下代码发送了邮件:
Properties props = new Properties();
props.put("mail." + protocol + ".host", smtpHost);
props.put("mail." + protocol + ".port", smtpPort);
Session session = Session.getDefaultInstance(props, null);
// Construct the message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(user));
msg.setRecipients(RecipientType.TO, loadAddress());
msg.setSubject(subject);
msg.setText(body);
// Send the message
props.put("mail." + protocol + ".auth", "false");
Transport t = session.getTransport(protocol);
try {
t.connect();
t.sendMessage(msg, msg.getAllRecipients());
} finally {
t.close();
}
编辑:我正在尝试插入以下代码:
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
但是结果没有改变。我创建主体的函数是:
public void setBody(ArrayList<User> users) {
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, -1);
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String formattedDate = df.format(c.getTime());
subject = new String("Day " + formattedDate);
body += "Hi "
+ formattedDate;
}
有什么想法吗?
最佳答案
尝试 Message 类中的 .setContent(Object o, String s)
方法,将正文作为字符串放入 Object 参数中,并在第二个参数中放入类似“text/html”的内容定义内容类型。
关于java - 使用 java 发送的邮件包含不正确的 html 符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17991297/