我在使用JavaMail发送带附件的邮件时候,每次到了Transport.sendMessage()这一步,控制台就会输出附件内容,请问如何设置可以取消输出呢?
public void sendFileAttachedMail(String fromMail, String toMail, String fromMailPwd, String bookId) {
Properties prop = new Properties();
prop.setProperty(MAIL_HOST, MAIL_HOST_VALUE);
prop.setProperty(MAIL_TRANSPORT_PROTOCOL, MAIL_TRANSPORT_PROTOCOL_VALUE);
prop.setProperty(MAIL_SMTP_AUTH, MAIL_SMTP_AUTH_VALUE);
Session session = Session.getInstance(prop);
session.setDebug(true);
try {
Transport ts = session.getTransport();
String fromMailPrefix = fromMail.split("@")[0];
ts.connect(MAIL_HOST_VALUE,fromMailPrefix, fromMailPwd);
String subject = "FILE ATTACHED MAIL TEST";
String content = "Mail Content RE";
String fileSavePath = "E://attachMail.eml";
Message message = createFileAttachedMail(session, fromMail, toMail, subject, content, bookId, fileSavePath);
ts.sendMessage(message, message.getAllRecipients());
ts.close();
} catch (Exception e) {
if(logger.isErrorEnabled()){
logger.error("send fileAttachedMail failed!",e);
}
}
}
public MimeMessage createFileAttachedMail(Session session, String fromAdd, String toAdd, String subject, String content, String fileObjectId, String fileSavePath) throws Exception {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAdd));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(toAdd));
message.setSubject(subject);
// 邮件正文
MimeBodyPart text = new MimeBodyPart();
text.setContent(content, MAIL_CONTENT_FORMAT_CHARSET);
// 附件
MimeBodyPart attach = new MimeBodyPart();
DataHandler handler = new DataHandler(new FileDataSource(this.gridFSService.readFiles(fileObjectId)));
attach.setDataHandler(handler);
attach.setFileName(handler.getName());
// 创建容器描述数据关系
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(attach);
mp.setSubType("mixed");
message.setContent(mp);
message.saveChanges();
//将创建的email写入到本地存储
//message.writeTo(new FileOutputStream(fileSavePath));
return message;
}