Sub RunThroughInbox(ByVal sender As Object, _
ByVal e As ElapsedEventArgs) _
Handles cbInboxTimer.Elapsed
Try
If profile.RunService Then
If profile.WriteServiceRunningMessageToLog Then
EventLog.WriteEntry("NECSInBox", profile.GetServiceRunningMessage)
End If
Dim olApp As Outlook.Application = New Outlook.Application
Dim olNS As Outlook.NameSpace = olApp.GetNamespace("MAPI")
olNS.Logon(profile.GetDefaultMailProfile, Missing.Value, False, True)
Dim olIn As Outlook.MAPIFolder = olNS.Folders.Item(profile.GetNecsMailbox)
Dim olInbox As Outlook.MAPIFolder = olIn.Folders.Item(profile.GetNecsMailboxInbox)
Dim olItems As Outlook.Items = olInbox.Items
Dim boxCount As Integer = olItems.Count
Dim i As Integer
Dim cnt As Integer = 0
Dim delKey As Integer
'********************
'Dim strMessage As String
'********************
Try
For i = boxCount To 1 Step -1
Dim err As String = ""
Dim keyIndexSubject As String = String.Empty
Dim keyIndexBody As String = String.Empty
Dim emailSubject As String = olItems.Item(i).Subject
Dim receiptBody As String = olItems.Item(i).Body
'Check for return messages that get deleted without processing.
keyIndexSubject = ContainsValue(_deleteAtOnce, emailSubject)
If keyIndexSubject Is String.Empty Then
delKey = 1
keyIndexSubject = ContainsValue(_deleteMessages, emailSubject)
keyIndexBody = ContainsValue(_deleteMessages, receiptBody)
'If no deleted flags, check for messages that get marked.
If keyIndexBody Is String.Empty Then
keyIndexBody = ContainsValue(_strikeMessages, receiptBody)
Else
delKey = -1
End If
If keyIndexSubject Is String.Empty Then
keyIndexSubject = ContainsValue(_strikeMessages, emailSubject)
Else
delKey = -1
End If
If TypeOf olItems.Item(i) Is Outlook.ReportItem Then
cnt += 1
Dim report As Outlook.ReportItem = CType(olItems.Item(i), Outlook.ReportItem)
If report.Attachments.Count > 0 Then
Dim returnedEmail As Outlook.Attachment = report.Attachments.Item(1)
returnedEmail.SaveAsFile("c:\" & returnedEmail.FileName)
Dim emailBody = GetMessageBody("c:\" & returnedEmail.FileName, err)
If err = "" Then
Dim key As String = GetKeyName(String.Empty, keyIndexBody)
Dim invalidEmail As String = RegExValue(emailBody, profile.GetInboxEmailRegExPattern)
Dim matchGuid As String = RegExValue(emailBody, profile.GetInboxGuidRegExPattern)
If CheckIfSubscriberOnly(invalidEmail) Then
DeleteSubscriberEmail(invalidEmail)
olItems.Item(i).Delete()
Else
If Not matchGuid Is String.Empty Then
Dim parsedSubject = emailSubject.Replace("Undeliverable:", String.Empty).Trim
SaveReturnedEmails(matchGuid, invalidEmail, parsedSubject, receiptBody, -1)
olItems.Item(i).Delete()
File.Delete("c:\" & returnedEmail.FileName)
'strMessage = "DELETED: :keyIndexSubject: " & keyIndexSubject & " :keyIndexBody: " & keyIndexBody & " :delKey: " & delKey & " :Email: " & invalidEmail & " :GUID: " & matchGuid
'EventLog.WriteEntry("NECSInBox", strMessage)
Else
If Not keyIndexSubject Is String.Empty Or Not keyIndexBody Is String.Empty Then
'strMessage = "MOVED: :keyIndexSubject: " & keyIndexSubject & " :keyIndexBody: " & keyIndexBody & " :delKey: " & delKey & " :Email: " & invalidEmail & " :GUID: " & matchGuid
'EventLog.WriteEntry("NECSInBox", strMessage)
Dim parsedSubject = emailSubject.Replace("Undeliverable:", String.Empty).Trim
matchGuid = GetGuidFromSubject(parsedSubject)
MoveMailMessageForReview(olInbox, olItems, i)
End If
End If
End If
End If
Else
Dim key As String = GetKeyName(String.Empty, keyIndexBody)
Dim invalidEmail As String = RegExValue(report.Body, profile.GetInboxEmailRegExPattern).Trim
Dim matchGuid As String = RegExValue(report.Body, profile.GetInboxGuidRegExPattern).Trim
Dim subject As String = RegExValue(report.Body, profile.GetSubjectFromMessageRegExPattern).Trim
If CheckIfSubscriberOnly(invalidEmail) Then
DeleteSubscriberEmail(invalidEmail)
olItems.Item(i).Delete()
Else
If matchGuid = String.Empty And Not subject = String.Empty Then
matchGuid = GetGuidFromSubject(subject)
End If
If Not matchGuid = String.Empty And Not subject = String.Empty Then
SaveReturnedEmails(matchGuid, invalidEmail, subject, report.Body, -1)
olItems.Item(i).Delete()
Else
MoveMailMessageForReview(olInbox, olItems, i)
End If
End If
End If
ElseIf TypeOf olItems.Item(i) Is Outlook.MailItem Then
If Not keyIndexBody Is String.Empty Or Not keyIndexSubject Is String.Empty Then
Dim mail As Outlook.MailItem = olItems.Item(i)
Dim invalidEmail As String = RegExValue(mail.Body, profile.GetInboxEmailRegExPattern)
Dim matchGuid As String = RegExValue(mail.Body, profile.GetInboxGuidRegExPattern)
If CheckIfSubscriberOnly(invalidEmail) Then
DeleteSubscriberEmail(invalidEmail)
olItems.Item(i).Delete()
Else
If matchGuid Is String.Empty Or invalidEmail Is String.Empty Then
Dim attachCount As Integer = mail.Attachments.Count
Dim x As Integer = 1
If attachCount > 0 Then
Do While (x < attachCount + 1) Or (keyIndexSubject Is String.Empty And keyIndexBody Is String.Empty)
Dim mailAttach As Outlook.Attachment = mail.Attachments.Item(x)
If matchGuid Is String.Empty And mailAttach.FileName.EndsWith("msg") Then
matchGuid = GetGuidFromSubject(mailAttach.FileName)
Else
mailAttach.SaveAsFile("c:\" & mailAttach.FileName)
Dim attachBody = GetMessageBody(GetFileAttachmentName(x), err)
If invalidEmail Is String.Empty Then
invalidEmail = RegExValue(attachBody, profile.GetInboxEmailRegExPattern)
End If
If matchGuid Is String.Empty Then
matchGuid = RegExValue(attachBody, profile.GetInboxGuidRegExPattern)
End If
File.Delete("c:\" & mailAttach.FileName)
x += 1
End If
Loop
Else
'No Attachments.... No guid.... look for subject in text of email.
Dim newSubject As String = RegExValue(mail.Body, profile.GetSubjectFromMessageRegExPattern)
If Not newSubject Is String.Empty Then
matchGuid = GetGuidFromSubject(newSubject)
End If
End If
End If
End If
If Not matchGuid Is String.Empty And Not invalidEmail Is String.Empty Then
Dim parsedSubject = emailSubject.Replace("Undeliverable:", String.Empty).Trim
SaveReturnedEmails(matchGuid, invalidEmail, parsedSubject, receiptBody, -1)
olItems.Item(i).Delete()
'strMessage = "DELETED: :keyIndexSubject: " & keyIndexSubject & " :keyIndexBody: " & keyIndexBody & " :delKey: " & delKey & " :Email: " & invalidEmail & " :GUID: " & matchGuid
'EventLog.WriteEntry("NECSInBox", strMessage)
Else
If Not keyIndexSubject Is String.Empty Or Not keyIndexBody Is String.Empty Then
'strMessage = "MOVED: :keyIndexSubject: " & keyIndexSubject & " :keyIndexBody: " & keyIndexBody & " :delKey: " & delKey & " :Email: " & invalidEmail & " :GUID: " & matchGuid
'EventLog.WriteEntry("NECSInBox", strMessage)
MoveMailMessageForReview(olInbox, olItems, i)
End If
End If
End If
Else
MoveMailMessageForReview(olInbox, olItems, i)
End If
Else
'delete message with no processing
olItems.Item(i).Delete()
End If
Next i
Catch ex As Exception
WriteToEventLog(ex.ToString)
Finally
olNS.Logoff()
olApp = Nothing : olNS = Nothing
olItems = Nothing : olInbox = Nothing
If profile.WriteServiceTotalUndeliveredEmailsToLog Then
WriteToEventLog(profile.GetTotalUndeliveredEmailsMessage.Replace("?", cnt))
End If
End Try
Else
If profile.WriteServicePauseMessageToLog Then
WriteToEventLog(profile.GetServicePauseMessage)
End If
End If
Catch outEx As Exception
WriteToEventLog(outEx.ToString)
End Try
End Sub