The EmailServe class was designed to be accessed from VBScript code. Read more on using .Net DLLs in classic ASP in my old Registering a .NET DLL to COM tutorial. EmailServe goes through the process of sending the message just as you would in a standard ASP.NET page. The difference is in the handling of the various collections, such as the Recipient's list and the Attachment's list. All of these lists are handled as the .NET object, ArrayList. ArrayList does not translate into anything in VBScript. As a result, all of our public properties require functions to allow our calling ASP page to access them. Along these same lines is the "Get" portion of the property. That little chunk of code turns our array list into a simple string for use in ASP.
'-----------------------TO-----------------------------
Private _Recipients As ArrayList 'Stores MailAddresses
Public Property Recipients() As String
Get
Dim rawout As String = ""
For Each item As MailAddress In _Recipients
rawout &= item.Address & "; "
Next
rawout = rawout.Substring(0, rawout.Length - 2)
Return rawout
End Get
Set(ByVal value As String)
Me._Recipients.Add(New MailAddress(value))
End Set
End Property
Public Sub AddRecipient(ByVal Address As String, ByVal Name As String) 'For use in plain ASP
Me._Recipients.Add(New MailAddress(Address, Name))
End Sub
Most of our variables that are exposed to ASP work this way. The only one that is more complicated is the Attachments list because the attachments list has to locate the file on the server. The property I have written also generates a string explaining what files are attached to this email.
Private _Files As ArrayList 'Attachments!
Public Property Files() As String
Get
Dim rawout As String = ""
For Each item As Attachment In _Files
rawout &= item.Name
Next
Return rawout
End Get
Set(ByVal value As String)
Dim file As String = System.Web.Hosting.HostingEnvironment.MapPath(value)
Dim attach As New Attachment(file)
Me._Files.Add(attach)
'Optional code that appends information about the attached files to our email
If Me._AttachmentInfo <> "Attached Files: " Then
Me._AttachmentInfo &= " / "
End If
Me._AttachmentInfo &= System.IO.Path.GetFileName(file) & " (" & System.IO.File.GetCreationTime(file).Date & ")"
End Set
End Property
I've created a little wrapper function in this class for dealing with the creation of the SmtpClient object responsible for sending the email. This function ensures that if all the values aren't passed to the class the class will use the default values to send from the local machine.
'---------------------PRIVATE FUNCTIONS---------------------
Private Function SmtpSetup(ByVal server As String, ByVal port As Integer, ByVal user As String, ByVal pass As String) As SmtpClient
Dim serve As New SmtpClient()
If Not String.IsNullOrEmpty(server) Then
serve.Host = server
End If
If Not port = 0 Then
serve.Port = port
End If
If Not String.IsNullOrEmpty(user) Then
serve.Credentials = New NetworkCredential(user, pass)
End If
Return serve
End Function
The most important part of the class is of course our actual send function.
'--------------------SEND FUNCTIONS--------------------
'Do all the work this object is intended for.
Public Function Send() As Boolean
Try
Dim message As New MailMessage()
message.From = New MailAddress(Me._Sender, Me._SenderName)
For Each item As MailAddress In Me._Recipients
message.To.Add(item)
Next
For Each item As MailAddress In Me._CCs
message.CC.Add(item)
Next
For Each item As MailAddress In Me._BCCs
message.Bcc.Add(item)
Next
For Each item As Attachment In Me._Files
message.Attachments.Add(item)
Next
message.Subject = Me._Subject
Dim strBody As String = Me._Body
'If settings are correct and there are attachments, write out information on those attachments.
If Me._ShowFileInfo And Me._AttachmentInfo <> "Attached Files: " And Not Me._IsBodyHtml Then
strBody &= Environment.NewLine & Environment.NewLine & Me._AttachmentInfo
ElseIf Me._ShowFileInfo And Me._AttachmentInfo <> "Attached Files: " Then
strBody &= "<br><br><b style=""color:#c0c0c0;font-size:10px;"">" & Me._AttachmentInfo & "</b>"
End If
message.Body = strBody
message.IsBodyHtml = Me._IsBodyHtml
Dim client As SmtpClient = SmtpSetup(Me._Server, Me._Port, Me._Username, Me._Password)
client.Send(message)
Catch ex As Exception
Me._Errors = ex.Message
Return False
End Try
Return True
End Function