MS: Emailservice und Logging

This commit is contained in:
2020-01-06 13:00:25 +01:00
parent bc18b693bb
commit f20d0e5edd
6 changed files with 272 additions and 21 deletions

View File

@@ -360,7 +360,142 @@ Public Class Email
End Try
End Function
Public Function New_EmailISoft(ByVal mailSubject As String, ByVal mailBody As String, mailto 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
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
_logger.Debug($"in Email_Send_Independentsoft..")
Dim empfaenger As String()
If mailto.Contains(";") Then
empfaenger = mailto.Split(";")
Else
ReDim Preserve empfaenger(0)
empfaenger(0) = mailto
End If
Dim _error As Boolean = False
'Für jeden Empfänger eine Neue Mail erzeugen
For Each _mailempfaenger As String In empfaenger
_logger.Debug($"Working on email for {_mailempfaenger}..")
Try
Dim message As New Message()
message.From = New Mailbox(mailfrom, mailfrom)
message.[To].Add(New Mailbox(_mailempfaenger))
message.Subject = mailSubject
_logger.Debug($"Message created..")
Dim textBodyPart As New BodyPart()
textBodyPart.ContentType = New ContentType("text", "html", "utf-8")
textBodyPart.ContentTransferEncoding = ContentTransferEncoding.QuotedPrintable
textBodyPart.Body = mailBody
message.BodyParts.Add(textBodyPart)
If attment <> String.Empty Then
If System.IO.File.Exists(attment) Then
Dim attachment1 As New Independentsoft.Email.Mime.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
message.BodyParts.Add(attachment1)
Else
_logger.Warn($"Attachment {attment.ToString} is not existing!")
End If
End If
Dim client As Independentsoft.Email.Smtp.SmtpClient
Try
client = New Independentsoft.Email.Smtp.SmtpClient(mailsmtp, mailport)
Catch ex As Exception
_logger.Warn("clsEmail.Create Client: " & ex.Message)
_error = True
Continue For
End Try
Try
client.Connect()
Catch ex As Exception
_logger.Warn("clsEmail.Client.Connect1: " & ex.Message)
_logger.Debug("Error in ClientConnect - but still trying to send")
_error = True
' Continue For
End Try
_logger.Debug("Connected to Client!")
If AUTH_TYPE = "SSL" Then
client.EnableSsl = True
'client.ValidateRemoteCertificate = True
_logger.Debug("Authentification via SSL.")
ElseIf AUTH_TYPE = "TLS" Then
' client.ValidateRemoteCertificate = False
client.StartTls()
client.EnableSsl = False
_logger.Debug("Authentification via TLS. SSL disabled")
Else
client.EnableSsl = False
_logger.Debug("Authentification NONE. SSL disabled")
End If
Try
client.Connect()
Catch ex As Exception
_logger.Warn("clsEmail.Client.Connect: " & ex.Message)
_error = True
' Continue For
End Try
Try
If mailsmtp.Contains("office365.com") Then
client.Login(mailUser, mailPW, AuthenticationType.None)
Else
client.Login(mailUser, mailPW)
End If
_logger.Debug("Logged in!")
Catch ex As Exception
Try
If mailsmtp.Contains("office365.com") Then
client.Login(mailUser, mailPW, AuthenticationType.Login)
Else
client.Login(mailUser, mailPW, AuthenticationType.Anonymous)
End If
Catch ex1 As Exception
Try
client.Login(mailUser, mailPW, AuthenticationType.Login)
Catch ex2 As Exception
_logger.Warn("clsEmail.Client.Login: " & ex.Message)
_error = True
client.Disconnect()
Continue For
End Try
End Try
End Try
Try
client.Send(message)
_logger.Info("Message to " & _mailempfaenger & " has been send.")
_error = False
Catch ex As Exception
_logger.Warn("clsEmail.Client.Send: " & ex.Message)
_error = True
client.Disconnect()
Continue For
End Try
client.Disconnect()
Catch ex As Exception
_logger.Error(ex)
_error = True
End Try
Next
If _error = 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