在我的程序中,我必须使用 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;
}
有任何想法吗?