VB.Net – Mail versenden inkl. Screenshot als Anlage

Mit dieser VB.Net Funktion können Sie Mails inkl. eines Screenshots als Anlage versenden.

So wird es gemacht:

    '============================================
    ' VB.NET Beispiel in Visual Basic .Net
    ' © wilfried.bitz@bridgesolution.de
    '============================================
 
    Private Sub Mail_Fast_Send(strBody As String)
 
        Try
 
            '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( _
                 gStruc.APP_Path & "screenshot_" & Today & ".bmp")
            strAttachmments(0) = gStruc.APP_Path & "screenshot_" & Today & ".bmp"
 
            Dim blnOK As Boolean = MAIL_Send( _
                strMAIL_TO_1, strMAIL_TO_2, "", _
                strAPP_TITEL & " " & strAPP_VERSION, _
                strAPP_TITEL & " " & strAPP_VERSION & " - Wichtige Meldung !", _
                "" & "" & vbCrLf & strBody, _
                strAttachmments, "", gStruc)
 
 
        Catch ex As Exception
 
        End Try
    End Sub
 
    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
        End Try
    End function

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

[wpedon id=2074]


0 Kommentare

Schreibe einen Kommentar

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