Create an email object in java and save it to file
Asked Answered
P

3

8

i need to backup the emails contained in a PST file (outlook storage). i'm using libpst which is the only free library i found on the web ( http://code.google.com/p/java-libpst/ )

so i can access all the information in each single email (subject, body, sender ecc..), but i need to put them on 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: how do i create this Message object? i don't have a server or an email session, just the information contained in the email

p.s. creating a .msg file would be fine too

Pivot answered 17/11, 2011 at 13:7 Comment(0)
B
7

You create a Message object the same way you would create one for sending, but instead of sending it you write it to a file. You don't need an email server. There's lots of examples of creating messages in the demo programs included with the JavaMail download, and in the JavaMail FAQ. See the Message.writeTo method to write the message to a file (Message is a Part, and writeTo is on Part).

Beating answered 17/11, 2011 at 22:30 Comment(0)
O
13

Here's the code to create a valid eml file with java mail api. works fine with thunderbird and probably 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);
    }
}
Orthogenic answered 30/10, 2012 at 10:4 Comment(2)
Why don't any of you java answerers ever include the imports?Nullipore
There are any way to remove the Message-ID generated?Melinamelinda
B
7

You create a Message object the same way you would create one for sending, but instead of sending it you write it to a file. You don't need an email server. There's lots of examples of creating messages in the demo programs included with the JavaMail download, and in the JavaMail FAQ. See the Message.writeTo method to write the message to a file (Message is a Part, and writeTo is on Part).

Beating answered 17/11, 2011 at 22:30 Comment(0)
P
0

You can use mimeMessageHelper to create the message , as given below:

    import org.springframework.mail.javamail.MimeMessageHelper;
    
    public methodTocreateMessageObject(){
    MimeMessage message = mailSender.createMimeMessage();
    
    MimeMessageHelper mh= new MimeMessageHelper(message, true);
    mh.setSubject(subject);
    mh.setTo(toArray(to));
    if (CollectionUtils.isNotEmpty(cc)) {
    mh.setCc(toArray(cc));
    }
    mh.setFrom(from);
    
    mh.setText(body, true);
    
            if (attachmentFilenames != null) {
                for (String filename : attachmentFilenames) {
                    FileSystemResource file = new FileSystemResource(filename);
                    mh.addAttachment(file.getFilename(), file);
                }
            }
    
            if (inlineAttachments != null && contentType!=null) {
                for (Entry<String, byte[]> inlineAttach : inlineAttachments.entrySet()) {
    
                    String cId = inlineAttach.getKey();
                    byte[] attachInByteStream = inlineAttach.getValue();
                    InputStreamSource attachSource = new ByteArrayResource(attachInByteStream);
                    mh.addInline(cId, attachSource, contentType);
    
                }
            }
ByteArrayOutputStream output = new ByteArrayOutputStream();
        message.writeTo(output);

        Date lastUpdatetime = new Date();

        try(OutputStream outputStream = new FileOutputStream("D:/mail2.eml")) {
            output.writeTo(outputStream);
        }
    }
Posh answered 16/2, 2022 at 22:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.