SendAsDeniedException.MapiExceptionSendAsDenied - Cannot send mail via Java - Even if SMTP is enabled at Outlook

Viewed 3108

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.

@Configuration
@PropertySource("classpath:application.properties")
public class MailConfiguration {
    
    @Value("${configuration.MailConfiguration.host}")
    private String host;
    @Value("${configuration.MailConfiguration.port}")
    private int port;
    @Value("${configuration.MailConfiguration.username}")
    private String username;
    @Value("${configuration.MailConfiguration.password}")
    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("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.debug", "true");
         
        return mailSender;
    }
}

And I send a message like this. Here email = myMail@outlook.com and subject is just a string.

    // Send message
    SimpleMailMessage msg = new SimpleMailMessage();
    msg.setTo(email);
    msg.setSubject(subject);
    msg.setText("Message: " + message +"\nCause: " + cause);
    try {
        javaMailSender.send(msg);
    }catch(Exception e) {
        // Something went wrong
    }

When I run the code above, I get this error. I'm trying to understand why.

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 "smtp.office365.com", 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 "smtp.office365.com", 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 "SIZE", arg "157286400"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
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 "SIZE", arg "157286400"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN XOAUTH2"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: protocolConnect login, host=smtp.office365.com, user=myMail@outlook.com, password=<non-null>
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:<dell@dell-Precision-M6400>
250 2.1.0 Sender OK
RCPT TO:<myMail@outlook.com>
250 2.1.5 Recipient OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   myMail@outlook.com
DATA
354 Start mail input; end with <CRLF>.<CRLF>
Date: Fri, 25 Dec 2020 02:56:05 +0100 (CET)
To: myMail@outlook.com
Message-ID: <783908570.5.1608861365004@dell-Precision-M6400>
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

I have enabled POP and IMAP at Outlook. But still, I cannot send. As you can see, I can login DEBUG SMTP: AUTH LOGIN succeeded.

But why does this happen?

enter image description here

1 Answers

It's a login/sender related issue.

554 5.2.0 SendAsDenied

You are trying to send a mail with a sender different from your outlook login address.

In your case, your login is one and the MAILFROM you're trying to send is a different one:

MAIL FROM--> dell@dell-Precision-M6400

LOGIN --> myMail@outlook.com

So SendAs(dell@dell-Precision-M6400)Denied is thrown.

To make a simple test, try sending the same message but with your outlook credentials set as MAILFROM. It should work without errors.

In order to set different valid senders, add the new sender in the outlook admin panel. That would avoid the exception if trying to send the email with a different sender than your login.

Related