VB.Net – Toolbox 1 – Mail SMTP INI Errorhandler Screenshot Stringbearbeitung Verschlüsselung Structure

Mit diesem Artikel stelle ich Ihnen eine komplette in sich geschlossene VB.Net Toolbox (Modul/Klasse) zur Verfügung mit vielen Funktionen die Sie 1 zu 1 in Ihrem VB.Net Projekt einbinden können. Darüber hinaus gibt es noch eine Initialisierungsprozedur die Sie beim Starten Ihrer Anwendung verwenden können. Damit stehen Ihnen alle wichtigen Parameter innerhalb einer Structure in der gesamten Anwendung zur Verfügung. Ausgelesen werden die Daten aus einer ein INI Datei die Sie von außen konfigurieren können.

So wird es gemacht:

INITIALISIERUNG

Option Strict On
Option Explicit On
 
Module modStart
 
    Public gStruc As IniStructure
    Private mStrModul As String = "modStart"
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Sub Start_Init()
 
        '----------------------------------------
        Dim strProzedur As String = "Start_Init()"
        '----------------------------------------
 
        Try
 
            '------------------------------------
            With gStruc
                .APP_TITEL = "Titel"
                .APP_VERSION = "1.0.2"
                .APP_Path = SYS_App_Path() & "\"
                .APP_INI_DATEI = .APP_Path & "system.ini"
 
                .SMTP_HOST = INI_Read("SMTP", "HOST", .APP_INI_DATEI)
                .SMTP_USER = INI_Read("SMTP", "USER", .APP_INI_DATEI)
                .SMTP_PW = CONV_PW_Read(INI_Read("SMTP", "PASSWORT", .APP_INI_DATEI))
                .SMTP_FROM = INI_Read("SMTP", "FROM", .APP_INI_DATEI)
                .SMTP_SSL = CBool(INI_Read("SMTP", "SMTP_SSL", .APP_INI_DATEI))
 
                .MAIL_TO_1 = INI_Read("MAIL", "TO_1", .APP_INI_DATEI)
                .MAIL_TO_2 = INI_Read("MAIL", "TO_2", .APP_INI_DATEI)
 
                .SQL_Kz = INI_Read("SQL", "SqlKz", .APP_INI_DATEI)
                .SQL_Pwd = CONV_PW_Read(INI_Read("SQL", "SqlPwd", .APP_INI_DATEI)) 
                .SQL_Server = INI_Read("SQL", "SqlServer", .APP_INI_DATEI)
                .SQL_DB = INI_Read("SQL", "SqlDB", .APP_INI_DATEI)
 
                .ERR_SILENT = CBool(INI_Read("SYSTEM", "ERR_SILENT", .APP_INI_DATEI))
                .ERR_MAIL = CBool(INI_Read("SYSTEM", "ERR_MAIL", .APP_INI_DATEI))
                .ERR_LOG = CBool(INI_Read("SYSTEM", "ERR_LOG", .APP_INI_DATEI))
 
            End With
            '------------------------------------
 
        Catch ex As Exception
            '------------------------------------
            gStruc.ERR_Message = ex.Message
            gStruc.ERR_Modulname = mStrModul
            gStruc.ERR_ProzedurName = strProzedur
            ERR_Handler(gStruc)
            '------------------------------------
 
        End Try
 
    End Sub
 
End Module

TOOLBOX 1:

'============================================
' VB.NET Beispiel in Visual Basic .Net
' © wilfried.bitz@bridgesolution.de
'============================================
Option Strict On
Option Explicit On
 
Imports System.Data.SqlClient
Imports System
Imports System.IO
Imports System.Text
Imports System.Net.Mail
 
Module modToolbox
 
    '--------------------------------------------
    Private mStrModul As String = "modToolbox"
    Public gSqlCON As New SqlConnection
    '--------------------------------------------
 
    '--------------------------------------------
    'INI Dateien
    Private Declare Ansi Function INI_WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" ( _
                    ByVal lpApplicationName As String, _
                    ByVal lpKeyName As String, _
                    ByVal lpString As String, _
                    ByVal lpFileName As String) As Integer
 
    Private Declare Ansi Function INI_GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" ( _
                    ByVal lpApplicationName As String, _
                    ByVal lpKeyName As String, _
                    ByVal lpDefault As String, _
                    ByVal lpReturnedString As StringBuilder, _
                    ByVal nSize As Integer, _
                    ByVal lpFileName As String) As Integer
    '--------------------------------------------
 
 
    '--------------------------------------------
    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
    '--------------------------------------------
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Sub INI_Write( _
         ByRef strBereich As String, _
         ByRef strKey As String, _
         ByRef strWert As String, _
         ByRef strIniDatei As String)
        On Error Resume Next
 
        'System.Windows.Forms.Application.DoEvents()
 
        Call INI_WritePrivateProfileString( _
            strBereich, _
            strKey, _
            strWert, _
            strIniDatei)
 
    End Sub
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Function INI_Read( _
        ByRef strBereich As String, _
        ByRef strKey As String, _
        ByRef strIniDatei As String) As String
        On Error Resume Next
 
        Dim sb As New StringBuilder(200)
        Dim Ret As Integer = _
            INI_GetPrivateProfileString( _
                strBereich, _
                strKey, _
                "?", _
                sb, _
                200, _
                strIniDatei)
 
        'Return sb.ToString()
        INI_Read = sb.ToString
 
    End Function
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Sub ERR_Handler(ByVal mySTRUC As IniStructure)
 
        Try
 
            '------------------------------------
            If Not mySTRUC.ERR_SILENT Then
                MessageBox.Show(mySTRUC.ERR_ProzedurName & " " & vbCrLf & mySTRUC.ERR_Message)
            End If
            '------------------------------------
 
            '------------------------------------
            If mySTRUC.ERR_LOG Then
                Log_Add(mySTRUC.ERR_ProzedurName & " " & vbCrLf & mySTRUC.ERR_Message, mySTRUC)
            End If
            '------------------------------------
 
            '------------------------------------
            If mySTRUC.ERR_MAIL Then
                'Mailbenachrichtigung inkl. Screenshot
                Dim strAttachmments(0) As String
                Dim tmpPic As System.Windows.Forms.PictureBox
                tmpPic = New System.Windows.Forms.PictureBox
                tmpPic.Image = SYS_CaptureScreen()
                tmpPic.Update()
                tmpPic.Image.Save( _
                     mySTRUC.APP_Path & "error_" & Today & ".bmp")
                strAttachmments(0) = mySTRUC.APP_Path & "error_" & Today & ".bmp"
                Dim blnOK As Boolean = MAIL_Send( _
                    mySTRUC.MAIL_TO_1, mySTRUC.MAIL_TO_2, "", _
                    mySTRUC.APP_TITEL & " " & mySTRUC.APP_VERSION, _
                    mySTRUC.APP_TITEL & " " & mySTRUC.APP_VERSION & " - Ein Fehler ist aufgetreten !", _
                    mySTRUC.ERR_ProzedurName & " " & vbCrLf & mySTRUC.ERR_Message, _
                    strAttachmments, "", mySTRUC)
            End If
            '------------------------------------
 
        Catch ex As Exception
 
        End Try
 
    End Sub
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Sub Log_Add( _
        ByVal strText As String, _
        ByVal mySTRUC As IniStructure)
        On Error Resume Next
 
        '----------------------------------------
        Dim strLogDatei As String = _
            mySTRUC.APP_Path & "LOG_" & Today & ".log"
        INI_Write("MESSAGE", CStr(Now), strText, strLogDatei)
        '----------------------------------------
 
        '----------------------------------------
        Exit Sub
        '----------------------------------------
 
    End Sub
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Function SYS_CaptureScreen() As Bitmap
        On Error Resume Next
 
        Dim b As New Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height)
        Dim g As Graphics = Graphics.FromImage(b)
        g.CopyFromScreen(0, 0, 0, 0, b.Size)
        g.Dispose()
        Return b
 
    End Function
 
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Function SYS_App_Path() As String
        SYS_App_Path = ""
 
        'Return System.IO.Path.GetDirectoryName(Application.StartupPath)
 
        Dim strTempPath As String
        strTempPath = System.Reflection.Assembly.GetExecutingAssembly().Location()
 
        Dim strFileName_Extension As String = _
            CONV_RestStringNachZeichen(strTempPath, ".")
        Dim lngFileName_Extension_Length As Long = _
            Len(strFileName_Extension)
        Dim strFileName As String = _
            CONV_RestStringNachZeichen(strTempPath, "\")
 
        SYS_App_Path = Left(strTempPath, Len(strTempPath) - Len(strFileName) - 1)
 
    End Function
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Function CONV_PW_Read(ByVal strPW_Verschluesselt As String) As String
        On Error Resume Next
 
        'Passwort entschlüsseln
        Dim i As Integer
        Dim strPW As String = Nothing
 
        For i = 1 To Len(strPW_Verschluesselt)
            strPW = strPW & Chr(25 Xor Asc(Mid(strPW_Verschluesselt, i, 1)))
        Next i
 
        CONV_PW_Read = strPW
 
    End Function
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Function CONV_PW_Write(ByVal strPW_Unverschluesselt As String) As String
        On Error Resume Next
 
        'Passwort verschlüsseln
        Dim i As Integer
        Dim strPW As String = Nothing
 
        For i = 1 To Len(strPW_Unverschluesselt)
            strPW = strPW & Chr(25 Xor Asc(Mid(strPW_Unverschluesselt, i, 1)))
        Next i
 
        CONV_PW_Write = strPW
 
 
    End Function
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Function CONV_RestStringNachZeichen(ByVal strString As String, ByVal strZEICHEN As String) As String
        CONV_RestStringNachZeichen = Nothing
 
        On Error Resume Next
 
        Dim nI As Integer
        Dim nPos As Integer
 
        For nI = 1 To Len(strString)
            nPos = InStr(nI, strString, strZEICHEN)
            If Not nPos > 0 Then
                CONV_RestStringNachZeichen = Mid(strString, nI)
                Exit For
            End If
        Next nI
 
    End Function
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Function CONV_RestStringVorZeichen( _
          ByVal strString As String, _
          ByVal strZEICHEN As String) As String
        On Error Resume Next
        CONV_RestStringVorZeichen = ""
 
        Dim nI As Integer
        Dim nPos As Integer
 
        For nI = 1 To Len(strString)
            nPos = InStr(nI, strString, strZEICHEN)
            If nPos > 0 Then
                CONV_RestStringVorZeichen = Left(strString, nPos - 1)
                Exit For
            End If
        Next nI
 
 
    End Function
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Function CONV_NS(ByRef varStringOrNull As Object) As String
        On Error Resume Next
 
        If IsDBNull(varStringOrNull) Or IsNothing(varStringOrNull) Then
            CONV_NS = ""
        Else
            CONV_NS = CStr(varStringOrNull)
        End If
 
        Exit Function
 
    End Function
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Function CONV_NN(ByRef varStringOrNull As Object) As String
        On Error Resume Next
        'ConvNullToNumber
 
        If IsDBNull(varStringOrNull) Or IsNothing(varStringOrNull) Or Not IsNumeric(varStringOrNull) Then
            CONV_NN = "0"
        Else
            CONV_NN = CStr(varStringOrNull)
        End If
 
        Exit Function
 
    End Function
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Function CONV_Doppelstrich(ByRef cString As String, ByRef lngModusEinsZwei As Integer) As String
 
        Dim cRueckgabe As String = Nothing
 
        Select Case lngModusEinsZwei
            Case 1
                cRueckgabe = CONV_BeliebigesZeichen(CStr(cString), "'", "''")
            Case 2
                cRueckgabe = CONV_BeliebigesZeichen(CStr(cString), "'", "" & "' & " & Chr(34) & "'" & Chr(34) & " & '" & "")
        End Select
 
        CONV_Doppelstrich = cRueckgabe
 
 
    End Function
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    Public Function CONV_BeliebigesZeichen( _
         ByRef strString As String, _
         ByRef cSuchzeichen As String, _
         ByRef cNeuzeichen As String) As String
 
        Dim nI As Integer
        Dim cRueckgabe As String
 
        CONV_BeliebigesZeichen = strString
 
        cRueckgabe = ""
        For nI = 1 To Len(strString)
            Select Case Mid(strString, nI, 1)
                Case cSuchzeichen
                    cRueckgabe = cRueckgabe & cNeuzeichen
                Case Else
                    cRueckgabe = cRueckgabe & Mid(strString, nI, 1)
            End Select
        Next nI
 
        CONV_BeliebigesZeichen = cRueckgabe
 
        Exit Function
 
    End Function
 
    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
    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
 
End Module

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


Schreibe einen Kommentar

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