This is what im trying to do. When testing this i only see the sending mail to log. There is no exception caught. I have the logging set to debug.
I assume this line goes in some timeout
graphClient.me().sendMail().post(mail)
class MailService {
val logger: Logger = LoggerFactory.getLogger(MailService::class.java)
private val clientId = ""
private val scopes = listOf("https://graph.microsoft.com/.default").toTypedArray()
private val credential = ManagedIdentityCredentialBuilder()
.clientId(clientId)
.build()
val graphClient = GraphServiceClient(credential, *scopes)
fun sendMail() {
try {
val mail = SendMailPostRequestBody()
val message: Message = buildMail()
message.ccRecipients = getRecipients()
mail.message = message
mail.saveToSentItems = false
logger.info("Sending mail to: ${getRecipients().joinToString(", ") { it.emailAddress.address }}")
graphClient.me().sendMail().post(mail)
logger.info("Mail sent successfully")
} catch (e: Exception) {
logger.error("Error sending mail: ${e.message}", e)
}
}
}