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
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?
The property's I made:
Because I get mine from my.settings when a new "sendMail" is declared.![Image]()
Then finaly to send the mail I made the function "send".
Thx for reading for an example what uses this class: Or just the class: 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.
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)
Code: Select all
Why I assigned to all my variables a value, because in other languages u have to assign a value. 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 = ""

The property's I made:
Code: Select all
Why no username, password,..? 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
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

Then finaly to send the mail I made the function "send".
Code: Select all
Oh, red unerlined? U forgot to import! 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

Code: Select all
at the top!Imports System.Net.Mail
Thx for reading for an example what uses this class: Or just the class: 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
Copyright Information
Copyright © Codenstuff.com 2020 - 2023