title: SendAsDeniedException.MapiExceptionSendAsDenied - Cannot send mail via Java - Even if SMTP is enabled at Outlook
body: <p>I have this Java code. The Java code handels mail sending only via Outlook. This example is a minimal working example. Notice that it's NOT Office 365. It's regular Outlook.</p>
  <pre><code>@Configuration
  @PropertySource(&quot;classpath:application.properties&quot;)
  public class MailConfiguration {
      
      @Value(&quot;${configuration.MailConfiguration.host}&quot;)
      private String host;
      @Value(&quot;${configuration.MailConfiguration.port}&quot;)
      private int port;
      @Value(&quot;${configuration.MailConfiguration.username}&quot;)
      private String username;
      @Value(&quot;${configuration.MailConfiguration.password}&quot;)
      private String password;
      
      @Bean
      public JavaMailSender getJavaMailSender() {
  
          JavaMailSenderImpl mailSender = new JavaMailSenderImpl();       
          mailSender.setHost(host);
          mailSender.setPort(port);
          mailSender.setUsername(username);
          mailSender.setPassword(password);
           
          Properties props = mailSender.getJavaMailProperties();
          props.put(&quot;mail.transport.protocol&quot;, &quot;smtp&quot;);
          props.put(&quot;mail.smtp.auth&quot;, &quot;true&quot;);
          props.put(&quot;mail.smtp.starttls.enable&quot;, &quot;true&quot;);
          props.put(&quot;mail.debug&quot;, &quot;true&quot;);
           
          return mailSender;
      }
  }
  </code></pre>
  <p>And I send a message like this. Here <code>email = myMail@outlook.com</code> and <code>subject</code> is just a string.</p>
  <pre><code>    // Send message
      SimpleMailMessage msg = new SimpleMailMessage();
      msg.setTo(email);
      msg.setSubject(subject);
      msg.setText(&quot;Message: &quot; + message +&quot;\nCause: &quot; + cause);
      try {
          javaMailSender.send(msg);
      }catch(Exception e) {
          // Something went wrong
      }
  </code></pre>
  <p>When I run the code above, I get this error. I'm trying to understand why.</p>
  <pre><code>DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
  DEBUG SMTP: useEhlo true, useAuth true
  DEBUG SMTP: trying to connect to host &quot;smtp.office365.com&quot;, port 587, isSSL false
  220 HE1PR0701CA0065.outlook.office365.com Microsoft ESMTP MAIL Service ready at Fri, 25 Dec 2020 01:56:03 +0000
  DEBUG SMTP: connected to host &quot;smtp.office365.com&quot;, port: 587
  EHLO dell-Precision-M6400
  250-HE1PR0701CA0065.outlook.office365.com Hello [213.89.50.140]
  250-SIZE 157286400
  250-PIPELINING
  250-DSN
  250-ENHANCEDSTATUSCODES
  250-STARTTLS
  250-8BITMIME
  250-BINARYMIME
  250-CHUNKING
  250 SMTPUTF8
  DEBUG SMTP: Found extension &quot;SIZE&quot;, arg &quot;157286400&quot;
  DEBUG SMTP: Found extension &quot;PIPELINING&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;DSN&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;ENHANCEDSTATUSCODES&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;STARTTLS&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;8BITMIME&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;BINARYMIME&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;CHUNKING&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;SMTPUTF8&quot;, arg &quot;&quot;
  STARTTLS
  220 2.0.0 SMTP server ready
  EHLO dell-Precision-M6400
  250-HE1PR0701CA0065.outlook.office365.com Hello [213.89.50.140]
  250-SIZE 157286400
  250-PIPELINING
  250-DSN
  250-ENHANCEDSTATUSCODES
  250-AUTH LOGIN XOAUTH2
  250-8BITMIME
  250-BINARYMIME
  250-CHUNKING
  250 SMTPUTF8
  DEBUG SMTP: Found extension &quot;SIZE&quot;, arg &quot;157286400&quot;
  DEBUG SMTP: Found extension &quot;PIPELINING&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;DSN&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;ENHANCEDSTATUSCODES&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;AUTH&quot;, arg &quot;LOGIN XOAUTH2&quot;
  DEBUG SMTP: Found extension &quot;8BITMIME&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;BINARYMIME&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;CHUNKING&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;SMTPUTF8&quot;, arg &quot;&quot;
  DEBUG SMTP: protocolConnect login, host=smtp.office365.com, user=myMail@outlook.com, password=&lt;non-null&gt;
  DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2 
  DEBUG SMTP: Using mechanism LOGIN
  DEBUG SMTP: AUTH LOGIN command trace suppressed
  DEBUG SMTP: AUTH LOGIN succeeded
  DEBUG SMTP: use8bit false
  MAIL FROM:&lt;dell@dell-Precision-M6400&gt;
  250 2.1.0 Sender OK
  RCPT TO:&lt;myMail@outlook.com&gt;
  250 2.1.5 Recipient OK
  DEBUG SMTP: Verified Addresses
  DEBUG SMTP:   myMail@outlook.com
  DATA
  354 Start mail input; end with &lt;CRLF&gt;.&lt;CRLF&gt;
  Date: Fri, 25 Dec 2020 02:56:05 +0100 (CET)
  To: myMail@outlook.com
  Message-ID: &lt;783908570.5.1608861365004@dell-Precision-M6400&gt;
  Subject: Alarm Message Subject Title
  MIME-Version: 1.0
  Content-Type: text/plain; charset=us-ascii
  Content-Transfer-Encoding: 7bit
  
  Message: hejehej
  Cause: Mesurement exceeded the threshold at analog input 0.
  .
  554 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message. 
  
  RSET
  250 2.0.0 Resetting
  DEBUG SMTP: MessagingException while sending, THROW: 
  com.sun.mail.smtp.SMTPSendFailedException: 554 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message.
  
      at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2373)
      at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:2095)
      at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1301)
      at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:465)
      at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:323)
      at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:312)
      at se.danielmartensson.service.MailService.sendMessage(MailService.java:44)
      at se.danielmartensson.threads.SamplingThread.run(SamplingThread.java:227)
  QUIT
  221 2.0.0 Service closing transmission channel
  </code></pre>
  <p>I have enabled POP and IMAP at Outlook. But still, I cannot send. As you can see, I can login <code>DEBUG SMTP: AUTH LOGIN succeeded</code>.</p>
  <p>But why does this happen?</p>
  <p><a href="https://i.stack.imgur.com/PcRBs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PcRBs.png" alt="enter image description here" /></a></p>