Exception occurs when trying to read a UTF-7 email in a MS365 mailbox using OAuth 2 and Jakarta Mail

Copper Contributor

I'm trying to read emails I receive in Microsoft 365 using Jakarta Mail in Java. I was able to read the UTF-8 encoded email using the following code, but I ran into an issue where the UTF-7 encoded email threw a javax.mail.MessagingException. Specifically, the contents of the Envelope cannot be retrieved, and the header and document are always empty.
Could anyone advise on how to handle reading UTF-7 encoded emails read from Microsoft 365?

This is Sample Code.

 

 

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.time.Duration;
import java.util.Map;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;

import net.arnx.jsonic.JSON;

public class Office365ImapExample {

    public static void main(String[] args) throws Exception {
        // IMAP Server Setting
        String host = "outlook.office365.com";
        String username = "email address removed for privacy reasons";
        String accessToken = "<OAUTH2 AccessToken>";
        // Properties Setting
        Properties props = new Properties();
        props.put("mail.store.protocol", "imaps");
        props.put("mail.imaps.port", 993);
        props.put("mail.imaps.ssl.enable", "true");
        props.put("mail.imaps.starttls.enable", "true");
        props.put("mail.imaps.auth.mechanisms", "XOAUTH2");
        Session session = Session.getInstance(props, null);
        session.setDebug(true);
        Store store = session.getStore("imaps");
        store.connect(host, username, accessToken);
        // Email reading process
        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_WRITE);
        Message[] messages = inbox.getMessages();
        for (Message message : messages) {
            System.out.println("Header: " + message.getAllHeaders());
            System.out.println("Subject: " + message.getSubject());
            System.out.println("From: " + message.getFrom()[0]);
            System.out.println("Body: " + message.getContent().toString());
        }
        inbox.close(true);
        // inbox.close(false);
        store.close();
    }
}

 

Additional Information:

  • Jakarta Mail version 1.6.7 is being utilized.

  • The protocol employed is IMAPS.

  • Authentication is carried out using OAUTH2.

When setting the value of the sessiono object's setDebug method to true, we have confirmed that the following situation occurs.

 

  • UTF-8
A2 FETCH 1 (BODY[]<0.16384>) 
* 1 FETCH (FLAGS (\Seen \Recent) BODY[]<0> {681}
 ... <Contents of the email> ... 
A2 OK Fetch completed.
  • UTF-7
A4 FETCH 1 (BODY[]<0.16384>)
A4 OK FETCH completed.
DEBUG IMAPS: got 0 BODY responses for section 

 

0 Replies