EMailProfiler Common: Neue Ablehnungsmail-Logik

This commit is contained in:
2024-04-24 11:19:41 +02:00
parent 01da88aa54
commit 2898da380d
5 changed files with 153 additions and 42 deletions

View File

@@ -41,10 +41,11 @@ Public Class clsWorkEmail
Private ReadOnly _Patterns As Patterns2
Private ReadOnly _EmailAccountID As Integer = 1
Private ReadOnly _RejectionTemplateId As Integer = 0
Private _worked_email As Boolean = False
Sub New(LogConf As LogConfig, ConStr As String, WmConStr As String, pUseWindream As Boolean, EmailAccountID As Integer, EmlProfPraefix As String)
Sub New(LogConf As LogConfig, ConStr As String, WmConStr As String, pUseWindream As Boolean, EmailAccountID As Integer, EmlProfPraefix As String, pRejectionTemplateId As Integer)
Try
_Logger = LogConf.GetLogger
_LogConfig = LogConf
@@ -52,6 +53,8 @@ Public Class clsWorkEmail
_Logger.Debug("clsWorkmail _email initialized")
_UseWindream = pUseWindream
_Patterns = New Patterns2(LogConf)
_RejectionTemplateId = pRejectionTemplateId
_Logger.Debug($"_RejectionTemplateId: {_RejectionTemplateId}")
If pUseWindream Then
_windream = New clsWindream_allgemein(LogConf)
@@ -162,7 +165,9 @@ Public Class clsWorkEmail
'insert history und exit
InsertHistoryEntryWithStatus(CurrentMail, "REJECTED", oResult)
AddEmailToQueueMSSQL(CurrentMail.MessageId, oResult, "Email validation failed", _EmailAccountID)
'AddEmailToQueueMSSQL(CurrentMail.MessageId, oResult, "Email validation failed", _EmailAccountID)
AddToEmailQueueMSSQL(CurrentMail.MessageId, oResult, "Email validation failed", _EmailAccountID,
_RejectionTemplateId, ErrorCode.SenderValidationFailed, "", "")
' Return early from processing eml
Return True
@@ -186,7 +191,9 @@ Public Class clsWorkEmail
If CURRENT_ATTMT_COUNT = 0 Then
_Logger.Info("### Mail contained no Attachments!! ###")
Dim oBody = EmailStrings.EMAIL_NO_FERDS
If AddEmailToQueueMSSQL(CurrentMail.MessageId, oBody, "No Attachments", _EmailAccountID) = True Then
'If AddEmailToQueueMSSQL(CurrentMail.MessageId, oBody, "No Attachments", _EmailAccountID) = True Then
If AddToEmailQueueMSSQL(CurrentMail.MessageId, oBody, "No Attachments", _EmailAccountID,
_RejectionTemplateId, ErrorCode.NoAttachments, "", "") = True Then
CURRENT_ImapObject.DeleteMessageByUID(poUID)
End If
InsertHistoryEntryWithStatus(CurrentMail, "REJECTED", "No Attachments")
@@ -204,15 +211,136 @@ Public Class clsWorkEmail
End Try
End Function
Public Function AddEmailToQueueMSSQL(MessageId As String, BodyText As String, Comment As String, pEmailAccountId As Integer) As Boolean
''' <summary>
''' Method to decide wether we use the old or the new
''' Rejection E-mail method.
'''
''' TODO we have no information about the language of the receiver at the moment
''' </summary>
''' <param name="pMessageId">E-Mail Message ID</param>
''' <param name="pBodyText">Body Text</param>
''' <param name="pComment">Comment</param>
''' <param name="pEmailAccountId">Sending Profile from config</param>
''' <param name="pTemplateId">ID for E-Mail-Template from config</param>
''' <param name="pErrorCode">Error Code</param>
''' <param name="pParameter1">Zusätzlicher Parameter 1</param>
''' <param name="pParameter2">Zusätzlicher Parameter 2</param>
Public Function AddToEmailQueueMSSQL(pMessageId As String, pBodyText As String, pComment As String, pEmailAccountId As Integer,
pTemplateId As Integer, pErrorCode As ErrorCode, pParameter1 As String, pParameter2 As String) As Boolean
Dim useLegacyMethod = True
Dim oErrorCode As String = String.Empty
' ErrorCode valid?
If pErrorCode <> ErrorCode.Unknown Then
Dim intCode As Integer = DirectCast(pErrorCode, Integer)
oErrorCode = $"{EmailStrings.ErrorCodePraefix}{intCode}"
Dim oSQL = $"SELECT COUNT(*) FROM TBDD_GUI_LANGUAGE_PHRASE WHERE TITLE = '{oErrorCode}'"
If _DB_MSSQL.GetScalarValue(oSQL) > 0 Then
useLegacyMethod = False
Else
_Logger.Warn($"Rejection reason [{oErrorCode}] not found in TBDD_GUI_LANGUAGE_PHRASE!")
End If
End If
' Gibt es das Template in TBDD_EMAIL_TEMPLATE?
If useLegacyMethod = False AndAlso pTemplateId > 0 Then
Try
Dim oSQL = $"SELECT COUNT(*) FROM TBDD_EMAIL_TEMPLATE WHERE GUID = {pTemplateId}"
If _DB_MSSQL.GetScalarValue(oSQL) <= 0 Then
_Logger.Warn($"EMAIL_TEMPLATE [{pTemplateId}] not found in TBDD_EMAIL_TEMPLATE!")
useLegacyMethod = True
End If
Catch ex As Exception
_Logger.Error(ex)
useLegacyMethod = True
End Try
Else
_Logger.Debug($"RejectionTemplateId not configured!")
useLegacyMethod = True
End If
' Check if Stored Procedure PRDD_SEND_REJECTION_MAIL exists
If useLegacyMethod = False Then
Try
Dim oSQL = $"SELECT COUNT(*) FROM sys.objects WHERE type = 'P' AND OBJECT_ID = OBJECT_ID('dbo.PRDD_SEND_REJECTION_MAIL')"
If _DB_MSSQL.GetScalarValue(oSQL) <= 0 Then
_Logger.Warn($"Procedure ['PRDD_SEND_REJECTION_MAIL'] not found in Database!")
useLegacyMethod = True
End If
Catch ex As Exception
_Logger.Error(ex)
useLegacyMethod = True
End Try
End If
If useLegacyMethod = True Then
_Logger.Warn("New rejection mail logic is not configured correctly, use legacy logic instead!")
Return AddEmailToQueueMSSQL(pMessageId, pBodyText, pComment, pEmailAccountId)
Else
_Logger.Debug("New rejection mail logic is configured!")
Return AddEmailToQueueMSSQL(pMessageId, pTemplateId, oErrorCode, pEmailAccountId, pParameter1, pParameter2)
End If
End Function
''' <summary>
''' Function calls SP PRDD_SEND_REJECTION_MAIL
''' for sending rejection mail.
''' </summary>
''' <param name="pMessageId">E-Mail Message ID</param>
''' <param name="pTemplateId">GUID for TBDD_EMAIL_TEMPLATE from config</param>
''' <param name="pErrorCode">ErrorID (TBDD_GUI_LANGUAGE_PHRASE)</param>
''' <param name="pEmailAccountId">Sending profile from config</param>
''' <param name="pParameter1">Zusätzlicher Parameter 1</param>
''' <param name="pParameter2">Zusätzlicher Parameter 2</param>
Private Function AddEmailToQueueMSSQL(pMessageId As String, pTemplateId As Integer, pErrorCode As String, pEmailAccountId As Integer,
pParameter1 As String, pParameter2 As String) As Boolean
If pParameter1.IsNullOrEmpty Then
pParameter1 = ""
Else
pParameter1 = pParameter1.Replace("'", "''")
End If
If pParameter2.IsNullOrEmpty Then
pParameter2 = ""
Else
pParameter2 = pParameter2.Replace("'", "''")
End If
Try
Dim oReference = MessageId
Dim oExecute = $"EXECUTE dbo.PRDD_SEND_REJECTION_MAIL
'{pMessageId}'
, 0
, {pEmailAccountId}
, 'ZUGFeRD Service'
, {pTemplateId}
, '{pErrorCode}'
, '{pParameter1}'
, '{pParameter2}'
, 77"
Return _DB_MSSQL.ExecuteNonQuery(oExecute)
Catch ex As Exception
_Logger.Error(ex)
Return False
End Try
End Function
Public Function AddEmailToQueueMSSQL(pMessageId As String, pBodyText As String, pComment As String, pEmailAccountId As Integer) As Boolean
Try
Dim oReference = pMessageId
Dim oEmailTo = ""
Dim oSubject = $"{SUBJECT_PRAFIX} - {EmailStrings.EMAIL_SUBJECT_REJECTED}"
Dim oCreatedWho = "DDEmailProfiler"
Dim oMaskedBodyText = BodyText.Replace("'", "''")
Dim oMaskedBodyText = pBodyText.Replace("'", "''")
Dim oSubjectBodyText = String.Format(EmailStrings.EMAIL_SUBJECT_TEXT, CURRENT_MAIL_SUBJECT)
Dim oCompleteBodyText = oMaskedBodyText & oSubjectBodyText
@@ -222,7 +350,7 @@ Public Class clsWorkEmail
If IsNothing(oEmailAddress) OrElse String.IsNullOrWhiteSpace(oEmailAddress) Then
_Logger.Warn("Could not find email-address for MessageId {0}", MessageId)
_Logger.Warn("Could not find email-address for MessageId {0}", pMessageId)
oEmailTo = String.Empty
Else
oEmailTo = oEmailAddress
@@ -232,7 +360,7 @@ Public Class clsWorkEmail
_Logger.Debug("To: {0}", oEmailTo)
_Logger.Debug("Subject: {0}", oSubject)
_Logger.Debug("Body {0}", oFinalBodyText)
Dim osql = $"Select COALESCE(MAX(GUID), 0) FROM TBEMLP_HISTORY WHERE EMAIL_MSGID = '{MessageId}'"
Dim osql = $"Select COALESCE(MAX(GUID), 0) FROM TBEMLP_HISTORY WHERE EMAIL_MSGID = '{pMessageId}'"
Dim oHistoryID As Integer = _DB_MSSQL.GetScalarValue(osql)
Dim oInsert = $"INSERT INTO [dbo].[TBEMLP_EMAIL_OUT] (
@@ -250,14 +378,14 @@ Public Class clsWorkEmail
(77
,{pEmailAccountId}
,{oHistoryID}
,'{MessageId}'
,'{pMessageId}'
,77
,'{oEmailTo}'
,'{oSubject}'
,'{oFinalBodyText}'
,'{Comment}'
,'{pComment}'
,'{oCreatedWho}')"
Return _DB_MSSQL.ExecuteNonQuery(oInsert)
Return _DB_MSSQL.ExecuteNonQuery(oInsert)
Catch ex As Exception
_Logger.Error(ex)