VB.Net – Mail per SMTP SSL versenden mit Anlagen und praktischer Parameter Structure

VB.Net – Mail per SMTP SSL versenden mit Anlagen und praktischer Parameter Structure

Mit dieser Funktion können Sie komfortabel Mails inkl. Anlagen über ihren SMTP Server versenden. Die Parameter kommen aus einer Structure die Sie beim Starten Ihrer Anwendung zum Beispiel mit einer Ini-Datei befüllen können.

So wird es gemacht:

    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
 
   Public Structure IniStructure
        Dim SMTP_HOST As String
        Dim SMTP_USER As String
        Dim SMTP_PW As String
        Dim SMTP_FROM As String
        Dim SMTP_SSL As Boolean
        Dim MAIL_TO_1 As String
        Dim MAIL_TO_2 As String
 
        Dim SQL_Kz As String
        Dim SQL_Pwd As String
        Dim SQL_DB As String
        Dim SQL_Server As String
 
        Dim ERR_Modulname As String
        Dim ERR_ProzedurName As String
        Dim ERR_Message As String
        Dim ERR_SILENT As Boolean
        Dim ERR_MAIL As Boolean
        Dim ERR_LOG As Boolean
 
        Dim APP_TITEL As String
        Dim APP_VERSION As String
        Dim APP_Path As String
        Dim APP_INI_DATEI As String
 
        Dim lngSqlUserId As Long
    End Structure
 
   Public Function MAIL_Send( _
        ByVal strTo As String, _
        ByVal strCC As String, _
        ByVal strBCC As String, _
        ByVal strFromDisplay As String, _
        ByVal strSubject As String, _
        ByVal strBody As String, _
        ByVal strAttachments() As String, _
        ByRef strErrMessage As String, _
        ByVal myStruc As IniStructure) As Boolean
        MAIL_Send = False
 
        '----------------------------------------
        Dim strProzedur As String = "MAIL_Send()"
        '----------------------------------------
 
        Try
 
            '------------------------------------
            Dim msgMail = New MailMessage()
            msgMail.To.Add(strTo)
            If strCC <> "" Then msgMail.CC.Add(strCC)
            If strBCC <> "" Then msgMail.Bcc.Add(strBCC)
            msgMail.From = New MailAddress(myStruc.SMTP_FROM, strFromDisplay)
            msgMail.Subject = strSubject
            msgMail.IsBodyHtml = False
            msgMail.Body = strBody
            '------------------------------------
 
            '------------------------------------
            Dim attachment As System.Net.Mail.Attachment
            For i = 0 To UBound(strAttachments)
                If Not IsNothing(strAttachments(i)) Then
                    attachment = New System.Net.Mail.Attachment(strAttachments(i))
                    msgMail.Attachments.Add(attachment)
                End If
            Next
            '------------------------------------
 
            '------------------------------------
            Dim smtp = New SmtpClient()
            smtp.Host = myStruc.SMTP_HOST
            smtp.Credentials = New Net.NetworkCredential(myStruc.SMTP_USER, myStruc.SMTP_PW)
            '------------------------------------
 
            '------------------------------------
            If myStruc.SMTP_SSL Then
                smtp.Port = 587
                smtp.EnableSsl = True
            End If
            '------------------------------------
 
            '------------------------------------
            smtp.Send(msgMail)
            '------------------------------------
 
            '------------------------------------
            MAIL_Send = True
            '------------------------------------
 
            'Catch ex2 As SmtpFailedRecipientException
            '    MsgBox(ex2.Message)
 
            'Catch ex3 As SmtpException
            '    MsgBox(ex3.Message)
 
        Catch ex As Exception
            '------------------------------------
            Dim strTmp As String = "To: " & strTo & " Cc: " & strCC & " From: " & myStruc.SMTP_FROM
            strErrMessage = ex.Message & vbCrLf & vbCrLf & strTmp
            If strErrMessage Like "*Domain not found*" Then 'ungültige Mailadresse
            End If
            myStruc.ERR_Message = strErrMessage
            myStruc.ERR_Modulname = mStrModul
            myStruc.ERR_ProzedurName = strProzedur
            myStruc.ERR_MAIL = False
            ERR_Handler(myStruc)
            myStruc.ERR_MAIL = True
            '------------------------------------
 
        End Try
    End Function

Spendierst du mir eine Tasse Kaffee? Ich würd mich riesig freuen. DANKE !! 🙂

Author: admin

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.