Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to send email from vb.net program using modern authentication

Status
Not open for further replies.

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
 
Afraid I'm not familiar with the MS Graph API, so cannot offer any help or advice
 
Perhaps this will help.
SendMailMB(ToEmail, FromEMail, SubjectTxt, EText, , , , PDFfile, XlFil)

Function SendMailMB(ByVal sTo As String, ByVal sFrom As String, sSubject As String, sBody As String, Optional sCC As Boolean, _
Optional sBCC As Boolean, Optional sReplyTo As String = "", Optional sAttachment As String = "", _
Optional sAttachmentAlias As String = "", Optional sPriority As Integer = 1, Optional sHTML As Boolean) As Boolean
On Error GoTo Fejl 'Resume Next
Dim CCOk As Boolean, objEmail As Object, Re As DAO.Recordset, DocName, sSMTP, SendDir As Boolean
Set objEmail = CreateObject("CDO.Message")

objEmail.FROM = sFrom
If sCC Then
objEmail.CC = sTo
CCOk = True
ElseIf sBCC Then
objEmail.BCC = sTo
CCOk = True
End If
If Not IsBlank(sTo) And Not CCOk Then objEmail.To = sTo
On Error Resume Next
objEmail.Subject = sSubject
If sHTML Then objEmail.HTMLBody = sBody Else objEmail.TextBody = sBody

If Not IsBlank(sAttachment) Then objEmail.AddAttachment sAttachment
If Not IsBlank(sAttachmentAlias) Then objEmail.AddAttachment sAttachmentAlias
sSMTP = DLookup("WEB", "Firma")
Do While Left(sSMTP, 1) = "W"
sSMTP = Right(sSMTP, Len(sSMTP) - 1)
Loop
sSMTP = "SMTP" & sSMTP
If Len(sSMTP) > 6 Then 'Fik den til at køre igen efter denne "the sendusing configuration is invalid" MB 130207
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = sSMTP 'Name or IP of remote SMTP server
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
Else
'MsgBox GetMsg("Flet_028"), , SD
End If
If SendDir Then objEmail.Send Else objEmail.Display
objEmail.Send
If Err Then SendMailMB = False Else SendMailMB = True
ExitHer:
Exit Function
Fejl:
MsgBox Err.Description, , SD
Resume ExitHer
End Function
 
Ah, the whole point f the original post is that the poster wants to avoid using old, less secure methods - such as raw SMTP, which is what your VB6 example uses. And if they do want to use an enhanced SMTP, they'd be better off using the System.Net.Mail namespace, as they seem to be, rather than CDO
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top