codedocu.de

 

 

hier finden Sie ein einfaches Beispiel eines Net SMTP Sendmail Clients.

Mit diesem Code oder Beispiel kann man einfach Emails automatisch aus der Net Anwendung heraus senden.

 

 

Das Beispiel zeigt, dass man nur die Email Absender und Empfänger Daten benötigt, sowie den Email Server, über welchen die Email versendet werden.

Zu diesem erstellt man eine .Net MailMessage mit den Sendeadressen.

System.Net.Mail.MailMessage(MailAdress_From, MailAdress_To)

 

Anschliessend erstellt man einen Mail Sendecliente und versendet die Message

Dim objMailClient As New System.Net.Mail.SmtpClient(sSMTP)

..

objMailClient.Send(vMail)

 

 

Eingabeformular zum Test

Winform, WPF: Einen Mail Client als SendMail Client in Net erstellen

 

Nach dem Senden kann man den eigenen Email Account gegenprüfen ob die Email angekommen ist

Winform, WPF: Einen Mail Client als SendMail Client in Net erstellen

 

.Net Framework Code zum Aufbauen des Send-Mail Clients

SMTPMailClient

            '---< Mailobjekt erzeugen und senden >--

            Dim MailAdress_From As New System.Net.Mail.MailAddress(FROM_Email_Address)

            Dim MailAdress_To As New System.Net.Mail.MailAddress(TO_Email_Address)

            Dim vMail As New System.Net.Mail.MailMessage(MailAdress_From, MailAdress_To)

 

            vMail.IsBodyHtml = True

            vMail.Subject = Subject_Title

            vMail. = Text_Body

 

            '--< Mailobject >--

            '*create mail client and send

            Dim objMailClient As New System.Net.Mail.SmtpClient(sSMTP)

            objMailClient.UseDefaultCredentials = False '*either windows login or user-password

            objMailClient.Credentials = New Net.NetworkCredential(FROM_Email_Address, sPassword)

            '< Senden >

           

                objMailClient.Send(vMail)

 

                '< #test >

                objMailClient.Dispose()

                '</ #test >

                'objMailClient.SendAsync                 '*ansonsten sendasync probieren

 

 

 

 

Public Class Form1

 

 

#Region "Buttons"

    '-------------------------< Region: Buttons >------------------

    Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click

 

        Dim IsSend As Boolean

        IsSend = Send_Email(tbxTo.Text, "", tbxFrom.Text, tbxPassword.Text, tbxDomain.Text, tbxSubject.Text, tbxText.Text)

        MsgBox("Email Status send is: " & IsSend)

    End Sub

    '-------------------------</ Region: Buttons >------------------

#End Region

 

 

#Region "Functions"

    '-------------------------< Region: Functions >------------------

    Public Function Send_Email(ByVal TO_Email_Address As String, ByVal CC_Email_Address As String, ByVal FROM_Email_Address As String, ByVal Password As String, ByVal sSMTP As String, ByVal Subject_Title As String, ByVal Text_Body As String, Optional ByVal Attachements_Path As String = "") As Boolean

        '-------------------------------< Funktion: Email erstellen und senden >-------------------------------

        '--< Variablen >--

        Dim Result_Send As Boolean = False

        

        '--</ Variablen >--

 

        '-< Kontrolle >-

        If TO_Email_Address Like "" Then

            MsgBox("Email Adress TO is empty", MsgBoxStyle.Critical, "Send_Email")

            Return False

            Exit Function

        End If

        If FROM_Email_Address Like "" Then

            MsgBox("Email Adress FROM is empty", MsgBoxStyle.Critical, "Send_Email")

            Return False

            Exit Function

        End If

 

        '-</ Kontrolle >-

 

        Dim arrEmpfaenger As Array

        arrEmpfaenger = Split(TO_Email_Address, ";", , CompareMethod.Binary)

        If arrEmpfaenger.Length > 1 Then

            TO_Email_Address = arrEmpfaenger(0)

        End If

 

 

        '----< Email >----

        Try

            '---< Mailobjekt erzeugen und senden >--

            Dim MailAdress_From As New System.Net.Mail.MailAddress(FROM_Email_Address)

            Dim MailAdress_To As New System.Net.Mail.MailAddress(TO_Email_Address)

            Dim vMail As New System.Net.Mail.MailMessage(MailAdress_From, MailAdress_To)

            '< weitere empfaenger anfuegen >

            If arrEmpfaenger.Length > 1 Then

                For iAn As Integer = 1 To arrEmpfaenger.Length - 1

                    vMail.To.Add(arrEmpfaenger(iAn))

                Next

            End If

            '</ weitere empfaenger anfuegen >

 

            If Not CC_Email_Address Like "" Then

                'Dim vAdressCC As New System.Net.Mail.MailAddress(sEMailcc)

                Dim arrCC As Array

                arrCC = Split(CC_Email_Address, ";", , CompareMethod.Binary)

                For iCC As Integer = 0 To arrCC.Length - 1

                    vMail.CC.Add(arrCC(iCC))

                Next

 

            End If

            vMail.IsBodyHtml = True

            vMail.Subject = Subject_Title

            vMail. = Text_Body

 

            '--< Mailobject >--

            '*create mail client and send

            Dim objMailClient As New System.Net.Mail.SmtpClient(sSMTP)

            objMailClient.UseDefaultCredentials = False '*either windows login or user-password

            objMailClient.Credentials = New Net.NetworkCredential(FROM_Email_Address, Password)

            '< Senden >

            Try

 

                '===== < Achtung : Hier wird gesendet  >====

                '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

                'debug

                objMailClient.Send(vMail)

                '/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 

                '< #test >

                '*ansonsten sendasync probieren

                objMailClient.Dispose()

                '</ #test >

                'objMailClient.SendAsync

                '===== </ Achtung : Hier wird gesendet  >====

 

 

 

                'Status_Text = "status=OK /send to:" & TO_Email_Address & " subject:" & Subject_Title

                Result_Send = True

            Catch ex As Exception

                '-< Fehler >-

                Dim sError As String

                If ex.InnerException Is Nothing Then

                    sError = ex.Message

                Else

                    sError = ex.Message & vbCrLf & ex.InnerException.Message

                End If

                MsgBox(sError)

                '-</ Fehler >-

            End Try

            '</ Senden >

            '---</ Mailobjekt erzeugen und senden >--

        Catch ex As Exception

            '-< Fehler >-

            Dim sError As String

            If ex.InnerException Is Nothing Then

                sError = ex.Message

            Else

                sError = ex.Message & vbCrLf & ex.InnerException.Message

            End If

            MsgBox(sError)

            '-</ Fehler >-

        End Try

 

        '--< Mailobject >--

 

        '< Abschluss >

        Return Result_Send

        '</ Abschluss >

        '----</ Email >----

        '-------------------------------< Funktion: Email erstellen und senden >-------------------------------

    End Function

 

    '-------------------------</ Region: Functions >------------------

#End Region

 

End Class

 

 


Software Entwicklung Stuttgart Nürtingen
Suche Projekte C#, WPF, Windows App,ASP.Net, vb.Net, WinForms, SQL Server, Access, Excel