Create an email object in java and save it to a file

I need to back up emails contained in a PST file (Outlook storage). I use libpst, which is the only free library I have found on the Internet. ( http://code.google.com/p/java-libpst/ )

so I can access all the information in every single letter (subject, body, sender ecc ..), but I need to put them in a file

here someone said you can create an eml file from a javax.mail.Message object: Create a .eml (email) file in Java

The problem is this: how to create this Message object? I do not have a server or email session, just the information contained in the letter

ps creating a .msg file will also be great.

+4
source share
2 answers

You create a Message object in the same way as you created it for sending, but instead of sending it, you write it to a file. You do not need an email server. There are many examples of creating messages in demos complete with JavaMail Download and JavaMail Frequently Asked Questions . See Message.writeTo method to write a message to a file (Message is part, and writeTo is on part).

+6
source

Here is the code for creating a valid eml file with java mail api. works great with thunderbird and possibly other email clients:

public static void createMessage(String to, String from, String subject, String body, List<File> attachments) { try { Message message = new MimeMessage(Session.getInstance(System.getProperties())); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subject); // create the message part MimeBodyPart content = new MimeBodyPart(); // fill message content.setText(body); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(content); // add attachments for(File file : attachments) { MimeBodyPart attachment = new MimeBodyPart(); DataSource source = new FileDataSource(file); attachment.setDataHandler(new DataHandler(source)); attachment.setFileName(file.getName()); multipart.addBodyPart(attachment); } // integration message.setContent(multipart); // store file message.writeTo(new FileOutputStream(new File("c:/mail.eml"))); } catch (MessagingException ex) { Logger.getLogger(Mailkit.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Mailkit.class.getName()).log(Level.SEVERE, null, ex); } } 
+6
source

Source: https://habr.com/ru/post/1381693/


All Articles