
Imports System
Imports Independentsoft.Email
Imports Independentsoft.Email.Smtp
Imports Independentsoft.Email.Mime
Imports DigitalData.Modules.Logging
Imports System.Net.Mail
Imports System.Net
Imports System.Net.Mime

Public Class clsEmail
    Dim Logger As DigitalData.Modules.Logging.Logger
    Private Shared MailAktiv As Boolean = False
    Sub New(MyLogger As LogConfig)
        Logger = MyLogger.GetLogger()
    End Sub
    Public Function Email_Send(ByVal pMailSubject As String, ByVal pMailBody As String, pMailto As String,
                                                      pMailfrom As String, pMailSmtp As String, pMailport As Integer, pMailUser As String, pMailPW As String,
                                                      pAUTH_TYPE As String, Optional pAttment As String = "", Optional pTest As Boolean = False)
        Try
            Dim oError As Boolean = False
            Dim oReceipiants As String()
            If pMailto.Contains(";") Then
                oReceipiants = pMailto.Split(";")
            Else
                ReDim Preserve oReceipiants(0)
                oReceipiants(0) = pMailto
            End If
            For Each oMailReceipiant As String In oReceipiants
                Dim sClient = New Net.Mail.SmtpClient(pMailSmtp)
                Dim mymesssage As New MailMessage
                sClient.Port = pMailport
                If pAUTH_TYPE = "SSL" Then
                    sClient.EnableSsl = True
                Else
                    sClient.EnableSsl = False
                End If

                sClient.Credentials = New NetworkCredential(pMailUser, pMailPW)
                sClient.UseDefaultCredentials = False
                If pTest = True Then
                    mymesssage.Body = $"This is the body (text will be replaced within profile)! <br> mailsmtp: {pMailSmtp} <br> mailport: {pMailport} <br> mailUser: {pMailUser} <br> mailPW: XXXX <br> AUTH_TYPE: {pAUTH_TYPE}"
                Else
                    mymesssage.Body = pMailBody
                End If

                'mymesssage.IsBodyHtml = True
                Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(mymesssage.Body)
                htmlView.ContentType = New System.Net.Mime.ContentType("text/html")
                mymesssage.AlternateViews.Add(htmlView)
                pAttment = pAttment.Replace("W:\", "\\windream\objects\")
                If pAttment <> String.Empty Then
                    If System.IO.File.Exists(pAttment) Then
                        Logger.Debug($"working on attachment {pAttment.ToString}...")
                        Dim oAttachment As New System.Net.Mail.Attachment(pAttment)
                        mymesssage.Attachments.Add(oAttachment)
                    Else
                        Logger.Warn($"Attachment {pAttment.ToString} is not existing - Mail won't be send!")
                        oError = True

                    End If
                End If


                mymesssage.From = New MailAddress(pMailfrom)
                mymesssage.Subject = pMailSubject
                mymesssage.To.Add(New MailAddress(oMailReceipiant))
                sClient.Send(mymesssage)
                Logger.Info($"Email successfully send to: [{oMailReceipiant}]!")
                Logger.Debug($"Email successfully send to: [{oMailReceipiant}]!")
            Next
            If oError = False Then
                Return True
            Else
                Return False
            End If

        Catch ex As Exception
            Logger.Error(ex)
            Return False
        End Try

    End Function
End Class
