Imports System.Net.Mail
Public Class clsEmail
    Private Shared MailAktiv As Boolean = False
    Private Shared MailSSL As Boolean = False
    Private Shared MailEmpfaenger, MailFrom, MAilSMTP, MailUser, MailUser_PW As String
    Public Shared Function Init()
        Try
            Dim DT As DataTable = clsSQLITE.Return_Datatable("select * from TBKONFIGURATION where GUID = 1 and EMAIL_AKTIV = 1")
            If DT.Rows.Count = 1 Then
                For Each row As DataRow In DT.Rows
                    MailEmpfaenger = row.Item("EMAIL_EMP")
                    MailFrom = row.Item("EMAIL_ABS")
                    MAilSMTP = row.Item("EMAIL_SMTP")
                    MailSSL = row.Item("EMAIL_SSL")
                    MailUser = row.Item("EMAIL_USER")
                    MailUser_PW = row.Item("EMAIL_USER_PW")
                    MailAktiv = True
                    Exit For
                Next
            Else
                MailAktiv = False
            End If

            Return True
        Catch ex As Exception
            ClassLogger.Add(ex.Message, True, "clsEmail.Init")
            Return False
        End Try
    End Function
    Public Shared Function Send_EMail(ByVal MailBetreff As String, ByVal vBody As String)
        '#### E-MAIL NACHRICHT VERSENDEN
        Try
            Dim empfaenger As String()
            If MailEmpfaenger.Contains(";") Then
                empfaenger = MailEmpfaenger.Split(";")
            Else
                ReDim Preserve empfaenger(0)
                empfaenger(0) = MailEmpfaenger
            End If
            'Für jeden Empfänger eine Neue Mail erzeugen
            For Each _mailempfaenger As String In empfaenger
                'Neue Nachricht erzeugen: 
                Dim message As New MailMessage(MailFrom, _mailempfaenger, MailBetreff, _
                                                         "<font face=""Arial"">" & vBody & "<br>Maschine: " & Environment.MachineName & _
                                                         "<br>Domain-Name: " & Environment.UserDomainName & "<br>" & _
                                                         "<br>Gesendet am: " & My.Computer.Clock.LocalTime.ToShortDateString & "-" & _
                                                         My.Computer.Clock.LocalTime.ToLongTimeString & "</font>")
                ' create and add the attachment(s) */
                ' Dim Attachment As Attachment = New Attachment("E:\test\Frachkostenreport.txt")
                'message.Attachments.Add(Attachment)
                With message
                    .IsBodyHtml = True
                End With

                'Einen SMTP Client erzeugen und Anmeldungsinformationen hinterlegen
                Dim emailClient As New SmtpClient(MAilSMTP)
                emailClient.EnableSsl = MailSSL
                'Email mit Authentifizierung
                Dim SMTPUserInfo As New System.Net.NetworkCredential(MailUser, MailUser_PW) ', My.Settings.vDomain)
                emailClient.UseDefaultCredentials = False
                emailClient.Credentials = SMTPUserInfo
                emailClient.Port = 25
                ClassLogger.Add("==> Fehler Email erfolgreich an " & _mailempfaenger & " versendet!", False)
                ClassLogger.Add("==> Text: " & vBody, False)
                ClassLogger.Add("", False)
                '*Send the message */
                emailClient.Send(message)
                Return True
            Next
        Catch ex As Exception
            ClassLogger.Add(ex.Message, True, "cls.SendEmail")
            Return False
        End Try
    End Function
End Class
