title: Send email via console app over Microsoft Graph API using user credentials
body: <p>I've adapted the <a href="https://github.com/Azure-Samples/active-directory-java-native-headless-v2" rel="nofollow noreferrer">Java console application</a> letting users sign-in with username/password to call Microsoft Graph API on behalf of them, but instead of retrieving basic user data to send emails.</p>
  <p>However, while the original example works fine (I am getting user email, see commented code below), I am getting this error when sending emails:</p>
  <pre class="lang-none prettyprint-override"><code>Graph service exception Error code: NoPermissionsInAccessToken
  Error message: The token contains no permissions, or permissions can not be understood.
  </code></pre>
  <p>For this operation Mail.Send scope needs to be defined in Azure portal and it should be sufficient to use without Admin consent.
  <a href="https://i.stack.imgur.com/Rj3y2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Rj3y2.png" alt="enter image description here" /></a></p>
  <p>I use these Microsoft dependencies:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;dependency&gt;
      &lt;groupId&gt;com.microsoft.azure&lt;/groupId&gt;
      &lt;artifactId&gt;msal4j&lt;/artifactId&gt;
      &lt;version&gt;1.6.1&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;com.microsoft.graph&lt;/groupId&gt;
      &lt;artifactId&gt;microsoft-graph&lt;/artifactId&gt;
      &lt;version&gt;1.6.0&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  <p>This is the actual code:</p>
  <pre class="lang-java prettyprint-override"><code>public class MicrosoftGraphMailer {
  
      private final static String CLIENT_APP_ID = &quot;{real-client-app-id}&quot;;
      private final static String AUTHORITY = &quot;https://login.microsoftonline.com/{real-tenant-id}/&quot;;
  
      public static void main(String[] args) throws Exception {
  
          String user = &quot;{real-user}&quot;;
          String password = &quot;{real-password}&quot;;
  
          String token = getUserPasswordAccessToken(user, password).accessToken();
          System.out.println(token);
  
          SimpleAuthProvider authProvider = new SimpleAuthProvider(token);
          IGraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();
  
          Message message = new Message();
          message.subject = &quot;My subject&quot;;
  
          ItemBody body = new ItemBody();
          body.contentType = BodyType.HTML;
          body.content = &quot;&lt;h1&gt;My HTML body&lt;/h1&gt;&quot;;
          message.body = body;
  
          List&lt;Recipient&gt; toRecipientList = new LinkedList&lt;&gt;();
          Recipient toRecipient = new Recipient();
          EmailAddress emailAddress = new EmailAddress();
          emailAddress.address = &quot;{real-recipient}&quot;;
          toRecipient.emailAddress = emailAddress;
          toRecipientList.add(toRecipient);
          message.toRecipients = toRecipientList;
  
          graphClient.me()
                  .sendMail(message, false)
                  .buildRequest()
                  .post();
  
  /*
           String mail = graphClient.me()
                  .buildRequest()
                  .get().mail;
  
           System.out.println(mail);
  */
      }
  
      private static IAuthenticationResult getUserPasswordAccessToken(String user, String password) throws Exception {
  
          PublicClientApplication app = PublicClientApplication.builder(CLIENT_APP_ID).authority(AUTHORITY).build();
  
          Set&lt;String&gt; scopes = new HashSet&lt;&gt;(Arrays.asList(&quot;Mail.Send&quot;));
  
          UserNamePasswordParameters userNamePasswordParam = UserNamePasswordParameters.builder(
                  scopes, user, password.toCharArray())
                  .build();
  
          return app.acquireToken(userNamePasswordParam).get();
      }
  
      private static class SimpleAuthProvider implements IAuthenticationProvider {
  
          private String accessToken = null;
  
          public SimpleAuthProvider(String accessToken) {
              this.accessToken = accessToken;
          }
  
          @Override
          public void authenticateRequest(IHttpRequest request) {
              request.addHeader(&quot;Authorization&quot;, &quot;Bearer &quot; + accessToken);
          }
      }
  }
  </code></pre>
  <p>Basically I need console daemon app for sending emails on behalf of me without any user interaction. Credentials will be stored outside the app. I don't need permissions to send emails on behalf of arbitrary user.</p>