Monorepo/Message/Email.vb
Digital Data - Marlon Schreiber 71fdc188c2 MS Service ZuGferd und Begin GUI
2018-12-17 18:30:17 +01:00

149 lines
6.7 KiB
VB.net

Imports System
Imports Independentsoft.Email
Imports Independentsoft.Email.Smtp
Imports Independentsoft.Email.Mime
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 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, 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
oTextBodyPart.Body = mailBody
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 Client: " & ex.Message)
oError = True
Continue For
End Try
Try
oEmailCient.Connect()
Catch ex As Exception
_logger.Warn("clsEmail.Client.Connect: " & ex.Message)
oError = True
' Continue For
End Try
_logger.Info("Connected to Client!")
If AUTH_TYPE = "SSL" Then
oEmailCient.EnableSsl = True
'client.ValidateRemoteCertificate = True
_logger.Info("Authentification via SSL.")
ElseIf AUTH_TYPE = "TLS" Then
' client.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.Client.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.Info("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.Client.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("clsEmail.Client.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
End Class