tlrowe9065
Programmer
- Aug 18, 2024
- 1
I'm trying to create a program that can send emails in the background using modern authentication methods (i.e. using tenant, client and secret authorization) with the MS Graph API. I've gone through all of the setup and permissions necessary through Azure. I've found this sample function code online, but the syntax is off. I'm not sure when it was written. Getting validation error in VS on Sendmail stating that it is readonly and I'm unsure of how to get past it.
Using VS 2022 v17.11. Any advice is greatly appreciated.
Public Async Function SendAsync(fromAddress As String, toAddress As String, subject As String, content As String) As Task
Dim tenantId As String = _config("tenantId")
Dim clientId As String = _config("clientId")
Dim clientSecret As String = _config("clientSecret")
Dim credential As New ClientSecretCredential(tenantId, clientId, clientSecret)
Dim graphClient As New GraphServiceClient(credential)
Dim message As New Message With {
.Subject = subject,
.Body = New ItemBody() With {
.ContentType = BodyType.Text,
.Content = content
},
.ToRecipients = New List(Of Recipient) From {
New Recipient() With {
.EmailAddress = New EmailAddress() With {
.Address = toAddress
}
}
}
}
Dim saveToSentItems As Boolean = True
Await graphClient.Users(fromAddress) _
.SendMail(message, saveToSentItems) _ <-- Erroring on SendMail
.Request() _
.PostAsync()
End Function
Using VS 2022 v17.11. Any advice is greatly appreciated.
Public Async Function SendAsync(fromAddress As String, toAddress As String, subject As String, content As String) As Task
Dim tenantId As String = _config("tenantId")
Dim clientId As String = _config("clientId")
Dim clientSecret As String = _config("clientSecret")
Dim credential As New ClientSecretCredential(tenantId, clientId, clientSecret)
Dim graphClient As New GraphServiceClient(credential)
Dim message As New Message With {
.Subject = subject,
.Body = New ItemBody() With {
.ContentType = BodyType.Text,
.Content = content
},
.ToRecipients = New List(Of Recipient) From {
New Recipient() With {
.EmailAddress = New EmailAddress() With {
.Address = toAddress
}
}
}
}
Dim saveToSentItems As Boolean = True
Await graphClient.Users(fromAddress) _
.SendMail(message, saveToSentItems) _ <-- Erroring on SendMail
.Request() _
.PostAsync()
End Function