WIP Email Validation
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
Imports DigitalData.EMLProfiler.ClassCurrent
|
||||
Imports DigitalData.Modules.Logging
|
||||
Imports DigitalData.Modules.Messaging
|
||||
Imports Limilabs.Mail
|
||||
Imports Limilabs.Mail.MIME
|
||||
Imports Limilabs.Mail.Headers
|
||||
Imports DigitalData.Modules.Database
|
||||
Imports DigitalData.Modules.Base
|
||||
Imports DigitalData.Modules.Messaging.Mail
|
||||
Imports Limilabs.Mail
|
||||
Imports Limilabs.Mail.Headers
|
||||
|
||||
Public Class clsWorker
|
||||
Private ReadOnly Logger As Logger
|
||||
@@ -18,6 +17,9 @@ Public Class clsWorker
|
||||
Private ReadOnly Encryption As clsEncryption
|
||||
Private ReadOnly ProfileId As Integer = 0D
|
||||
Private ReadOnly EmailLimitationSender As String = ""
|
||||
Private ReadOnly EmailLimitationEnabled As Boolean = False
|
||||
|
||||
Private ReadOnly LocalEmlFile As String = ""
|
||||
|
||||
Sub New(pLogConfig As LogConfig, pConnectionString As String, pWindreamConnectionString As String, pPollProfileId As Integer, pUseWindream As Boolean, pEmailAccountID As Integer, pEmailPrefix As String, pEmailLimitationSender As String, Optional pLocalEML As String = "")
|
||||
Logger = pLogConfig.GetLogger
|
||||
@@ -27,6 +29,10 @@ Public Class clsWorker
|
||||
|
||||
UseWindream = pUseWindream
|
||||
EmailLimitationSender = pEmailLimitationSender
|
||||
If EmailLimitationSender.Contains("@") Then
|
||||
Logger.Info("Email Sender Limitation active for address: [{0}]", EmailLimitationSender)
|
||||
EmailLimitationEnabled = True
|
||||
End If
|
||||
|
||||
If UseWindream Then
|
||||
ClassWindreamAllgemein = New clsWindream_allgemein(pLogConfig)
|
||||
@@ -38,7 +44,7 @@ Public Class clsWorker
|
||||
|
||||
Encryption = New clsEncryption("!35452didalog=", pLogConfig)
|
||||
ProfileId = pPollProfileId
|
||||
CURRENT_DEBUG_LOCAL_EMAIL = pLocalEML
|
||||
LocalEmlFile = pLocalEML
|
||||
End Sub
|
||||
|
||||
Private Sub DeleteTempFiles()
|
||||
@@ -57,184 +63,196 @@ Public Class clsWorker
|
||||
TEMP_FILES.Clear()
|
||||
End Sub
|
||||
|
||||
Private Function LoadEmailAccounts() As DataTable
|
||||
Return Database.GetDatatable("SELECT * FROM TBDD_EMAIL_ACCOUNT WHERE ACTIVE = 1")
|
||||
End Function
|
||||
|
||||
Private Function LoadPollingProfiles(pProfileId As Integer) As DataTable
|
||||
Dim oSQL = "SELECT * FROM TBEMLP_POLL_PROFILES WHERE ACTIVE = 1"
|
||||
|
||||
If ProfileId = 0 Then
|
||||
oSQL &= " ORDER BY SEQUENCE"
|
||||
Else
|
||||
oSQL &= $" WHERE GUID = {pProfileId}"
|
||||
End If
|
||||
|
||||
Return Database.GetDatatable(oSQL)
|
||||
End Function
|
||||
|
||||
Public Sub Start_WorkingProfiles(Optional LocalEmail As Boolean = False)
|
||||
Try
|
||||
DeleteTempFiles()
|
||||
|
||||
If Database.DBInitialized = True Then
|
||||
Logger.Debug("now windream_init... ")
|
||||
If UseWindream Then
|
||||
If ClassWindreamAllgemein.Init = False Then
|
||||
Logger.Info("windream could not be initialized!!")
|
||||
Exit Sub
|
||||
Else
|
||||
Logger.Debug("windream_initialized!")
|
||||
End If
|
||||
End If
|
||||
Dim osql As String = "SELECT * FROM TBEMLP_POLL_PROFILES"
|
||||
If ProfileId = 0 Then
|
||||
osql &= " WHERE ACTIVE = 1 order by SEQUENCE"
|
||||
Else
|
||||
osql &= " WHERE GUID = " & ProfileId
|
||||
End If
|
||||
Dim DT_TBDD_EMAIL As DataTable = Database.GetDatatable("SELECT * FROM TBDD_EMAIL_ACCOUNT WHERE ACTIVE = 1")
|
||||
Dim DT_PROFILES = Database.GetDatatable(osql)
|
||||
If Not IsNothing(DT_PROFILES) Then
|
||||
If DT_PROFILES.Rows.Count > 0 Then
|
||||
Logger.Debug("count of active profiles: " & DT_PROFILES.Rows.Count.ToString)
|
||||
For Each oDR_Profile As DataRow In DT_PROFILES.Rows
|
||||
CURRENT_PROFILE_GUID = oDR_Profile.Item("GUID")
|
||||
DT_POLL_PROCESS = Nothing
|
||||
Dim sql = String.Format("SELECT * FROM TBEMLP_POLL_PROCESS WHERE PROFILE_ID = {0} AND ACTIVE = 1", CURRENT_PROFILE_GUID)
|
||||
DT_POLL_PROCESS = Database.GetDatatable(sql)
|
||||
If Not IsNothing(DT_POLL_PROCESS) Then
|
||||
If DT_POLL_PROCESS.Rows.Count = 0 Then
|
||||
Logger.Info("No processes configured for this Email-Profile - " & sql)
|
||||
Continue For
|
||||
Else
|
||||
DT_STEPS = Nothing
|
||||
DT_STEPS = Database.GetDatatable(String.Format("SELECT T.* FROM TBEMLP_POLL_STEPS T,TBEMLP_POLL_PROCESS T1 WHERE T.PROCESS_ID = T1.GUID AND T1.PROFILE_ID = {0} AND T1.ACTIVE = 1", CURRENT_PROFILE_GUID))
|
||||
|
||||
End If
|
||||
Else
|
||||
Logger.Warn("DT_POLL_PROCESS is nothing")
|
||||
Continue For
|
||||
End If
|
||||
|
||||
CURRENT_EMAIL_GUID = 0
|
||||
|
||||
CURRENT_POLL_TYPE = oDR_Profile.Item("POLL_TYPE")
|
||||
Logger.Debug(String.Format("Working on profile: ({0}-{1}-{2}) ", oDR_Profile.Item("GUID"), oDR_Profile.Item("PROFILE_NAME"), CURRENT_POLL_TYPE))
|
||||
CURRENT_EMAIL_GUID = oDR_Profile.Item("EMAIL_CONF_ID")
|
||||
|
||||
Dim EMAIL_PROF_TABLE As DataTable = DT_TBDD_EMAIL
|
||||
'Presuming the DataTable has a column named Date.
|
||||
Dim expression As String
|
||||
expression = "GUID = " & CURRENT_EMAIL_GUID
|
||||
Logger.Debug("Expression Filter email: " & expression)
|
||||
Logger.Debug("DT_TBDD_EMAIL.rowCount: " & DT_TBDD_EMAIL.Rows.Count)
|
||||
'Filter the rows using Select() method of DataTable
|
||||
Dim FilteredRows As List(Of DataRow) = EMAIL_PROF_TABLE.Select(expression).ToList()
|
||||
If FilteredRows.Count = 1 Then
|
||||
Dim oRow = FilteredRows(0)
|
||||
Dim MAIL_FROM = oRow("EMAIL_FROM")
|
||||
Dim MAIL_SERVER = oRow("EMAIL_SMTP")
|
||||
Dim MAIL_USER = oRow("EMAIL_USER")
|
||||
Dim MAIL_USER_PW = oRow("EMAIL_PW")
|
||||
Dim MAIL_PORT = oRow("PORT_IN")
|
||||
Dim MAIL_INBOX_NAME = "Inbox"
|
||||
Dim MAIL_ARCHIVE_FOLDER = oRow("ARCHIVE_FOLDER")
|
||||
Dim MAIL_AUTHTYPE = oRow("AUTH_TYPE")
|
||||
Logger.Debug(String.Format("{0}-{1}", MAIL_FROM, MAIL_SERVER))
|
||||
|
||||
Dim PWPlain = Encryption.DecryptData(MAIL_USER_PW)
|
||||
If Not IsNothing(PWPlain) Then
|
||||
If PWPlain <> "" Then
|
||||
MAIL_USER_PW = PWPlain
|
||||
Else
|
||||
Logger.Warn("PWPlain is string.empty - Could not decrypt passwort")
|
||||
End If
|
||||
Else
|
||||
Logger.Warn("PWPlain is nothing - Could not decrypt passwort")
|
||||
End If
|
||||
|
||||
CURRENT_WORKMAIL_UID_LIST.Clear()
|
||||
|
||||
If MAIL_SERVER <> "" Then
|
||||
Dim oPollResult As Boolean = False
|
||||
If LocalEmail = True Then
|
||||
oPollResult = True
|
||||
Else
|
||||
Select Case CURRENT_POLL_TYPE
|
||||
Case "IMAP"
|
||||
' We are using 'Archive Folder' as an additional field to save the Tenant-ID for O365-OAuth2
|
||||
oPollResult = FetchMessages(MAIL_SERVER, MAIL_PORT, MAIL_USER, MAIL_USER_PW, MAIL_AUTHTYPE, MAIL_ARCHIVE_FOLDER)
|
||||
|
||||
Case Else
|
||||
Logger.Error("Poll Type [{0}] is not supported!", CURRENT_POLL_TYPE)
|
||||
oPollResult = False
|
||||
End Select
|
||||
End If
|
||||
|
||||
If CURRENT_WORKMAIL_UID_LIST.Count() > 0 Or LocalEmail = True Then
|
||||
If LocalEmail Then
|
||||
Logger.Info("Working with local Mail")
|
||||
Dim oEmail As IMail = New MailBuilder().CreateFromEmlFile(CURRENT_DEBUG_LOCAL_EMAIL)
|
||||
ClassWorkMail.WorkEmailMessage(oEmail, 123456789)
|
||||
'CURRENT_MAIL_MESSAGE = Nothing
|
||||
Else
|
||||
Try
|
||||
Logger.Info(String.Format("Pulled: [{0}] E-Mails", CURRENT_WORKMAIL_UID_LIST.Count()))
|
||||
Dim LIMIT_EMAIL_FROM As Boolean = False
|
||||
If EmailLimitationSender.Contains("@") Then
|
||||
LIMIT_EMAIL_FROM = True
|
||||
Logger.Info("####################################")
|
||||
Logger.Info($"LIMIT_EMAIL_FROM is ACTIVE - EMAIL_FROM = [{EmailLimitationSender}]")
|
||||
Logger.Info("####################################")
|
||||
Logger.Debug("####################################")
|
||||
Logger.Debug($"LIMIT_EMAIL_FROM is ACTIVE - EMAIL_FROM = [{EmailLimitationSender}]")
|
||||
Logger.Debug("####################################")
|
||||
End If
|
||||
For Each oUID In CURRENT_WORKMAIL_UID_LIST
|
||||
Dim oEmail As IMail = Fetcher.FetchMail(oUID)
|
||||
|
||||
If Not IsNothing(oEmail) Then
|
||||
If LIMIT_EMAIL_FROM Then
|
||||
Dim oEmailFrom As String = ""
|
||||
For Each m As MailBox In oEmail.From
|
||||
oEmailFrom = m.Address
|
||||
Next
|
||||
If oEmailFrom <> EmailLimitationSender Then
|
||||
Logger.Debug($"Skipping email {oEmailFrom} ...Subject [{oEmail.Subject}]")
|
||||
Continue For
|
||||
End If
|
||||
End If
|
||||
If ClassWorkMail.WorkEmailMessage(oEmail, oUID) = True Then
|
||||
If CURRENT_DEBUG_LOCAL_EMAIL = "" Then
|
||||
EMAIL_DELETE()
|
||||
End If
|
||||
End If
|
||||
|
||||
Else
|
||||
Logger.Info("### oEmail was nothing ###")
|
||||
End If
|
||||
|
||||
'CURRENT_MAIL_MESSAGE = Nothing
|
||||
Next
|
||||
If CURRENT_POLL_TYPE = "IMAP" And CURRENT_WORKMAIL_UID_LIST.Count > 0 Then
|
||||
If Not IsNothing(Fetcher.Client) Then
|
||||
Fetcher.Client.Close()
|
||||
End If
|
||||
End If
|
||||
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Logger.Warn($"Unexpected Error working CURRENT_WORKMAIL_UID_LIST: {ex.Message} ")
|
||||
End Try
|
||||
|
||||
End If
|
||||
Else
|
||||
Logger.Debug(String.Format("No emails for profile!"))
|
||||
End If
|
||||
|
||||
Else
|
||||
Logger.Warn("For the Email-Profile ID " & CURRENT_EMAIL_GUID & " no record could be found!")
|
||||
End If
|
||||
|
||||
Database.ExecuteNonQuery("UPDATE TBEMLP_POLL_PROFILES SET LAST_TICK = GETDATE() WHERE GUID = " & oDR_Profile.Item("GUID").ToString)
|
||||
Else
|
||||
Logger.Warn("For the Email-Profile ID " & CURRENT_EMAIL_GUID & " no record could be found! Check wether Email-Profile is active!")
|
||||
End If
|
||||
Next
|
||||
DeleteTempFiles()
|
||||
|
||||
Else
|
||||
Logger.Info("No active profiles")
|
||||
End If
|
||||
End If
|
||||
|
||||
Database.ExecuteNonQuery("UPDATE TBEMLP_CONFIG SET LAST_TICK = GETDATE() WHERE GUID = 1")
|
||||
If Database.DBInitialized = False Then
|
||||
Logger.Warn("Database is not initialized. Exiting.")
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Logger.Debug("now windream_init... ")
|
||||
If UseWindream Then
|
||||
If ClassWindreamAllgemein.Init = False Then
|
||||
Logger.Info("windream could not be initialized!!")
|
||||
Exit Sub
|
||||
Else
|
||||
Logger.Debug("windream_initialized!")
|
||||
End If
|
||||
End If
|
||||
|
||||
Dim EmailAccountTable As DataTable = LoadEmailAccounts()
|
||||
Dim PollingProfileTable = LoadPollingProfiles(ProfileId)
|
||||
|
||||
If IsNothing(PollingProfileTable) Then
|
||||
Logger.Warn("Error while fetching Polling Profiles. Exiting.")
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
If PollingProfileTable.Rows.Count = 0 Then
|
||||
Logger.Warn("No active Polling Profiles found. Exiting.")
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Logger.Debug("Count of active profiles: " & PollingProfileTable.Rows.Count.ToString)
|
||||
|
||||
For Each oProfile As DataRow In PollingProfileTable.Rows
|
||||
|
||||
Dim oValidationSql = oProfile.ItemEx("VALIDATION_SQL", "")
|
||||
|
||||
CURRENT_PROFILE_GUID = oProfile.Item("GUID")
|
||||
DT_POLL_PROCESS = Nothing
|
||||
Dim sql = String.Format("SELECT * FROM TBEMLP_POLL_PROCESS WHERE PROFILE_ID = {0} AND ACTIVE = 1", CURRENT_PROFILE_GUID)
|
||||
DT_POLL_PROCESS = Database.GetDatatable(sql)
|
||||
If Not IsNothing(DT_POLL_PROCESS) Then
|
||||
If DT_POLL_PROCESS.Rows.Count = 0 Then
|
||||
Logger.Info("No processes configured for this Email-Profile - " & sql)
|
||||
Continue For
|
||||
Else
|
||||
DT_STEPS = Nothing
|
||||
DT_STEPS = Database.GetDatatable($"SELECT T.* FROM TBEMLP_POLL_STEPS T,TBEMLP_POLL_PROCESS T1 WHERE T.PROCESS_ID = T1.GUID AND T1.PROFILE_ID = {CURRENT_PROFILE_GUID} AND T1.ACTIVE = 1")
|
||||
|
||||
End If
|
||||
Else
|
||||
Logger.Warn("DT_POLL_PROCESS is nothing")
|
||||
Continue For
|
||||
End If
|
||||
|
||||
CURRENT_EMAIL_GUID = 0
|
||||
|
||||
CURRENT_POLL_TYPE = oProfile.Item("POLL_TYPE")
|
||||
Logger.Debug(String.Format("Working on profile: ({0}-{1}-{2}) ", oProfile.Item("GUID"), oProfile.Item("PROFILE_NAME"), CURRENT_POLL_TYPE))
|
||||
CURRENT_EMAIL_GUID = oProfile.Item("EMAIL_CONF_ID")
|
||||
|
||||
Dim FilteredRows As List(Of DataRow) = EmailAccountTable.
|
||||
Select($"GUID = {CURRENT_EMAIL_GUID}").
|
||||
ToList()
|
||||
Logger.Debug("FilteredRows: " & FilteredRows.Count)
|
||||
|
||||
If FilteredRows.Count = 1 Then
|
||||
Dim oRow = FilteredRows(0)
|
||||
Dim oMailFrom = oRow("EMAIL_FROM")
|
||||
Dim oMailServer = oRow("EMAIL_SMTP")
|
||||
Dim oMailUser = oRow("EMAIL_USER")
|
||||
Dim oMailPassword = oRow("EMAIL_PW")
|
||||
Dim oMailPortIn = oRow("PORT_IN")
|
||||
Dim oMailboxName = "Inbox"
|
||||
Dim oMailArchiveFolder = oRow("ARCHIVE_FOLDER")
|
||||
Dim oMailAuthType = oRow("AUTH_TYPE")
|
||||
|
||||
Logger.Debug("Mail Server: {0}", oMailServer)
|
||||
Logger.Debug("Mail From: {0}", oMailFrom)
|
||||
|
||||
Dim PWPlain = Encryption.DecryptData(oMailPassword)
|
||||
If Not IsNothing(PWPlain) Then
|
||||
If PWPlain <> "" Then
|
||||
oMailPassword = PWPlain
|
||||
Else
|
||||
Logger.Warn("PWPlain is string.empty - Could not decrypt passwort")
|
||||
End If
|
||||
Else
|
||||
Logger.Warn("PWPlain is nothing - Could not decrypt passwort")
|
||||
End If
|
||||
|
||||
CURRENT_WORKMAIL_UID_LIST.Clear()
|
||||
|
||||
If oMailServer <> "" Then
|
||||
Dim oPollResult As Boolean = False
|
||||
If LocalEmail = True Then
|
||||
oPollResult = True
|
||||
Else
|
||||
Select Case CURRENT_POLL_TYPE
|
||||
Case "IMAP"
|
||||
' We are using 'Archive Folder' as an additional field to save the Tenant-ID for O365-OAuth2
|
||||
oPollResult = FetchMessages(oMailServer, oMailPortIn, oMailUser, oMailPassword, oMailAuthType, oMailArchiveFolder)
|
||||
|
||||
Case Else
|
||||
Logger.Error("Poll Type [{0}] is not supported!", CURRENT_POLL_TYPE)
|
||||
oPollResult = False
|
||||
End Select
|
||||
End If
|
||||
|
||||
If CURRENT_WORKMAIL_UID_LIST.Count() > 0 Or LocalEmail = True Then
|
||||
If LocalEmail Then
|
||||
Logger.Info("Working with local Mail")
|
||||
Dim oEmail As IMail = New MailBuilder().CreateFromEmlFile(LocalEmlFile)
|
||||
ClassWorkMail.WorkEmailMessage(oEmail, 123456789, oValidationSql)
|
||||
'CURRENT_MAIL_MESSAGE = Nothing
|
||||
Else
|
||||
Try
|
||||
Logger.Info(String.Format("Pulled: [{0}] E-Mails", CURRENT_WORKMAIL_UID_LIST.Count()))
|
||||
|
||||
For Each oMailId In CURRENT_WORKMAIL_UID_LIST
|
||||
Dim oEmail As IMail = Fetcher.FetchMail(oMailId)
|
||||
|
||||
If Not IsNothing(oEmail) Then
|
||||
If EmailLimitationEnabled Then
|
||||
Dim oEmailFrom As String = ""
|
||||
For Each m As MailBox In oEmail.From
|
||||
oEmailFrom = m.Address
|
||||
Next
|
||||
If oEmailFrom <> EmailLimitationSender Then
|
||||
Logger.Debug($"Skipping email {oEmailFrom} ...Subject [{oEmail.Subject}]")
|
||||
Continue For
|
||||
End If
|
||||
End If
|
||||
If ClassWorkMail.WorkEmailMessage(oEmail, oMailId, oValidationSql) = True Then
|
||||
If LocalEmlFile = "" Then
|
||||
DeleteEmailFile(oMailId)
|
||||
End If
|
||||
End If
|
||||
|
||||
Else
|
||||
Logger.Info("### oEmail was nothing ###")
|
||||
End If
|
||||
|
||||
'CURRENT_MAIL_MESSAGE = Nothing
|
||||
Next
|
||||
|
||||
If CURRENT_POLL_TYPE = "IMAP" And CURRENT_WORKMAIL_UID_LIST.Count > 0 Then
|
||||
Fetcher.Disconnect()
|
||||
End If
|
||||
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Logger.Warn($"Unexpected Error working CURRENT_WORKMAIL_UID_LIST: {ex.Message} ")
|
||||
End Try
|
||||
|
||||
End If
|
||||
Else
|
||||
Logger.Debug(String.Format("No emails for profile!"))
|
||||
End If
|
||||
|
||||
Else
|
||||
Logger.Warn("For the Email-Profile ID " & CURRENT_EMAIL_GUID & " no record could be found!")
|
||||
End If
|
||||
|
||||
Database.ExecuteNonQuery("UPDATE TBEMLP_POLL_PROFILES SET LAST_TICK = GETDATE() WHERE GUID = " & oProfile.Item("GUID").ToString)
|
||||
Else
|
||||
Logger.Warn("For the Email-Profile ID " & CURRENT_EMAIL_GUID & " no record could be found! Check wether Email-Profile is active!")
|
||||
End If
|
||||
Next
|
||||
|
||||
DeleteTempFiles()
|
||||
|
||||
Database.ExecuteNonQuery("UPDATE TBEMLP_CONFIG SET LAST_TICK = GETDATE() WHERE GUID = 1")
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
End Try
|
||||
@@ -280,27 +298,26 @@ Public Class clsWorker
|
||||
|
||||
CURRENT_WORKMAIL_UID_LIST = oMailIds
|
||||
|
||||
Fetcher.Disconnect()
|
||||
|
||||
Return True
|
||||
|
||||
Catch ex As Exception
|
||||
Logger.Error(ex)
|
||||
Return False
|
||||
|
||||
End Try
|
||||
End Function
|
||||
Private Sub EMAIL_DELETE()
|
||||
Private Sub DeleteEmailFile(pMailId As Integer)
|
||||
Try
|
||||
If DeleteMail = True And MessageError = False Then
|
||||
If DeleteMail = True And MESSAGE_ERROR = False Then
|
||||
If IsNothing(Fetcher.Client) Then
|
||||
Logger.Warn("EMAIL_DELETE - CURRENT_ImapObject is nothing")
|
||||
End If
|
||||
|
||||
Fetcher.Client.DeleteMessageByUID(CURRENT_MAIL_UID)
|
||||
Logger.Info("Email with Id [{0}] was deleted.", CURRENT_MAIL_UID)
|
||||
Fetcher.Client.DeleteMessageByUID(pMailId)
|
||||
Logger.Info("Email with Id [{0}] was deleted.", pMailId)
|
||||
Else
|
||||
If MessageError = True Then
|
||||
Logger.Warn("Did not delete Message with UID [{0}] as there was an MessageError!", CURRENT_MAIL_UID)
|
||||
If MESSAGE_ERROR = True Then
|
||||
Logger.Warn("Did not delete Message with UID [{0}] as there was an MessageError!", pMailId)
|
||||
End If
|
||||
End If
|
||||
Catch ex As Exception
|
||||
|
||||
Reference in New Issue
Block a user