Imports System.Net.Mail
Imports System.Net
Imports System.Net.Mail.SmtpClient

Public Class ClassBWExportEmail
    Private Message As MailMessage = New MailMessage()
    ' // Anlage
    Private Attach As Attachment
    ' // Authenticated
    Private SmtpUser As New NetworkCredential()

    ' // Versenden über SMTPClient
    Private Smtp As New SmtpClient()
    ' // Absender
    Public Function Absender(ByVal Address As String, ByVal DisplayName As String) As String
        Message.From = New MailAddress(Address, DisplayName)
        Return Address & " (" & DisplayName & ")"
    End Function
    ' // Empfänger
    Public Function Empfänger(ByVal Address As String, ByVal DisplayName As String) As String
        Message.To.Add(New MailAddress(Address, DisplayName))
        Return Address & " (" & DisplayName & ")"
    End Function
    Public Function WeitereEmpfänger(ByVal Addresses As String) As String
        Dim sText As String = ""
        Message.CC.Add(Addresses)
        For iIndex As Integer = 0 To Message.CC.Count - 1
            sText &= Message.CC(iIndex).Address & "; "
        Next
        If sText.EndsWith("; ") Then
            sText = sText.Substring(0, sText.Length - 2)
        End If
        Return sText
    End Function

    ' // Betreff & Nachricht
    Public Function Nachricht(ByVal Betreff As String, ByVal Text As String) As MailMessage
        Message.Subject = Betreff
        Message.Body = Text
        Return Message
    End Function

    ' // Einstellungen der Nachricht (Als HTML oder nicht & Priorität der Nachricht)
    Public Function Nachricht_Einstellungen(ByVal IsTextHTML As Boolean, ByVal Priorität As  _
        MailPriority) As MailMessage
        Message.IsBodyHtml = IsTextHTML
        Message.Priority = Priorität
        Return Message
    End Function
    ' // Anlage Einzeln
    Public Sub Anlage(ByVal Pfad As String)
        Attach = New System.Net.Mail.Attachment(Pfad)
        Message.Attachments.Add(Attach)
    End Sub
    ' // Getestet
    Public Function Senden_MitAuthenzierung(ByVal Kontoname As String, ByVal Password As _
        String, ByVal SmtpAddress As String, Optional ByVal test As Boolean = False) As Boolean
        ' // Authenzierung
        SmtpUser.UserName = Kontoname
        SmtpUser.Password = Password
        ' -- Send Message
        Smtp.UseDefaultCredentials = False
        Smtp.Credentials = SmtpUser
        Smtp.Host = SmtpAddress
        Smtp.DeliveryMethod = SmtpDeliveryMethod.Network
        Try
            Smtp.Send(Message)

            If test = True Then
                MsgBox("Email erfolgreich versendet!", MsgBoxStyle.Information, "Erfolgsmeldung:")
            Else
                ' ClassLogger.Add(" => Erfolgsmeldung: Email wurde ERFOLGREICH VERSENDET ", True)
            End If
            Return True
        Catch ex As Exception
            If test = True Then
                MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler in Send_EMail")
            End If
            ClassLogger.Add("ACHTUNG_ FEHLERAUSNAHME", "Fehler in Send_EMail.", ex)
            Return False
        End Try
    End Function
End Class
