OOP - sendMail Class

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
1 post Page 1 of 1
Contributors
User avatar
Kieken72
VIP - Donator
VIP - Donator
Posts: 231
Joined: Sun May 02, 2010 10:38 am

OOP - sendMail Class
Kieken72
So for my tutorial "OOP" found here.
I made a OOP class for sending a mail.
How we start?
Just as easy Declare and make property's.
What we need for sending an email?
  • - Username
    - Password
    - smtp Adress
    - smtp Port
    - SSL
    - Reciever
    - Message Body
    - Message Subject
    - Attachment (Class supports only one attachment)
Declared as:
Code: Select all
    Private strUserName As String = ""
    Private strPassword As String = ""
    Private intServerPort As Integer = 0
    Private strServerHost As String = ""
    Private blnServerSSL As Boolean = False
    Private arrReceivers As ArrayList
'Why arraylist?
Because u can have more then one reciever.'
    Private strMessage As String = ""
    Private strSubject As String = ""
    Private strAttachment As String = ""
Why I assigned to all my variables a value, because in other languages u have to assign a value. :)

The property's I made:
Code: Select all
    Public Sub addReciever(value As String)
        arrReceivers.Add(value)
    End Sub
    WriteOnly Property Subject As String
        Set(value As String)
            strSubject = value
        End Set
    End Property
    WriteOnly Property message As String
        Set(value As String)
            strMessage = value
        End Set
    End Property
    WriteOnly Property Attachment As String
        Set(value As String)
            strAttachment = value
        End Set
    End Property
Why no username, password,..?
Because I get mine from my.settings when a new "sendMail" is declared.
Code: Select all
    Public Sub New()
        strUserName = My.Settings.Username
        strPassword = My.Settings.Password
        intServerPort = My.Settings.serverPort
        strServerHost = My.Settings.serverHost
        blnServerSSL = My.Settings.serverSSL

        arrReceivers = New ArrayList

        strMessage = "No message"
        strSubject = "No subject"
    End Sub
Image

Then finaly to send the mail I made the function "send".
Code: Select all
    Public Sub send()
        Dim smtpServer As New SmtpClient()
        Dim mail As New MailMessage()
        smtpServer.Credentials = New Net.NetworkCredential(strUserName, strPassword)
        smtpServer.Port = intServerPort
        smtpServer.Host = strServerHost
        smtpServer.EnableSsl = blnServerSSL
        mail = New MailMessage()
        mail.From = New MailAddress(strUserName)
        For Each reciever As String In arrReceivers
            mail.To.Add(reciever)
        Next
'To make sure u send to each reciever'
        If strAttachment <> "" Then
            Dim file As String = strAttachment.ToString
            Dim data As Attachment = New Attachment(file)
            mail.Attachments.Add(data)
        End If
'If there is an attachment'
        mail.Subject = strSubject
        mail.IsBodyHtml = True
'Body html? '
        mail.Body = strMessage
        smtpServer.Send(mail)

    End Sub
Oh, red unerlined? U forgot to import! :)
Code: Select all
Imports System.Net.Mail
at the top!

Thx for reading for an example what uses this class:
MadMarch-FancyMail Kieken72.rar
Or just the class:
sendMail.rar
Btw: U can use my class for free, i didn't put my name in the class. If u use can i please ask to give me credits.
You do not have the required permissions to view the files attached to this post.
1 post Page 1 of 1
Return to “Tutorials”