我想發送郵件作爲附件在另一封郵件使用javax api。截至目前,我先保存在磁盤上的郵件,然後使用下面的代碼將其連接到另一個電子郵件: -如何發送一封郵件作爲另一封郵件的附件在Java中,而不保存到磁盤
MimeMessage generateMailMessage = new MimeMessage(getMailSession);
generateMailMessage.setFrom(new InternetAddress("[email protected]"));
String mailSubject = properties.getProperty("mail.subject");
generateMailMessage
.setSubject(mailSubject);
generateMailMessage.setContent(emailBody, "text/html");
generateMailMessage.addRecipient(Message.RecipientType.TO,
new InternetAddress(properties.getProperty("message.recipienttype.to")));
generateMailMessage.addRecipient(Message.RecipientType.CC,
new InternetAddress(recipientEmail));
File file = new File(properties.getProperty("mail.draft.folder")+"mail.eml");
FileOutputStream fos = new FileOutputStream(chatFile);
generateMailMessage.writeTo(fos);
Session getMailSession1 = Session.getDefaultInstance(mailServerProperties, null);
MimeMessage generateMailMessage1 = new MimeMessage(getMailSession1);
generateMailMessage1
.setSubject("Attachment");
generateMailMessage1.addRecipient(Message.RecipientType.TO,
new InternetAddress("[email protected]"));
Multipart multipart = new MimeMultipart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDescription("hahdsa");
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("mail.eml");
multipart.addBodyPart(messageBodyPart);
generateMailMessage1.setContent(multipart);
transport = getMailSession1.getTransport("smtp");
if(!transport.isConnected())
transport.connect(properties.getProperty("mail.host"),
Integer.parseInt((String) properties.get("mail.smtp.port")), "[email protected]",
(String) properties.get("mail.password"));
transport.sendMessage(generateMailMessage1, generateMailMessage1.getAllRecipients());
transport.close();
有什麼辦法,我可以做的同樣的事情,但不保存連接電子郵件。我已經搜索出來,但發現要附加的文件可以存儲在內存中,但無法將郵件保存在內存中。
請建議。
感謝
你可以使用'java.io.File'來創建一個臨時文件(並在它自己之後清理),像這樣https://stackoverflow.com/a/7083754/16959 –