123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Computers >> View Article

Email Client With Anti-virus (delphi Internet Components)

Profile Picture
By Author: Sergey Shirokov
Total Articles: 2
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

Abstract

These days there are a lot of different ways of exchanging documents, data and any other important information. Among of these technologies, E-mail is the most commonly used way of exchanging information. As a result, Emails are widely used for disturbing with unwanted messages, as viruses and Trojans and other bad codes. That is why it is necessary to protect users by integrating an E-mail anti-virus scan functionality to E-mail client and server programs.

This article demonstrates how to write a simple Mail client program in Delphi with a functionality similar to the Windows Live Mail client that allows you to receve E-mails from the POP3 mailbox, scan it on the fly with anti-virus and display to the user. The message composing and sending via the SMTP protocol with embedded images and file attachments is implemented as well. The program allows you to connect to secure mail servers, such as Google Mail and Yahoo Mail via SSL/TLS.

Receive E-mail and parse the raw message source

For both, receiving and sending E-mails and also for the parsing of received messages we will use the Internet ...
... Components from the Clever Internet Suite . These components implement the necessary functionality for working with mails, and can be easily integrated with anti-virus scanner API. The Clever Internet Suite can connect to the POP3 mailbox, download E-mails, manage mailboxes via an IMAP4 protocol, send E-mails via SMTP and so on. The SSL/TLS encryption is supported as well. The TclMailMessage component works with MIME-formatted E-mails, parses raw message sources, extracts both, text and HTML bodies, embedded images and file attachments, composes new messages in any appropriate format.

clMailMessage.BuildMessage(text, html, imageList, attachmentList);
clMailMessage.Subject := subject;
clMailMessage.From.FullAddress := fromAddress;
clMailMessage.ToList.Add(recipient);

clSMTP.Server := FAccounts.SMTPServer;
clSMTP.UserName := FAccounts.SMTPUser;
clSMTP.Password := FAccounts.SMTPPassword;
clSMTP.Open();
clSMTP.Send(clMailMessage);

...
clPOP3.Open();
for i := 0 to clPOP3.MessageCount - 1 do
begin
clPOP3.Retrieve(i+1, clMailMessage);
text := clMailMessage.Text;
html := clMailMessage.Html;
subject := clMailMessage.Subject;
....
end;

If you need to digitally sign or encrypt your mail with x509 certificates, choose another component - TclSMimeMessage. This component is inherited from the TclMailMessage, and can be easily incorporated into your code for SMIME message support.

In addition you can use any third-party Internet library, e.g. Indy, for connecting to the POP3 mailbox, sending messages via an SMTP protocol and parsing raw message sources. Depending on the selected library, the implemented features in your program may change.
Call anti-virus scan programmatically

As an example of an anti-virus scanner, we used the Clam AV antivirus engine. This is an open source (GPL) solution designed for detecting Trojans, viruses, malware and other malicious threats. It contains also the Delphi API. So we can easily integrate it into our project. Alternatively you can use any other anti-virus scanner, which provides you with a similar functionality and allows scanning memory blocks and/or files onto the disk. See also Symantec Scan Engine

For instance, let us write a special basic wrapper class with abstract scanning methods and an inherited class that implements the ClamAV API:

TclAVScanner = class
...
protected
function ScanMessageSource(Msg: TStrings): Boolean; virtual; abstract;
function ScanAttachment(const AFileName: string): Boolean; virtual; abstract;
public
function ScanMessage(Msg: TStrings): Boolean;
property TempDir: string read FTempDir write SetTempDir;
end;

TclClamAVScanner = class(TclAVScanner)
private
FAV: TClamAV;
....
protected
function ScanMessageSource(Msg: TStrings): Boolean; override;
function ScanAttachment(const AFileName: string): Boolean; override;
...
end;

function TclClamAVScanner.ScanMessageSource(Msg: TStrings): Boolean;
begin
Result := (FAV.ScanMem(Msg.Text) = '');
end;

function TclClamAVScanner.ScanAttachment(const AFileName: string): Boolean;
begin
Result := (FAV.ScanFile(AFileName) = '');
end;

The TClamAV class is declared in Clam AV API and can be downloaded at ClamAV antivirus, Delphi edition

A malicious code can be within the message body or come together with encoded and packed attachments. So the best choice is to scan both 1) the raw message source and 2) the E-mail attachments extracted and saved to a temporary folder on the disk. The TclMailMessage component does all the work easily:

MsgSource: TStringList.Create;
//load MsgSource with raw message source
...
FMailMessage := TclMailMessage.Create(nil);
FMailMessage.OnSaveAttachment := DoMailMessageSaveAttachment;
FMailMessage.MessageSource := MsgSource;
.....

procedure TclAVScanner.DoMailMessageSaveAttachment(Sender: TObject;
ABody: TclAttachmentBody; const AFileName: string; var AData: TStream; var Handled: Boolean);
begin
AData := TFileStream.Create(AFileName, fmCreate);
Handled := True;
end;
View and Edit the message content

For displaying and editing the E-mail content the TRichView component from www.trichview.com will be used. TRichView is a suite of native Delphi/C++Builder components for displaying, editing and printing hypertext documents. This component can be easily integrated with Clever Internet Suite E-mail components. There is a demo, which provides the functionality of simple E-mail client and allows composing, sending, receiving and managing messages on POP3 mailbox: Functions for editing and sending HTML emails with the Clever Internet Suite

We can use this demo as as a starting point for developing our E-mail client with an anti-virus scanner. All that we need is adding an additional folder to the folders list. This folder will be used for stoding of infected messages.

Next, we need to create an instance of the TclClamAVScanner object and, finally, call it when receiving E-mails:

for i := 0 to clPOP3.MessageCount - 1 do
begin
....
clPOP3.Retrieve(i+1);
MsgItem.MailMessage.Assign(clPOP3.Response);

if not FAVScanner.ScanMessage(MsgItem.MailMessage) then
begin
MsgItem.Status := msInfected;
end;
....
end;

Please check the demo from the link above, to learn more about the message retrieving function.

Resources

The source code for the MailClient program with anti-virus scanner and also a list of resources that are needed to compile the program can be downloaded here:
http://www.clevercomponents.com/articles/article034/mailclientav.asp

Total Views: 204Word Count: 917See All articles From Author

Add Comment

Computers Articles

1. Devopsin Hallinnoimat Palvelut: Tietoturva Ja Tiedon Hallinta
Author: harju

2. Hyödynnä Pilvesi Täysi Potentiaali Google Cloud Monitoring Solutions -ratkaisuilla
Author: harju

3. Pysy Kyberuhkien Edellä Google Cloud Security Services -palvelun Avulla
Author: harju

4. Mullista Yrityksesi Nopeilla Ja Turvallisilla Googlen Pilvipalveluratkaisuilla
Author: harju

5. Googlen Pilvi-identiteetin Hallintapalvelut Pienille Ja Keskisuurille Yrityksille
Author: harju

6. Google Cloud -konsultointi: Tukea, Strategiaa Ja Kehitystä Yrityksellesi
Author: harju

7. Iot Edge -laskentapalvelut Ja Ai – Älykkään Datankäsittelyn Tulevaisuus
Author: harju

8. Älykäs Reunalaskenta: Tehokkuutta Ja Kilpailuetua Yrityksellesi
Author: harju

9. Cloud Change -palvelut – Tulevaisuuden It-ratkaisu Liiketoiminnallesi
Author: harju

10. Aws-tietokannan Siirtopalvelu Ja Hybridiympäristöt – Miten Ne Toimivat Yhdessä?
Author: harju

11. What Makes Google Ads Management Services Better Than Other Digital Marketing Campaigns?
Author: Digital Agency

12. Things You Should Know About Nema And International Plug Adapters
Author: Jennifer Truong

13. The Best 8 Tools For Data Analysis
Author: goodcoders

14. Top 5 Customer Experience Management Problems And Solution
Author: goodcoders

15. How To Create A Framework-agnostic Application In Php?
Author: goodcoders

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: