316 lines
14 KiB
VB.net
316 lines
14 KiB
VB.net
Imports System
|
|
Imports Independentsoft.Email
|
|
Imports Independentsoft.Email.Pop3
|
|
Imports Independentsoft.Email.Smtp
|
|
Imports Independentsoft.Email.Mime
|
|
Imports Independentsoft.Email.Imap
|
|
|
|
Imports DigitalData.Modules.Logging
|
|
Public Class Email
|
|
Private _logger As DigitalData.Modules.Logging.Logger
|
|
Private _logConfig As LogConfig
|
|
Public Sub New(LogConfig As LogConfig)
|
|
_logger = LogConfig.GetLogger()
|
|
_logConfig = LogConfig
|
|
|
|
End Sub
|
|
Public Function IMAP_COLLECT(INBOXNAME As String, MYMAIL_SERVER As String, MYMAIL_PORT As Integer, MYMAIL_USER As String, MYMAIL_USER_PW As String)
|
|
Try
|
|
Dim oMAIL_LIST As New ArrayList()
|
|
_logger.Info(String.Format("Working on IMAP_COLLECT..."))
|
|
Dim oClient As New ImapClient(MYMAIL_SERVER, MYMAIL_PORT)
|
|
oClient.Connect()
|
|
oClient.Login(MYMAIL_USER, MYMAIL_USER_PW)
|
|
oClient.SelectFolder("Inbox")
|
|
Dim oEnvelopes As Envelope() = oClient.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
|
|
_logger.Info($"Working on email: UniqueID: {oEnvelopes(i).UniqueID} - Subject:{oEnvelopes(i).Subject} - Date {oEnvelopes(i).Date.ToString}")
|
|
Dim message As Mime.Message = oClient.GetMessage(oEnvelopes(i).UniqueID)
|
|
If Not IsNothing(message) Then
|
|
oMAIL_LIST.Add(message)
|
|
End If
|
|
'End If
|
|
End If
|
|
Next
|
|
|
|
oClient.Disconnect()
|
|
_logger.Debug("IMAP_COLLECT finished!")
|
|
Return oMAIL_LIST
|
|
|
|
Catch ex As Exception
|
|
_logger.Error(ex, "Unexpected Error in IMAP COLLECT:")
|
|
Return Nothing
|
|
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 oClient As New ImapClient(MYMAIL_SERVER, MYMAIL_PORT)
|
|
oClient.Connect()
|
|
oClient.Login(MYMAIL_USER, MYMAIL_USER_PW)
|
|
oClient.SelectFolder(INBOXNAME)
|
|
Dim oEnvelopes As Envelope() = oClient.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 = oClient.GetMessage(oEnvelopes(i).UniqueID)
|
|
End If
|
|
Next
|
|
oClient.Disconnect()
|
|
_logger.Info("TEST_IMAP_COLLECT finished!")
|
|
Return True
|
|
Catch ex As Exception
|
|
_logger.Error(ex, "Unexpected Error in TEST_IMAP_COLLECT:")
|
|
Return False
|
|
End Try
|
|
End Function
|
|
Public Function POP3_COLLECT(MYMAIL_SERVER As String, MYMAIL_PORT As Integer, MYMAIL_USER As String, MYMAIL_USER_PW As String)
|
|
Try
|
|
Dim oMAIL_LIST As New ArrayList()
|
|
_logger.Debug(String.Format("Working on POP3_COLLECT....."))
|
|
Dim oClient As New Pop3Client(MYMAIL_SERVER, MYMAIL_PORT)
|
|
|
|
oClient.ValidateRemoteCertificate = False
|
|
oClient.Connect()
|
|
_logger.Debug(String.Format("..connected!"))
|
|
oClient.Login(MYMAIL_USER, MYMAIL_USER_PW)
|
|
|
|
Dim oMessageInfo As MessageInfo() = oClient.List()
|
|
|
|
For i As Integer = 0 To oMessageInfo.Length - 1
|
|
Dim message As Message = oClient.GetMessage(oMessageInfo(i).Index)
|
|
oMAIL_LIST.Add(message)
|
|
Try
|
|
_logger.Debug(String.Format("Message [{0}] added", message.Subject))
|
|
Catch ex As Exception
|
|
|
|
End Try
|
|
Next
|
|
|
|
oClient.Disconnect()
|
|
_logger.Debug(String.Format(" POP3_COLLECT finished!"))
|
|
Return oMAIL_LIST
|
|
Catch ex As Exception
|
|
_logger.Error(ex)
|
|
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
Public Function TEST_POP3_COLLECT(MYMAIL_SERVER As String, MYMAIL_PORT As Integer, MYMAIL_USER As String, MYMAIL_USER_PW As String) As Boolean
|
|
Try
|
|
_logger.Debug(String.Format("Working on TEST_POP3_COLLECT..."))
|
|
Dim oClient As New Pop3Client(MYMAIL_SERVER, MYMAIL_PORT)
|
|
|
|
oClient.ValidateRemoteCertificate = False
|
|
oClient.Connect()
|
|
_logger.Debug(String.Format("..connected!"))
|
|
oClient.Login(MYMAIL_USER, MYMAIL_USER_PW)
|
|
|
|
Dim messageInfo As MessageInfo() = oClient.List()
|
|
|
|
For i As Integer = 0 To messageInfo.Length - 1
|
|
Dim message As Message = oClient.GetMessage(messageInfo(i).Index)
|
|
MsgBox(String.Format("Message [{0}] added", message.Subject))
|
|
|
|
Next
|
|
|
|
oClient.Disconnect()
|
|
MsgBox(String.Format("TEST_POP3_COLLECT finished!"))
|
|
Return True
|
|
Catch ex As Exception
|
|
_logger.Error(ex)
|
|
|
|
Return False
|
|
End Try
|
|
End Function
|
|
Public Function NewEmail(mailto As String, mailSubject As String, mailBody As String,
|
|
mailfrom As String, mailsmtp As String, mailport As Integer, mailUser As String, mailPW As String,
|
|
AUTH_TYPE As String, SENDER_INSTANCE As String, Optional attment As String = "")
|
|
Try
|
|
_logger.Debug($"in NewEmail..")
|
|
Dim oEmpfaenger As String()
|
|
If mailto.Contains(";") Then
|
|
oEmpfaenger = mailto.Split(";")
|
|
Else
|
|
ReDim Preserve oEmpfaenger(0)
|
|
oEmpfaenger(0) = mailto
|
|
End If
|
|
Dim oError As Boolean = False
|
|
'Für jeden Empfänger eine Neue Mail erzeugen
|
|
For Each oMailempfaenger As String In oEmpfaenger
|
|
_logger.Debug($"Working on email for {oMailempfaenger}..")
|
|
Try
|
|
Dim oMessage As New Message()
|
|
oMessage.From = New Mailbox(mailfrom, mailfrom)
|
|
oMessage.[To].Add(New Mailbox(oMailempfaenger))
|
|
oMessage.Subject = mailSubject
|
|
_logger.Debug($"Message created..")
|
|
Dim oTextBodyPart As New BodyPart()
|
|
oTextBodyPart.ContentType = New ContentType("text", "html", "utf-8")
|
|
|
|
oTextBodyPart.ContentTransferEncoding = ContentTransferEncoding.QuotedPrintable
|
|
Dim formattedBody = "<font face=""Tahoma"">" & mailBody & "<br><br>>> Service: " & SENDER_INSTANCE & "<br>" &
|
|
">> DateTime: " & My.Computer.Clock.LocalTime.ToShortDateString.ToString("dd.MM.yyyy") & " " &
|
|
My.Computer.Clock.LocalTime.ToLongTimeString.ToString("H:mm:ss") & "</font>"
|
|
Dim thisDate1 As Date = #6/10/2011#
|
|
Console.WriteLine("Today is " + thisDate1.ToString("MMMM dd, yyyy") + ".")
|
|
oTextBodyPart.Body = formattedBody
|
|
oMessage.BodyParts.Add(oTextBodyPart)
|
|
If attment <> String.Empty Then
|
|
If System.IO.File.Exists(attment) Then
|
|
Dim attachment1 As New Attachment(attment)
|
|
If attment.ToLower.EndsWith("pdf") Then
|
|
attachment1.ContentType = New ContentType("application", "pdf")
|
|
ElseIf attment.ToLower.EndsWith("jpg") Then
|
|
attachment1.ContentType = New ContentType("application", "jpg")
|
|
ElseIf attment.ToLower.EndsWith("docx") Then
|
|
attachment1.ContentType = New ContentType("application", "MS-word")
|
|
End If
|
|
oMessage.BodyParts.Add(attachment1)
|
|
Else
|
|
_logger.Warn($"Attachment {attment.ToString} is not existing!")
|
|
End If
|
|
End If
|
|
Dim oEmailCient As SmtpClient
|
|
Try
|
|
oEmailCient = New SmtpClient(mailsmtp, mailport)
|
|
Catch ex As Exception
|
|
_logger.Warn("clsEmail.Create oClient: " & ex.Message)
|
|
oError = True
|
|
Continue For
|
|
End Try
|
|
Try
|
|
oEmailCient.Connect()
|
|
Catch ex As Exception
|
|
_logger.Warn("clsEmail.oClient.Connect: " & ex.Message)
|
|
oError = True
|
|
' Continue For
|
|
End Try
|
|
_logger.Debug("Connected to oClient!")
|
|
If AUTH_TYPE = "SSL" Then
|
|
oEmailCient.EnableSsl = True
|
|
'oClient.ValidateRemoteCertificate = True
|
|
_logger.Debug("Authentification via SSL.")
|
|
ElseIf AUTH_TYPE = "TLS" Then
|
|
' oClient.ValidateRemoteCertificate = False
|
|
oEmailCient.StartTls()
|
|
oEmailCient.EnableSsl = False
|
|
_logger.Info("Authentification via TLS. SSL disabled")
|
|
Else
|
|
oEmailCient.EnableSsl = False
|
|
_logger.Info("Authentification NONE. SSL disabled")
|
|
End If
|
|
Try
|
|
oEmailCient.Connect()
|
|
Catch ex As Exception
|
|
_logger.Warn("clsEmail.oClient.Connect: " & ex.Message)
|
|
oError = True
|
|
' Continue For
|
|
End Try
|
|
Try
|
|
If mailsmtp.Contains("office365.com") Then
|
|
oEmailCient.Login(mailUser, mailPW, AuthenticationType.None)
|
|
Else
|
|
oEmailCient.Login(mailUser, mailPW)
|
|
End If
|
|
|
|
_logger.Debug("Logged in!")
|
|
Catch ex As Exception
|
|
Try
|
|
If mailsmtp.Contains("office365.com") Then
|
|
oEmailCient.Login(mailUser, mailPW, AuthenticationType.Login)
|
|
Else
|
|
oEmailCient.Login(mailUser, mailPW, AuthenticationType.Anonymous)
|
|
End If
|
|
|
|
Catch ex1 As Exception
|
|
Try
|
|
oEmailCient.Login(mailUser, mailPW, AuthenticationType.Login)
|
|
Catch ex2 As Exception
|
|
_logger.Warn("clsEmail.oClient.Login: " & ex.Message)
|
|
oError = True
|
|
oEmailCient.Disconnect()
|
|
Continue For
|
|
End Try
|
|
End Try
|
|
End Try
|
|
Try
|
|
oEmailCient.Send(oMessage)
|
|
_logger.Info("Message to " & oMailempfaenger & " has been send.")
|
|
oError = False
|
|
Catch ex As Exception
|
|
_logger.Warn("NewEmail.Send: " & ex.Message)
|
|
oError = True
|
|
oEmailCient.Disconnect()
|
|
Continue For
|
|
End Try
|
|
oEmailCient.Disconnect()
|
|
|
|
Catch ex As Exception
|
|
_logger.Error(ex)
|
|
oError = True
|
|
End Try
|
|
Next
|
|
|
|
If oError = True Then
|
|
Return False
|
|
Else
|
|
Return True
|
|
End If
|
|
Catch ex As Exception
|
|
_logger.Error(ex)
|
|
Return False
|
|
End Try
|
|
End Function
|
|
Public Function DELETE_EMAIL(POLLTYPE As String, msgid As String, MYMAIL_SERVER As String, MYMAIL_PORT As Integer, MYMAIL_USER As String, MYMAIL_USER_PW As String)
|
|
Try
|
|
If POLLTYPE = "POP" Then
|
|
Dim oClient As New Pop3Client(MYMAIL_SERVER, MYMAIL_PORT)
|
|
|
|
oClient.ValidateRemoteCertificate = False
|
|
oClient.Connect()
|
|
oClient.Login(MYMAIL_USER, MYMAIL_USER_PW)
|
|
|
|
Dim oMessageInfo As MessageInfo() = oClient.List()
|
|
|
|
For i As Integer = 0 To oMessageInfo.Length - 1
|
|
Dim message As Message = oClient.GetMessage(oMessageInfo(i).Index)
|
|
If message.MessageID = msgid Then
|
|
oClient.Delete(oMessageInfo(i).Index)
|
|
_logger.Info(String.Format("Message [{0}] was deleted!", message.Subject))
|
|
Exit For
|
|
End If
|
|
Next
|
|
oClient.Disconnect()
|
|
Return True
|
|
ElseIf POLLTYPE = "IMAP" Then
|
|
Dim oIMAPClient As New ImapClient(MYMAIL_SERVER, MYMAIL_PORT)
|
|
|
|
oIMAPClient.ValidateRemoteCertificate = False
|
|
oIMAPClient.Connect()
|
|
oIMAPClient.Login(MYMAIL_USER, MYMAIL_USER_PW)
|
|
|
|
oIMAPClient.SelectFolder("Inbox")
|
|
Dim envelopes As Envelope() = oIMAPClient.ListMessages()
|
|
For i As Integer = 0 To envelopes.Length - 1
|
|
If envelopes(i).MessageID = msgid Then
|
|
oIMAPClient.Delete(envelopes(i).UniqueID) 'mark as deleted
|
|
End If
|
|
Next
|
|
oIMAPClient.Expunge() 'delete messages marked as deleted
|
|
oIMAPClient.Disconnect()
|
|
Return True
|
|
End If
|
|
Catch ex As Exception
|
|
_logger.Error(ex)
|
|
'clsLogger.Add("Unexpected Error in DELETE_EMAIL: " & ex.Message)
|
|
Return False
|
|
End Try
|
|
End Function
|
|
End Class
|