Click here to Skip to main content
15,885,767 members
Articles / Mobile Apps
Article

Using a system tray application to check , send and configure emails

Rate me:
Please Sign up or sign in to vote.
1.67/5 (5 votes)
25 Aug 20023 min read 161.4K   4K   94   15
This article demonstrates creation of a system tray application ,creation of processes , reading and writing XML data

Image 1

Introduction

This article aims at demonstrating creation of a system tray application , which uses a context menu. This article also explains creation of processes and handling XML Data (reading and writing ).

Why Desktop Mail Checker ?

Let me confess some thing guys !!! I have a trash of mails jumping in to me almost all of the 24 hours. I act a little bit frenzied about checking mails. I check my mails and recheck and I keep doing that over and over. I felt keeping my mail client opened was annoying and I wanted to do something to overcome this.

I wanted to write an application that sits over your system tray and convenient enough to check mails easily without hassles. Another thing was , I wanted to write an system tray application , and wanted to create process at run time, and Viola !! I posted this article.

The application has two classes - the DesktopMailChecker and mailDomain classes.

popupmenu.jpg

The DesktopMailChecker class has a
notifyicon 
control . Icons in the status area are short cuts to processes that are running in the background of a computer, such as a virus protection program or a volume control. These processes do not come with their own user interfaces. NotifyIcon provides a way to program in this functionality. Icon defines the icon that appears in the status area. Pop-up menus for an icon are addressed with ContextMenu and context menu control . The ContextMenu class represents shortcut menus that can be displayed when the user clicks the right mouse button over a control or area of the form.

This class also initiates the creation of a process using the

Process 
class.

Sample Image - Desktop_Mail_Checker.jpg

The MailDomain class has checkbox controls to receive the user options and the results are updated in an XML File. XMLTextReader and XMLTextWriter are used to read and write XML data in the application.

Things to remember before creating a system tray application.....

  • Include a NotifyIcon class into the project.
  • Use a ContextMenu class to provide a menu to the NotifyIcon class
  • Create a icon or use an icon to the NotifyIcon.Icon

The code and illustrations are given below.

The DesktopMailChecker class contains code to create process.

C#
private void menuItem4_Click(object sender, System.EventArgs e)
{
    MessageBox.Show("Checking Mails","Message");

         // Create a Process Object    
    Process myProcess = new Process();
    
         // Get the process start information of outlook
    ProcessStartInfo  myProcessStartInfo = 
           new ProcessStartInfo("outlook.exe");
    
    // Assign 'StartInfo' of outlook to 'StartInfo' of 'myProcess' object.
    myProcess.StartInfo = myProcessStartInfo;

    // Create a outlook
    myProcess.Start();
    createProc();
}

This is another method that creates web browsers in case you wanna check mails through your browser interface..

C#
        // This is another procedure that creates an browser process
                
private void createIEProc(String site)
{
    Process myProcess = new Process();

    // Get the process start information of iexplore
    ProcessStartInfo  myProcessStartInfo = 
         new ProcessStartInfo("iexplore.exe",site);

    // Assign 'StartInfo' of outlook to 'StartInfo' of 'myProcess' object.
    myProcess.StartInfo = myProcessStartInfo;

    // Create a outlook
    myProcess.Start();
}

The Methods in the MailDomain class stores the user response , options of the mail provider sites to open.  Here is a sample code illustrating reading and writing XML Data into a file and from a file. This how xml files are read.

C#
private void MailDomain_Load(object sender, System.EventArgs e)
{
XmlTextReader reader = new XmlTextReader ("maillist.xml");
String list ="";
if(!reader.EOF)
{
while (reader.Read())
{
    switch (reader.NodeType)
    {
    case XmlNodeType.Element: // The node is an Element
        while (reader.MoveToNextAttribute()) // Read attributes
        {
            MessageBox.Show(reader.Value);
            list=reader.Value+","+list;
        }
        break;
    case XmlNodeType.DocumentType: // The node is a DocumentType
        break;
    default:
        break;
    }
}
    .......
reader.Close();
}
else
{
// Do Something else
}
}

This piece of code demonstrates writing into XML Files.

C#
private void updateXMLdoc()
{
String []tok;
            
tok = textBox1.Text.Split(',');
            
            
if(textBox1.Text!="")
{
    XmlTextWriter xtw= new XmlTextWriter ("maillist.xml", null);
    xtw.Formatting = Formatting.Indented;
    xtw.WriteStartDocument(false);
    xtw.WriteDocType("maildomain", null, null, null);
    xtw.WriteComment("This file represents the mail sites the user uses");
    xtw.WriteStartElement("Data");
    
    for(int i=0;i<tok.Length;i++)
    {
        xtw.WriteStartElement("site", tok[i]);
        xtw.WriteEndElement();
    }
    xtw.WriteEndElement();
            
    //Write the XML to file and close the xtw
    xtw.Flush();
    xtw.Close();
}
}

Update

Sample Image - MailForm.jpg

I added faculties to send emails using outlook from the present version. It uses the outlook client and it requires to interop the outlook dll. Use tlbimp.exe to convert the msoutl9.olb and covert into a .dll. Include the reference in to the project. The following code performs sending e mails via Outlook.
C#
try
{
//Return a reference to the MAPI layer
oApp = new Outlook.Application();
oNameSpace= oApp.GetNamespace("MAPI");
// Logs on the user

oNameSpace.Logon(null,null,true,true);
//gets defaultfolder for my Outlook Outbox
oOutboxFolder = oNameSpace.GetDefaultFolder(
    Outlook.OlDefaultFolders.olFolderOutbox);
Outlook._MailItem oMailItem = (Outlook._MailItem)oApp.CreateItem(
    Outlook.OlItemType.olMailItem);      
         
oMailItem.To = textBox1.Text;
oMailItem.Subject = textBox2.Text;
oMailItem.Body = textBox3.Text;

oMailItem.SaveSentMessageFolder = oOutboxFolder;
         
//The draft is saved
oMailItem.Save();

//adds it to the outbox
oMailItem.Send();
            
if(oMailItem.Sent || oOutboxFolder.UnReadItemCount!=0)
oOutboxFolder.MoveTo(oNameSpace.GetDefaultFolder(
    Outlook.OlDefaultFolders.olFolderSentMail));
this.Close();
//this.Refresh();
}
catch
{
this.Dispose();
}

Conclusion

This article explains the creation of process, use processinfo and create process specified by name or sysID.  This article demonstrates the creation of a system tray application. This application also demonstrates the reading and writing of XML Data . This application demonstrates using MSOutlook to send mails.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Ragavendran was born in South India. He holds a Bacnelor of Computer Science and Engineering. Currently he works as a Software Engineer. He is techno Savvy and All he wishes to do in life is to get to know a lotta things....
He is always intrigued about learning new things.

His skills include C,C++,Java ,.NET.He also experience working in Legacy Systems. He feels most comfortable with C++ and Java.
Computers are his first love and he likes reading books, music and trekking.

He is a voracious reader and loves reading books. A Leo by birth, he loves trekking and gardening. One of his main ambitions is to write his autobiography, though he is not sure who will read it.

Comments and Discussions

 
QuestionContextMenu Pin
BMWABCD21-Jul-07 15:51
BMWABCD21-Jul-07 15:51 
GeneralPreventing Alt-Tabbing to the main form Pin
CoffeeZombie24-Nov-04 10:54
CoffeeZombie24-Nov-04 10:54 
GeneralRe: Preventing Alt-Tabbing to the main form Pin
CoffeeZombie25-Nov-04 12:02
CoffeeZombie25-Nov-04 12:02 
GeneralRe: Preventing Alt-Tabbing to the main form Pin
CoffeeZombie29-Nov-04 22:49
CoffeeZombie29-Nov-04 22:49 
GeneralRe: Preventing Alt-Tabbing to the main form Pin
moerssl6-Feb-07 23:21
moerssl6-Feb-07 23:21 
GeneralRe: Preventing Alt-Tabbing to the main form Pin
Marcelo Calado5-Jul-08 8:14
Marcelo Calado5-Jul-08 8:14 
GeneralAnother Update Pin
Ragavendran Vaidhyanadhan26-Aug-02 0:00
Ragavendran Vaidhyanadhan26-Aug-02 0:00 
GeneralDemo Project link Updated Pin
Ragavendran Vaidhyanadhan19-Aug-02 20:22
Ragavendran Vaidhyanadhan19-Aug-02 20:22 
Generallink for demo Pin
Stephane Rodriguez.16-Aug-02 22:26
Stephane Rodriguez.16-Aug-02 22:26 
GeneralRe: link for demo Pin
James T. Johnson17-Aug-02 4:15
James T. Johnson17-Aug-02 4:15 
GeneralRe: link for demo Pin
19-Aug-02 7:45
suss19-Aug-02 7:45 
GeneralRe: link for demo Pin
Ragavendran Vaidhyanadhan18-Aug-02 19:05
Ragavendran Vaidhyanadhan18-Aug-02 19:05 
GeneralRe: link for demo Pin
19-Aug-02 7:43
suss19-Aug-02 7:43 
GeneralRe: link for demo Pin
Ammar20-Aug-02 1:44
Ammar20-Aug-02 1:44 
GeneralRe: link for demo Pin
Ragavendran Vaidhyanadhan20-Aug-02 3:30
Ragavendran Vaidhyanadhan20-Aug-02 3:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.