我正在尝试使用zip file
作为附件使用以下代码段发送邮件,我可以发送电子邮件,但是zip文件的附件会转换为某个.bin
文件。
我需要设置一些属性吗?
为什么zip文件被转换为.bin文件?
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.gmail.com");
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("HTML mail with images");
message.setFrom(new InternetAddress("b@gmail.com"));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("a@gmail.com"));
MimeMultipart multipart = new MimeMultipart("related");
// first part (the html)
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "PFA Query Output.";
messageBodyPart.setContent(htmlText, "text/html");
// add it
multipart.addBodyPart(messageBodyPart);
// second part (the image)
messageBodyPart = new MimeBodyPart();
DataSource fds = new FileDataSource(zipFilePath);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID","<image>");
// add it
multipart.addBodyPart(messageBodyPart);
// put everything together
message.setContent(multipart);
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
答案 0 :(得分:2)
如果您没有发送图片,请删除messageBodyPart.setHeader("Content-ID","<image>")
。
添加以下语句集附件文件名
messageBodyPart.setFileName(zipFilePath);
答案 1 :(得分:1)
而不是
DataSource fds = new FileDataSource(zipFilePath);
尝试
DataSource fds = new FileDataSource(new File(zipFilePath));
也尝试删除,
messageBodyPart.setHeader("Content-ID","<image>");