Digital Data - Marlon Schreiber 470430e9c4 IMAP Biblios
2019-04-09 10:17:11 +02:00

137 lines
6.4 KiB
VB.net

Imports Independentsoft.Email
Imports Independentsoft.Email.Imap
Imports Independentsoft.Email.Mime
Imports DigitalData.EMLProfiler.ClassCurrent
Imports DigitalData.Modules.Logging
Imports AE
Public Class clsEmailIMAP
Private Shared Logger As DigitalData.Modules.Logging.Logger
Sub New(LogConf As LogConfig)
Logger = LogConf.GetLogger
End Sub
'Private Shared Sub OnWriteLog(ByVal sender As Object, ByVal e As WriteLogEventArgs)
' Logger.Info(e.Log)
'End Sub
Public Function IMAP_COLLECT()
Try
Logger.Info(String.Format("Working on IMAP_COLLECT....."))
Logger.Debug(String.Format("Working on IMAP_COLLECT....."))
Dim oClient As New Independentsoft.Email.Imap.ImapClient(MAIL_SERVER, MAIL_PORT)
oClient.ValidateRemoteCertificate = False
oClient.Connect()
oClient.Login(MAIL_USER, MAIL_USER_PW)
oClient.SelectFolder("Inbox")
Dim oEnvelopes As Independentsoft.Email.Imap.Envelope() = oClient.ListMessages()
Dim oCount As Integer = 0
For i As Integer = 0 To oEnvelopes.Length - 1
If Not IsNothing(oEnvelopes(i).Subject) Then
'If envelopes(i).Subject.ToString.ToUpper.Contains("[PROCESSMANAGER]") Or envelopes(i).Subject.ToString.ToUpper.Contains("[ADDI]") Then
Logger.Info($"Working on email: UniqueID: {oEnvelopes(i).UniqueID} - Subject:{oEnvelopes(i).Subject} - Date {oEnvelopes(i).Date.ToString}")
Dim oMessage As Mime.Message = oClient.GetMessage(oEnvelopes(i).UniqueID)
If Not IsNothing(oMessage) Then
oCount += 1
MAIL_LIST.Add(oMessage)
End If
'End If
End If
Next
oClient.Disconnect()
Logger.Debug($"{oCount.ToString} messages will be worked..")
Logger.Debug("IMAP_COLLECT finished!")
Return True
Catch ex As Exception
Logger.Error(ex, "Unexpected Error in IMAP COLLECT:")
Return False
End Try
End Function
Public Function TEST_IMAP_COLLECT(INBOXNAME As String, MYMAIL_SERVER As String, MYMAIL_PORT As Integer, MYMAIL_USER As String, MYMAIL_USER_PW As String)
Try
Logger.Info(String.Format("Working on TEST_IMAP_COLLECT....."))
Dim oLogPath = System.IO.Path.Combine(My.Application.Info.DirectoryPath, "Log\logindependentSoft.txt")
Logger.Debug($"IsoftLog: {oLogPath}...")
Dim oindependentLogger As New Independentsoft.Email.Logger(oLogPath)
Dim oImapClient As New Independentsoft.Email.Imap.ImapClient(MYMAIL_SERVER, MYMAIL_PORT)
oImapClient.Logger = oindependentLogger
oImapClient.ValidateRemoteCertificate = False
oImapClient.Connect()
Logger.Debug($"oImapClient connected...")
Try
oImapClient.Login(MAIL_USER, MAIL_USER_PW, AuthenticationType.Login)
Catch ex As Exception
MsgBox($"Unexpected error in (oImapClient.Login): {ex.Message}")
Logger.Info($"Unexpected error in oImapClient.Login - User: [{MYMAIL_USER}] PW: [{MYMAIL_USER_PW}]")
Logger.Warn(ex.StackTrace.ToString)
Logger.Warn(ex.Message)
oImapClient.Logger.Close()
Logger.Error(ex)
Return False
End Try
oImapClient.SelectFolder(INBOXNAME)
Dim oEnvelopes As Independentsoft.Email.Imap.Envelope() = oImapClient.ListMessages()
For i As Integer = 0 To oEnvelopes.Length - 1
If Not IsNothing(oEnvelopes(i).Subject) Then
'If envelopes(i).Subject.ToString.ToUpper.Contains("[PROCESSMANAGER]") Or envelopes(i).Subject.ToString.ToUpper.Contains("[ADDI]") Then
MsgBox($"Working on email: UniqueID: {oEnvelopes(i).UniqueID} - Subject:{oEnvelopes(i).Subject} - Date {oEnvelopes(i).Date.ToString}")
Dim message As Mime.Message = oImapClient.GetMessage(oEnvelopes(i).UniqueID)
End If
Next
oImapClient.Disconnect()
oImapClient.Logger.Close()
Logger.Info("TEST_IMAP_COLLECT finished!")
Return True
Catch ex As Exception
Logger.Info($"Unexpected error in TEST_IMAP_COLLECT - User: [{MYMAIL_USER}] PW: [{MYMAIL_USER_PW}]")
MsgBox($"Unexpected error in TEST_IMAP_COLLECT: {ex.Message}")
Logger.Error(ex, "Unexpected Error in TEST_IMAP_COLLECT:")
Return False
End Try
End Function
Public Function TEST_AEIMAP_COLLECT(INBOXNAME As String, MYMAIL_SERVER As String, MYMAIL_PORT As Integer, MYMAIL_USER As String, MYMAIL_USER_PW As String)
Try
Logger.Info(String.Format("Working on TEST_AEIMAP_COLLECT....."))
'Dim oLogPath = System.IO.Path.Combine(My.Application.Info.DirectoryPath, "Log\logindependentSoft.txt")
'Logger.Debug($"IsoftLog: {oLogPath}...")
'Dim oindependentLogger As New Independentsoft.Email.Logger(oLogPath)
' Dim sslProtocol As System.Security.Authentication.SslProtocols = Security.Authentication.SslProtocols.Default
Dim oImapClient As New AE.Net.Mail.ImapClient(MYMAIL_SERVER, MYMAIL_USER, MYMAIL_USER_PW, AE.Net.Mail.AuthMethods.Login, MYMAIL_PORT)
'oImapClient.Logger = oindependentLogger
'oImapClient.ValidateRemoteCertificate = False
Try
If (oImapClient.IsConnected) Then
Logger.Debug($"oImapClient Login successfull...")
Else
Logger.Info($"oImapClient NOT connected...")
Return False
End If
Catch ex As Exception
Logger.Warn(ex.Message)
Logger.Error(ex)
Return False
End Try
oImapClient.Disconnect()
Logger.Info("TEST_IMAP_COLLECT finished!")
Return True
Catch ex As Exception
Logger.Info($"Unexpected error in TEST_IMAP_COLLECT - User: [{MYMAIL_USER}] PW: [{MYMAIL_USER_PW}]")
Logger.Error(ex, "Unexpected Error in TEST_IMAP_COLLECT:")
MsgBox($"Unexpected error in TEST_IMAP_COLLECT: {ex.Message}")
Return False
End Try
End Function
End Class