Here is a non-functional example to try to send an email with the Microsoft Graph V6 java sdk
Dependencies - Gradle Koltin DSL
implementation("jakarta.annotation:jakarta.annotation-api:2.1.1")
implementation("com.azure:azure-identity:1.11.4") //authentification
implementation("com.microsoft.graph:microsoft-graph:6.4.0") // Microsoft graph for sending emails
V6 seems not to cause any errors here is the authentication and message creation part I followed the following example https://learn.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=java
val clientId = ""
val tenantId = ""
val clientSecret = ""
val scopes = arrayOf("https://graph.microsoft.com/.default")
val credential =
ClientSecretCredentialBuilder().clientId(clientId).tenantId(tenantId).clientSecret(clientSecret).build()
?: throw Exception("Unexpected error")
val graphClient = GraphServiceClient(credential, *scopes)
// ---------------------------------
// Prepare the email message
val message = Message()
message.subject = "Meet for lunch?"
val body = ItemBody()
body.contentType = BodyType.Html // Set content type to text
body.content = "The new cafeteria is open."
message.body = body
// Add recipient to the 'To' list
val toRecipients = LinkedList<Recipient>()
val toRecipient = Recipient()
val toEmailAddress = EmailAddress()
toEmailAddress.address = "[email protected]" // Recipient email address
toRecipient.emailAddress = toEmailAddress
toRecipients.add(toRecipient)
message.toRecipients = toRecipients
// Add recipient to the 'CC' list
val ccRecipients = LinkedList<Recipient>()
val ccRecipient = Recipient()
val ccEmailAddress = EmailAddress()
ccEmailAddress.address = "[email protected]" // CC recipient email address
ccRecipient.emailAddress = ccEmailAddress
ccRecipients.add(ccRecipient)
message.ccRecipients = ccRecipients
// Create SendMailPostRequestBody and set the message and other properties
val sendMailPostRequestBody = SendMailPostRequestBody()
sendMailPostRequestBody.message = message
sendMailPostRequestBody.saveToSentItems = false // Do not save the email to 'Sent Items'
val emailSender = "[email protected]"
V5 I can't build a functional email sender using the Microsoft V6 graph API I just put it there as an example of what I had before
this.emailSender?.let {
graphClient.users(it)
.sendMail(
UserSendMailParameterSet
.newBuilder()
.withMessage(message)
.withSaveToSentItems(true)
.build()
)
.buildRequest()
.post()
}
How can I replace this piece of V5 code in V6 to send an email, I think the V6 authentication I performed works
additional information
I'm trying to send an email from a defined email address Sender if I try to follow the microsoft example, which doesn't do it I get the following error
graphClient.me().sendMail().post(sendMailPostRequestBody, null)
// me() is not correct
Exception in thread "main" com.microsoft.graph.models.odataerrors.ODataError: /me request is only valid with delegated authentication flow.
at com.microsoft.graph.models.odataerrors.ODataError.createFromDiscriminatorValue(ODataError.java:36)
at com.microsoft.kiota.serialization.JsonParseNode.getObjectValue(JsonParseNode.java:210)
at com.microsoft.kiota.http.OkHttpRequestAdapter.lambda$throwIfFailedResponse$0(OkHttpRequestAdapter.java:672)
at com.microsoft.kiota.ApiExceptionBuilder.<init>(ApiExceptionBuilder.java:26)
at com.microsoft.kiota.http.OkHttpRequestAdapter.throwIfFailedResponse(OkHttpRequestAdapter.java:671)
at com.microsoft.kiota.http.OkHttpRequestAdapter.sendPrimitive(OkHttpRequestAdapter.java:341)
at com.microsoft.graph.users.item.sendmail.SendMailRequestBuilder.post(SendMailRequestBuilder.java:58)
at be.neuronics.microsoftgraphdemo.Main$Companion.main(Main.kt:71)
at be.neuronics.microsoftgraphdemo.Main.main(Main.kt)
````
I found how to make it work for those who are interested
useful link : https://learn.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=java#client-credentials-provider
https://github.com/microsoftgraph/msgraph-sdk-java/blob/dev/docs/upgrade-to-v6.md
https://learn.microsoft.com/en-us/graph/sdks/create-requests?tabs=java