Advertisment

Adding Mailing Capability to your KMS

author-image
PCQ Bureau
New Update

In the previous part of this series (Adding an Address Book to your KMS, p110,
July 06) we added address book and contact management capabilities to our KMS system. We're back
after a month's gap and this month, we make use of that data to send our
contacts e-mail, either on-demand or on a pre-configured schedule. This way, we
can send them specific contact messages or pre-created alerts and newsletters for various purposes. And what's
more, since we're using an SQL Server database for our backend, we simply add
all the messages we send (and all the replies we receive) intothis database for
automatic database-based archival.

Advertisment

Unlike our address book, DMS and other modules so far, this module requires a
few components to be installed on our application server before it gets
cracking. Namely, we would need an SMTP and a POP3 server. If you want to use
Newsgroup functionality though the KMS (not a part of the project at this
point), you need to install an NNTP server as well. Since we're anyway using a
Windows Server 2003 system for our app server, this is already there with the OS
and all you need to do is go into Add/Remove Programs> Add/Remove Windows
Components and install the SMTP, POP3 and NNTP servers from there.
Alternatively, you can launch the Manage Your Server front-end from the
Administrative Tools menu and audit from there.

Direct
Hit!
Applies
to:
CIOs
USP:
Adding a messaging component to our KMS project
Links:
http://tinyurl.com/q5bcd 
Google
keywords:
mail mass newsletter

Sending mail

Sending out mail using ASP.NET requires just a couple of lines of code. All
you do need to do is set up your recipient list, add any attachments you have, include the subject and body of
the e-mail and call the Send function. If additionally, the mail server administrator configured your SMTP server to
require authentication before sending out mail (this helps prevent someone/thing
from using your SMTP server as a spam relay and is really recommended), you need
an additional couple of lines that specify the username and password to the SMTP
backend in .NET. You should import the System.Web.Mail namespace for this module
to work.

Advertisment

Public Sub AddAttachment(ByVal FullPathToFileOnServer As String)

Dim MA As MailAttachment, L As Int32

MA = New MailAttachment(AttFilePath)

If (m_Attach Is Nothing) Then

        ReDim m_Attach(0)

        L = 0

Else

        L = m_Attach.Length

        ReDim Preserve m_Attach(L - 1)

        L -= 1

End If

m_Attach(L) = MA

End Sub

Our SMTP and POP modules
use a common Attachment Handler to save attachments into our DMS. This
maintains quality of data in our DMS database and avoids duplicity

Do note that to add attachments in .NET, you need to add it to the
Attachments collection as in the above code. m_Attach is an object of type
System.Web.Mail.MailAttachment. Next, we compose our SMTP mail message and send
it out.

Advertisment

Public Sub SendMail()

Dim Msg As MailMessage, MA As MailAttachment

    Try

    Msg = New MailMessage

    With Msg

        ' Set username & password

        ' if authentication is required

        ' setup: BodyFormat, From,

        ' To, Cc/Bcc, Subject and Body

        ' properties of the Msg object

If ((Not m_Attach Is Nothing) AndAlso (m_Attach.Length > 0)) Then

    For Each MA In m_Attach

        .Attachments.Add(MA)

    Next

    End If

End With

SmtpMail.SmtpServer = SmtpServer

SmtpMail.Send(Msg)

Catch Ex As Exception

        ' error handler

    End Try

End Sub



Saving to database

Before the e-mail is actually sent out, it is best to store our entire message
into our SQL Server database. Our table structure mimics the SMTP construct for
convenience with attachments stored as objects in the DMS table we created in
part 1 of the series (Roll your own IPMS, p66, June 2006). The code to do all
this is in our DMS module, we simply call the function as follows:

Advertisment

SaveDMSAsset( AttachmentFilename, MailFromUserID, MailSubject, MailFromUserID
)

SaveDMSContent ( MailFromUserID, MailBodyContent )

AttachmentFilename is the name of the attachment file we are adding to the
DMS. The MailFromUserID is the ID of the person sending this mail. We are

ignoring a lot of other fields in the DMS table (like document ID and so on) as
they will be automatically generated by the SaveDMSAsset function.
MailBodyContent used in the second function is the body of the mail being sent
out. SaveDMSContent directly stores data into a DMS table called DMS_TextContent
(fields being a unique ID, and a Text datatype Content).

Of course, it will be ridiculous to store duplicates of the document in the DMS
table. But, that is taken care of by the DMS code that we call, which first
checks the table to see if the document is already in there and stores the one
we specify only if it is not already present.

Advertisment



Receiving e-mail

This is slightly complicated. .NET does not provide the means to talk to the
POP3 server the same way as the SMTP server and we need to actually write our
own POP3 client to log on to the server using Socket connections and retrieve
messages. The code for this client is quite large to be given in this article in
any useful size and context. It suffices to say that we shall simply call the
below sequence to do our job:

Dim Msg As POPMessage

...

Msg = RetrievePOPMessage()

If (Not Msg Is Nothing) Then

    CheckSpam( Msg )

    SaveDMSContent( Msg.Body )

    For Each m_Attach In _

        Msg.Attachments

    SaveDMSAsset( m_Attach, _

        MailToUserID, _

        Msg.Subject, MailToUserID )

    Next

End If

Finally, we store this content into our database, using the same function (SaveDMSAsset)
as above.

Advertisment

We now have just one final part next month where we create a portal that links
all our various modules together. This would let all your users access the
system from one consistent interface. The final completed application would be
carried in our October 2006 Enterprise disk, along with a full installer program that will install, configure and walk you through the initial working of
the application.

Advertisment

Stay connected with us through our social media channels for the latest updates and news!

Follow us: