Imports System.Net.Mail
Public Class clsEmail
    Private Shared MailAktiv As Boolean = False
    Private Shared MailSSL As Boolean = False

    'Public Shared Function Init()
    '        Try
    '        Dim DT As DataTable = clsDatatabase.Return_Datatable("select * from TBDD_EMAIL_ACCOUNT where ACTIVE = 1")
    '        If DT.Rows.Count = 1 Then
    '                For Each row As DataRow In DT.Rows

    '                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
    '            clsLogger.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, MailEmpfaenger As String, MailFrom As String, MAilSMTP As String, MailUser As String, MailUser_PW As String, Optional attment As String = "", Optional test As Boolean = False)
        '#### 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) */
                If attment <> String.Empty Then
                    If System.IO.File.Exists(attment) Then
                        Dim Attachment As Attachment = New Attachment(attment)
                        message.Attachments.Add(Attachment)
                    End If
                End If

                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
                clsLogger.Add("==> Email erfolgreich an " & _mailempfaenger & " versendet!", False)
                clsLogger.Add("==> Text: " & vBody, False)
                clsLogger.Add("", False)
                '*Send the message */
                emailClient.Send(message)
                If test = True Then
                    MsgBox("The testmail was send successfully", MsgBoxStyle.Information)
                End If
                Return True
            Next
        Catch ex As Exception
            clsLogger.Add(ex.Message, True, "cls.SendEmail")
            If test = True Then
                MsgBox("Unexpected error in Send Testmail: " & ex.Message, MsgBoxStyle.Critical)
            End If
            Return False
        End Try
    End Function


End Class
