我正在开发一个发送电子邮件的应用程序。我想以RTF格式发送正文消息。当我尝试以RTF格式发送消息时,它会创建一个附件文件而不是正文消息。
public boolean run() {
System.out.println("START SENDING");
Transport transport = null;
Session mailSession = null;
Properties props = null;
try {
props = new Properties();
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.port", port);
props.put("mail.smtp.user", user);
props.put("mail.smtp.password", password);
props.put("mail.smtp.auth", auth);
props.put("mail.smtp.ssl.enable", false);
props.put("mail.smtp.ssl.trust", "*");
mailSession = Session.getInstance(props, new SMTPAuthenticator(user, password));
transport = mailSession.getTransport("smtp");
MimeMessage message = prepareMessage(mailSession, "UTF-8",
user, subject, HtmlMessage, recipientsString);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(HtmlMessage);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
if (file != null) {
DataSource source = new FileDataSource(file.getAbsoluteFile());
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(file.getName());
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
} else {
/*HtmlMessage (String)*/
message.setContent(HtmlMessage, "text/rtf");
}
transport.connect();
Transport.send(message);
System.out.println("SEND DONE");
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Unable to send Message.");
ex.printStackTrace();
} finally {
System.out.println(mailSession.getProperty("mail.smtp.user"));
mailSession = null;
props.clear();
smtpServer = null;
user = null;
password = null;
if (t != null && t.isAlive()) {
t.interrupt();
t = null;
}
this.dispose();
}
return false;
}
答案 0 :(得分:0)
要将邮件正文部分设为内联而不是附件,请执行以下操作:
messageBodyPart.setDisposition(Part.INLINE);