It's not a big deal if you understand that I was addicted to receiving emails with Java Mail from Exchange Online

Introduction

I decided to send and receive emails with Exchange Online using the Java (Kotlin) app, so I tried sending emails that I could easily find out.

kotlin:build.gradle.kts


import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
  id("org.springframework.boot") version "2.1.6.RELEASE"
  kotlin("jvm") version "1.3.41"
  kotlin("plugin.spring") version "1.3.41"
}
repositories {
  mavenCentral()
}
dependencies {
  implementation(platform("org.springframework.boot:spring-boot-dependencies:2.1.6.RELEASE"))
  implementation("org.springframework.boot:spring-boot-starter-mail")
  implementation(kotlin("stdlib-jdk8"))
}
val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
  jvmTarget = "1.8"
}
val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions {
  jvmTarget = "1.8"
}

application.yml


spring:
  mail:
    host: smtp.office365.com
    port: 587
    username:Meru Adores
    password:Passwa-do
    properties:
      mail:
        smtp:
          starttls:
            enable: true

Application.kt


import org.springframework.boot.CommandLineRunner
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.mail.SimpleMailMessage
import org.springframework.mail.javamail.JavaMailSender

@SpringBootApplication
class Application(val mailSender: JavaMailSender): CommandLineRunner {
  override fun run(vararg args: String?) {
    val mailMessage = SimpleMailMessage().apply {
      setTo("Destination")
      from = "Meru Adores"
      subject = "Kenmei"
      text = "Really"
    }
    mailSender.send(mailMessage)
  }
}

fun main( args: Array<String>) {
  runApplication<Application>(*args)
}

Since I was able to send mail easily like this, I thought that even though it was Exchange Online, it would be easy to receive as well as a normal mail server. In the end, that's right, but when I stumbled on the way, I was struck by suspicion and went back and forth.

A little cloud bound ...

Since I was able to send an email, I pretended that I could receive it as a matter of course, and when it came time to implement it, I thought that !? Spring-context-support-5.1.8.RELEASE.jar with JavaMailSender does not have JavaMailReciver. .. Why is JavaMail itself able to receive? Spring integration also receives emails, but I didn't feel like incorporating them, so I decided to use JavaMail directly. However, I wondered if there is something difficult to standardize because there is no email reception in the Spring Framework that wraps various implementations and makes it easy to use.

And addicted

The receiving logic in JavaMail wasn't too vigilant.

val props = Properties()
//Generate session
val session = Session.getInstance(props)
//Select protocol
val store = session.getStore("imaps")
//Connect to server
store.connect("outlook.office365.com", 993, "Meru Adores", "Passwa-do")
//Select a folder
val folder = store.getFolder("INBOX")
folder.open(Folder.READ_ONLY)
//incoming mail
folder.messages.map {
  println(it.subject)
}
folder.close(false)
store.close()

Then, javax.mail.AuthenticationFailedException: AUTHENTICATE failed occurs in the call of store.connect method. If you specify session.debug = true and get the detailed log

DEBUG: setDebug: JavaMail version 1.6.2
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle]
DEBUG IMAPS: mail.imap.fetchsize: 16384
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
DEBUG IMAPS: closeFoldersOnStoreFailure
DEBUG IMAPS: trying to connect to host "outlook.office365.com", port 993, isSSL true
* OK The Microsoft Exchange IMAP4 service is ready. 
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: AUTH: XOAUTH2
DEBUG IMAPS: protocolConnect login, host=outlook.office365.com, user=Meru Adores, password=<non-null>
DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAPS: AUTHENTICATE PLAIN command result: A1 NO AUTHENTICATE failed.

Is authentication failing? This is the beginning of the stray. I tried using pop3s as the protocol and 995 as the port number.

DEBUG: setDebug: JavaMail version 1.6.2
DEBUG: getProvider() returning javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle]
DEBUG POP3: mail.pop3s.rsetbeforequit: false
DEBUG POP3: mail.pop3s.disabletop: false
DEBUG POP3: mail.pop3s.forgettopheaders: false
DEBUG POP3: mail.pop3s.cachewriteto: false
DEBUG POP3: mail.pop3s.filecache.enable: false
DEBUG POP3: mail.pop3s.keepmessagecontent: false
DEBUG POP3: mail.pop3s.starttls.enable: false
DEBUG POP3: mail.pop3s.starttls.required: false
DEBUG POP3: mail.pop3s.finalizecleanclose: false
DEBUG POP3: mail.pop3s.apop.enable: false
DEBUG POP3: mail.pop3s.disablecapa: false
DEBUG POP3: connecting to host "outlook.office365.com", port 995, isSSL true
+OK The Microsoft Exchange POP3 service is ready. 
CAPA
+OK
TOP
UIDL
SASL PLAIN
USER
.
DEBUG POP3: authentication command trace suppressed
DEBUG POP3: authentication command failed
QUIT
+OK Microsoft Exchange Server POP3 server signing off.

After all authentication has failed. If you search for information on the net, you will have to authenticate before @ instead of your email address, or you will not know the domain, so you will be proxyed or redirected to outlook.office365.com. I repeated trial and error for about half a day, such as if I had to connect directly to the server.

Answer

IMAP and POP3 were disabled in the user's mail connection settings in the Exchange Management Console. When I had to connect directly to the above Exchange server, I asked an administrator to access the management console. After all, TCP / IP did not pass directly, but I saw Document at this time. -and-imap4 / pop3-and-imap4) says, "By default, POP3 and IMAP4 are enabled for all Exchange Online users." But make sure you can disable them on a per-user basis. When I saw it, it was properly disabled. I enabled it there, and after a while (5-10 minutes), it worked smoothly!

image.png

Recommended Posts

It's not a big deal if you understand that I was addicted to receiving emails with Java Mail from Exchange Online
A story that I struggled to challenge a competition professional with Java
Android: How to deal with "Could not determine java version from '10 .0.1'"
A story that I was addicted to twice with the automatic startup setting of Tomcat 8 on CentOS 8
I tried learning Java with a series that beginners can understand clearly
A story that suffered from a space that does not disappear even if trimmed with Java The cause is BOM
I tried to make a program that searches for the target class from the process that is overloaded with Java
I was addicted to not being able to connect to AWS-S3 from the Docker container
SpringSecurity I was addicted to trying to log in with a hashed password (solved)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (gray magic that is not so much as black magic)
It's a pain to deal with old dates
I was addicted to doing onActivityResult () with DialogFragment
I tried to break a block with java (1)
What I was addicted to when developing a Spring Boot application with VS Code
MockMVC returns 200 even if I make a request to a path that does not exist
A site that was easy to understand when I was a beginner when I started learning Spring Boot
A story I was addicted to when getting a key that was automatically tried on MyBatis
Java: A story that made me feel uncomfortable when I was taught to compare strings with equals for no reason.