1

在我的程序中,我必须使用 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;    
}

有任何想法吗?

4

2 回答 2

0

最后我解决了插入以下代码:

MimeBodyPart mbp1 = new MimeBodyPart();
    mbp1.setContent(body, "text/html");
    Multipart mp = new MimeMultipart();
    mp.addBodyPart(mbp1);

    msg.setContent(mp, "text/html");
于 2013-08-01T11:31:26.910 回答
0

Try the .setContent(Object o, String s) method from the Message class, put your body into the Object parameter as a String and put something like "text/html" in the second parameter to define the content-type.

于 2013-08-01T10:33:54.823 回答