vb.net ID must set beim Senden von E-Mails mittels OAuth2

seit neustem bekomme ich eine Fehlermeldung sobald ich eine Mail versenden will. Und ich finde weder die Ursache noch eine Lösung zu dem Problem. Und da ich nichts im Internet finden konnte, glaube ich das ich irgendwo einen Fehler habe, den ich aber nicht sehe.

Hier der Code worüber ich E-Mails über den Exchange Onlineclient versende:

Public Class MyEws
    Private _ClientSecret As String, _MailBoxAdress As String
    Private ReadOnly AppId As String = "1234-5841-1A242-aaaa-Test", TenantId As String = "Test-8544-321-Test-12dbcf"

    Public WriteOnly Property ClientSecret As String
        Set
            _ClientSecret = Value
        End Set
    End Property

    Public Property MailBoxAdress As String
        Get
            Return _MailBoxAdress
        End Get
        Set
            _MailBoxAdress = Value
        End Set
    End Property

    Public Sub New(ClientSecret As String, MailBoxAdress As String)
        Me.ClientSecret = ClientSecret
        Me.MailBoxAdress = MailBoxAdress
    End Sub

    Public Sub New(ClientSecret As String)
        Me.ClientSecret = ClientSecret
    End Sub

    Public Function CreateAuthenticateExchangeService() As ExchangeService
        Dim Ccab As IConfidentialClientApplication
        Dim EwsScopes = {"https://outlook.office365.com/.default"}
        Dim Result As AuthenticationResult, EwsClient As ExchangeService
        Dim ClientOptions As New ConfidentialClientApplicationOptions With {.ClientId = Me.AppId, .ClientSecret = _ClientSecret, .TenantId = Me.TenantId}
        Try
            Ccab = ConfidentialClientApplicationBuilder.CreateWithApplicationOptions(ClientOptions).Build()
            Dim t = Ccab.AcquireTokenForClient(EwsScopes).ExecuteAsync
            t.Wait()
            Result = t.Result
            EwsClient = New ExchangeService(ExchangeVersion.Exchange2013_SP1) With {
            .Url = New Uri("https://outlook.office365.com/EWS/Exchange.asmx"),
            .Credentials = New OAuthCredentials(Result.AccessToken),
            .ImpersonatedUserId = New ImpersonatedUserId(ConnectingIdType.SmtpAddress, MailBoxAdress)}
            Return EwsClient
        Catch ex As Exception
            Dim t = String.Join(Environment.NewLine, Date.Now.ToString("F", ciDE), "Folgender Fehler ist im EwsExportMailAttachment - OAuth2 aufgetreten:", "Fehlernummer: " & ex.HResult, "Fehlerbeschreibung: " & ex.Message, "Stacktrace: " & ex.StackTrace)
            Dim t2 As New MyFile(Path.Combine(Application.StartupPath, "Log.txt"))
            t2.Write(t, True)
            Return Nothing
        End Try
    End Function
    
Public Sub SendMail(Mail As MimeMessage)
        MailSend(Mail, Nothing)
End Sub

Public Sub SendMail(Mail As MimeMessage, Attachment As MyFile)
        Dim MyAttachments As New List(Of MyFile) From {Attachment}
        MailSend(Mail, MyAttachments)
End Sub

    Public Sub SendMail(Mail As MimeMessage, Attachment As List(Of MyFile))
        MailSend(Mail, Attachment)
    End Sub
    Private Sub MailSend(Mail As MimeMessage, Attachments As List(Of MyFile))
        Dim Ews = CreateAuthenticateExchangeService()
        Dim Message = New EmailMessage(Ews)
        With Message
            .Sender = New EmailAddress(Mail.Sender.Name, Mail.Sender.Address)
            .From = .Sender
            .Subject = Mail.Subject
            Dim Recipients = Mail.GetRecipients(True)
            For i = 0 To Recipients.Count - 1
                Dim Receiver = Recipients(i)
                .ToRecipients.Add(New EmailAddress(Receiver.Name, Receiver.Address))
            Next
            If Mail.Cc IsNot Nothing Then
                For i = 0 To Mail.Cc.Count - 1
                    Dim CC = DirectCast(Mail.Cc(i), MailboxAddress)
                    .CcRecipients.Add(New EmailAddress(CC.Name, CC.Address))
                Next
            End If
            If Mail.Bcc IsNot Nothing Then
                For i = 0 To Mail.Bcc.Count - 1
                    Dim BCC = DirectCast(Mail.Bcc(i), MailboxAddress)
                    .BccRecipients.Add(New EmailAddress(BCC.Name, BCC.Address))
                Next
            End If
            If Mail.Body.ContentType.MimeType = "text/html" Then
                .Body = New MessageBody(BodyType.HTML, Mail.GetTextBody(Text.TextFormat.Html))
            Else
                .Body = New MessageBody(BodyType.Text, If(Mail.GetTextBody(Text.TextFormat.Plain), String.Empty))
            End If
            If Attachments IsNot Nothing Then
                For i = 0 To Attachments.Count - 1
                    .Attachments.AddFileAttachment(Attachments(i).FileName & Attachments(i).Extension, Attachments(i).FullPath)
                Next
            End If
        End With
        Message.SendAndSaveCopy()
    End Sub
End Class

Hier noch die Fehlermeldung inkl. Stacktrace

System.ArgumentException
  HResult=0x80070057
  Nachricht = The Id property must be set.
  Quelle = Microsoft.Exchange.WebServices
  Stapelüberwachung:
   bei Microsoft.Exchange.WebServices.Data.ImpersonatedUserId.WriteToXml(EwsServiceXmlWriter writer)
   bei Microsoft.Exchange.WebServices.Data.ServiceRequestBase.WriteToXml(EwsServiceXmlWriter writer)
   bei Microsoft.Exchange.WebServices.Data.ServiceRequestBase.EmitRequest(IEwsHttpWebRequest request)
   bei Microsoft.Exchange.WebServices.Data.ServiceRequestBase.BuildEwsHttpWebRequest()
   bei Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request)
   bei Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
   bei Microsoft.Exchange.WebServices.Data.ExchangeService.InternalCreateItems(IEnumerable`1 items, FolderId parentFolderId, Nullable`1 messageDisposition, Nullable`1 sendInvitationsMode, ServiceErrorHandling errorHandling)
   bei Microsoft.Exchange.WebServices.Data.Item.InternalCreate(FolderId parentFolderId, Nullable`1 messageDisposition, Nullable`1 sendInvitationsMode)
   bei Microsoft.Exchange.WebServices.Data.EmailMessage.InternalSend(FolderId parentFolderId, MessageDisposition messageDisposition)
   bei Test.MyEws.MailSend(MimeMessage Mail, List`1 Attachments) in C:Usersmaurice.rothmannsourcereposTestMailCl.vb: Zeile254
   bei Test.MyEws.SendMail(MimeMessage Mail) in C:Usersmaurice.rothmannsourcereposTestMailCl.vb: Zeile210
   bei Test.Form2.KryptonButton1_Click(Object sender, EventArgs e) in C:Usersmaurice.rothmannsourcereposTestForm2.vb: Zeile35
   bei System.Windows.Forms.Control.OnClick(EventArgs e)
   bei Krypton.Toolkit.KryptonButton.OnClick(EventArgs e)
   bei Krypton.Toolkit.KryptonButton.OnButtonClick(Object sender, MouseEventArgs e)
   bei Krypton.Toolkit.ButtonController.MouseUp(Control c, Point pt, MouseButtons button)
   bei Krypton.Toolkit.ViewManager.MouseUp(MouseEventArgs e, Point rawPt)
   bei Krypton.Toolkit.VisualControlBase.OnMouseUp(MouseEventArgs e)
   bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   bei System.Windows.Forms.Control.WndProc(Message& m)
   bei Krypton.Toolkit.VisualControlBase.WndProc(Message& m)
   bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   bei System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   bei Test.Form1.Button3_Click(Object sender, EventArgs e) in C:Usersmaurice.rothmannsourcereposTestForm1.vb: Zeile9
   bei System.Windows.Forms.Control.OnClick(EventArgs e)
   bei System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   bei System.Windows.Forms.Control.WndProc(Message& m)
   bei System.Windows.Forms.ButtonBase.WndProc(Message& m)
   bei System.Windows.Forms.Button.WndProc(Message& m)
   bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   bei Test.My.MyApplication.Main(String[] Args) in : Zeile83

Ich hoffe ich habe damit alle benötigten Informationen angegeben.
Die AppId & TenantId aus dem M365 habe ich verändert.

Ich habe Google bemüht und nachgesehen ob das Clientsecret noch gültig ist (ist es bis Mitte 2025).

Die E-Mails sollen automatisch und ohne Userinteraktion versendet werden.

Um es im Forum übersichtlicher zu halten, habe ich nur den verwendeten Code aus der Klasse eingefügt.

Vielen Dank schonmal im Vorraus.

LG
Numbfumble

New contributor

Numbfumble is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật