Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Messaging
Imports DigitalData.Modules.Messaging.Mail

Public Class clsJob_Work
    Dim Logger As Logger
    Private MyLogger As LogConfig
    Private _mail As MailSender
    Public Sub New(MyLoggerConf As LogConfig, _email As MailSender)
        Logger = MyLoggerConf.GetLogger()
        MyLogger = MyLoggerConf
        _mail = _email
    End Sub
    Public Function New_Mail_with_attachment(Email_receipiants As String, Email_subject As String, Email_Body As String, EmailProfil As Integer, DTTBDD_EMAIL As DataTable, Attachment_Filename As String)
        Try
            Logger.Debug("Email_Empfänger: " & Email_receipiants)
            Logger.Debug("Email_Betreff: " & Email_subject)
            Logger.Debug("Email_Body: " & Email_Body)
            Logger.Debug("EMAIL_PROFIL: " & EmailProfil)
            Dim oMAILFROM As String = ""
            Dim oMAILSMTP As String = ""
            Dim oMAIL_USER As String = ""
            Dim oMAIL_USER_PW As String = ""
            Dim oMAIL_AUTH_TYPE As String = "SSL"
            Dim oMAIL_PORT As String = "25"
            If IsNothing(DTTBDD_EMAIL) And DTTBDD_EMAIL.Rows.Count >= 1 Then
                Logger.Warn("DT_TBDD_EMAIL is nothing or contains no rows")
                Return False
            End If
            For Each emailrow As DataRow In clsCURRENT.DT_TBDD_EMAIL.Rows
                If emailrow.Item("GUID") = CInt(EmailProfil) Then
                    oMAILFROM = emailrow.Item("EMAIL_FROM")
                    oMAILSMTP = emailrow.Item("EMAIL_SMTP")
                    oMAIL_USER = emailrow.Item("EMAIL_USER")
                    oMAIL_USER_PW = emailrow.Item("EMAIL_PW")
                    oMAIL_AUTH_TYPE = emailrow.Item("AUTH_TYPE")
                    oMAIL_PORT = emailrow.Item("PORT")
                End If
            Next

            Dim owrapper As New clsEncryption("!35452didalog=")

            Dim oPWPlain = owrapper.DecryptData(oMAIL_USER_PW)
            If Not IsNothing(oPWPlain) Then
                oMAIL_USER_PW = oPWPlain
            Else
                Logger.Warn("PWPlain is Nothing - Could not decrypt passwort 44")
                Return False
            End If
            Dim oSendto As New List(Of String)
            Dim oSplit = Email_receipiants.Split(";")
            For Each oMailAdress In oSplit
                oSendto.Add(oMailAdress)
            Next

            Dim oAttMt As New List(Of String)
            If Attachment_Filename <> String.Empty Then
                oAttMt.Add(Attachment_Filename)
            End If
            Dim oSession = _mail.Connect(oMAILSMTP, oMAIL_PORT, oMAIL_USER, oPWPlain, oMAIL_AUTH_TYPE)
            If oSession.Connected = True Then
                Logger.Info($"MAIL: Connection to {oMAILSMTP} successfull!")
                If _mail.SendMail(oSendto, oMAILFROM, Email_subject, Email_Body, Now, oAttMt, 0) = True Then
                    Return True

                Else
                    Logger.Warn("ConnectToServer was not successfull!")
                    Return False
                End If
            End If
        Catch ex As Exception
            Logger.Error(ex)
            Return False
        End Try
    End Function
End Class
