I am trying to send a mail as attachment in another mail using javax api. As of now I am saving the mail first on the disk and then attaching it to the another email using the following code:-
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();
Is there any way that i can do the same thing without saving the attached email. I have searched out but found that files to be attached can be stored in memory but found nowhere to save the mail in memory.
Please suggest.
Thanks
java.io.File's ability to create a temporary file (and clean up after itself) with something like this stackoverflow.com/a/7083754/16959