•      Powered by
 

Sending Email Class with System.Net.Mail class in .NET 2.0 Framework.

by Steve Schofield

This is a class I put together that has 3 methods that accept different parameters depending on how you want to send an email..   The best resource I found for the System.Web.Mail name is http://Systemnetmail.com. This website is created by Dave Wanta, the creator of ASPNetEmail component, the best email component when using .NET.   Here is another great resource on MSDN.  This is the documentation regarding this namespace.  I couldn't believe how easy it was compared to the early classic ASP days.  Amazing this is built-in functionality.  Happy email coding!

Imports System.Net
Imports System.Net.Mail

Public Class SendMessage
    Sub SendEmail()
        Dim Message As New SmtpClient
        Message.Host = "127.0.0.1"
        Message.Port = 25
        Message.Send(_From, _SendTo, _Subject, _Send)
    End Sub

    Sub SendEmail(ByVal From As String, ByVal SendTo As String, ByVal Subject As String, ByVal Body As String, ByVal Host As String, ByVal Port As String)
        Dim Message As New SmtpClient
        Message.Host = Host
        Message.Port = Port
        Message.Send(From, SendTo, Subject, Body)
    End Sub

    Sub SendEmail(ByVal From As String, ByVal SendTo As String, ByVal Subject As String, ByVal Body As String, ByVal Host As String, ByVal Port As Int32, ByVal UserName As String, ByVal Password As String)
        'create the mail message
        Dim mail As New MailMessage()
        Dim smtp As New SmtpClient
        'set the addresses
        mail.From = New MailAddress(From)
        mail.To.Add(SendTo)

        'set the content
        mail.Subject = Subject
        mail.Body = Body

        'send the message
        If Port = 25 Then
            smtp = New SmtpClient(Host, 25)
        Else
            smtp = New SmtpClient(Host, Port)
        End If

        'to authenticate we set the username and password properites on the SmtpClient
        smtp.Credentials = New NetworkCredential(UserName, Password)
        smtp.Send(mail)
    End Sub

    Private _From As String
    Private _SendTo As String
    Private _Subject As String
    Private _Body As String
    Private _Send As String
    Private _Host As String
    Private _Port As String

    Public Property Send() As String
        Get
            Return _Send
        End Get
        Set(ByVal value As String)
            _Send = value
        End Set
    End Property

    Public Property From() As String
        Get
            Return _From
        End Get
        Set(ByVal value As String)
            _From = value
        End Set
    End Property

    Public Property SendTo() As String
        Get
            Return _SendTo
        End Get
        Set(ByVal value As String)
            _SendTo = value
        End Set
    End Property

    Public Property Subject() As String
        Get
            Return _Subject
        End Get
        Set(ByVal value As String)
            _Subject = value
        End Set
    End Property

    Public Property Body() As String
        Get
            Return _Body
        End Get
        Set(ByVal value As String)
            _Body = value
        End Set
    End Property
End Class

 

tio

Terms of Use | Privacy Statement ©2005-2006 IISLogs.com. All rights reserved - Powered by IIS7 - info @ www.IIS.net