Skip to content

ctabin/jotlmsg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Maven Build Javadoc

jotlmsg

It's a simple API meant to easily generate Microsoft Outlook message files (.msg). This library is based on Apache POI and is a 100% Java implementation.

Here the compatibility map of this API:

Version JDK Package
<= 1.9 JDK 8 and upwards javax
>= 2.0 JDK 11 and upwards jakarta
>= 3.0 JDK 21 and upwards jakarta

Installation

Simply add the jotlmsg.jar and its dependencies to your classpath.

If you're using maven, then simply add the following dependency:

<dependency>
    <groupId>ch.astorm</groupId>
    <artifactId>jotlmsg</artifactId>
    <version>3.0.1</version>
</dependency>

Usage examples

Create a new message:

OutlookMessage message = new OutlookMessage();
message.setSubject("Hello");

//plain text body or html body
message.setPlainTextBody("This is a plain text body.");
message.setHtmlBody("<html><body><p>This is an html body with <strong>bold</strong> and <i>italic</i> styles.</p></body></html>");

//creates a new Outlook Message file
message.writeTo(new File("myMessage.msg"));

//creates a javax.mail MimeMessage
MimeMessage mimeMessage = message.toMimeMessage();

Read an existing message:

OutlookMessage message = new OutlookMessage(new File("aMessage.msg"));
System.out.println(message.getSubject());
System.out.println(message.getPlainTextBody());

Managing recipients:

OutlookMessage message = new OutlookMessage();
message.addRecipient(Type.TO, "[email protected]");
message.addRecipient(Type.TO, "[email protected]", "Bill");
message.addRecipient(Type.CC, "[email protected]", "Steve");
message.addRecipient(Type.BCC, "[email protected]");
        
List<OutlookMessageRecipient> toRecipients = message.getRecipients(Type.TO);
List<OutlookMessageRecipient> ccRecipients = message.getRecipients(Type.CC);
List<OutlookMessageRecipient> bccRecipients = message.getRecipients(Type.BCC);
List<OutlookMessageRecipient> allRecipients = message.getAllRecipients();

Managing optional replyto recipients:

OutlookMessage message = new OutlookMessage();
message.setReplyTo(List.of("[email protected]", "[email protected]"));

List<String> replyToRecipients = message.getReplyTo();

Managing attachments:

OutlookMessage message = new OutlookMessage();
message.addAttachment("aFile.txt", "text/plain", new FileInputStream("data.txt")); //will be stored in memory
message.addAttachment("aDocument.pdf", "application/pdf", new FileInputStream("file.pdf")); //will be stored in memory
message.addAttachment("hugeFile.zip", "application/zip", a -> new FileInputStream("data.zip")); //piped to output stream

List<OutlookMessageAttachment> attachments = message.getAttachments();

HTML message with inlined attachments

The APIs allow to generate a message with embedded picture inside an HTML body that will then be editable directly in Outlook. The inlined attachment can be referenced by using the cid: prefix in a tag.

//generate a unique content id to be referenced
String contentId = UUID.randomUUID().toString();

OutlookMessage message = new OutlookMessage();
message.setSubject("My HTML message");
message.setFrom("[email protected]");
message.addRecipient(Type.TO, "[email protected]", "Cédric");

//optional alternative plain text message that can be shown if HTML is not displayed
message.setPlainTextBody("Alternative plain text message");

//defines the htmlbody with injected content id reference
String htmlBody = """
                  <html>
                    <body>
                      <div>
                        Inline attached smiley:
                        <img src="cid:%s" alt="Smiley">
                      </div>
                    </body>
                  </html>
                  """;
message.setHtmlBody(String.format(htmlBody, contentId));

//adds the inlined attachment with the referenced content id
OutlookMessageAttachment inlineAttachment = new OutlookMessageAttachment("Face-smile.png", "image/png", a -> OutlookMessageMIMETest.class.getResourceAsStream("Face-smile.png"));
inlineAttachment.setContentId(contentId);
message.addAttachment(inlineAttachment);

Note: The inlined attachements are not shown as "real" attachment in Outlook. It's an alternative to external URLs that might be prevented to be loaded by the mail client of the final recipient.

Limitations

The current implementation allows to create simple msg files with many recipients (up to 2048) and attachments (up to 2048). However, there is no current support of Microsoft Outlook advanced features like appointments or calendar integration, nor embedded messages.

Donate

This project is completely developed during my spare time.

Since I'm a big fan of cryptocurrencies and especially Cardano (ADA), you can send me some coins at the address below (check it here):

addr1q9sgms4vc038nq7hu4499yeszy0rsq3hjeu2k9wraksle8arg0n953hlsrtdzpfnxxw996l4t6qu5xsx8cmmakjcqhksaqpj66

About

Simple API on top of Apache POI to write Outlook .msg files

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 6

Languages