How to add sensitivity labels while sending mail using outlook365 mail id with java code.

Vinay Sharma 56 Reputation points
2024-01-24T10:27:00.9266667+00:00

I am sending Outlook mail using Java code. I am facing issues with adding sensitivity labels like confidential, private, public, etc.

Below is the POC I am using.
and also the output I am getting. User's image JavaCopy

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SmtpExample {
  public static void main(String[] args) throws MessagingException {

    String to = "dummy";
	
    String userEmail = "dummy";
    String accessToken = "dummy";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.imap.sasl.enable", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth.mechanisms", "XOAUTH2");
    props.put("mail.smtp.auth.login.disable", "true");
    props.put("mail.smtp.auth.plain.disable", "true");
    props.put("mail.debug.auth", "true");
     Authenticator auth = new Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(userEmail,accessToken );
      }
    };
    Session session = Session.getInstance(props, auth);
    Transport transport = session.getTransport("smtp");
    transport.connect(smtpHost, port, userEmail,
        accessToken);;
        session.setDebug(true); 
    session.setDebug(true);
    try {
      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(userEmail));
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
      message.setSubject("Hello");
      message.setText("Hello Testing!");
	  message.addHeader("Sensitivity", "Confidential");
      Transport.send(message);

      System.out.println("message sent successfully...");

    } catch (MessagingException e) {
      e.printStackTrace();
    }
  }
}

The message is sent successfully but the issues I am facing are :

  1. I am not getting any confidential tags or extra security labels.
  2. Could you tell me how to do the same with UI.
    Please check the POC and Tell me if is there anything wrong.
Outlook | Windows | Classic Outlook for Windows | For business
Microsoft 365 and Office | Install, redeem, activate | For business | Windows
Microsoft Security | Microsoft Graph
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Deepanshu Sharma 500 Reputation points Microsoft External Staff
    2024-01-27T14:53:42.5866667+00:00

    Hi Vinay Sharma, According to RFC 1327, the accepted values for the Sensitivity header are one of these below:

    sensitivity = "Personal" / "Private" / "Company-Confidential"

    So in your case, it would be:

    message.addHeader("Sensitivity", "Company-Confidential");
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.