Use C # To create an email notification program with genie

Source: Internet
Author: User
Tags getstream microsoft website

Visual C # is the next-generation programming language launched by Microsoft and an important part of the Microsoft. Net Framework. Microsoft's. Net platform is a new generation Internet platform. For technical developers ,. the two major features of the Net platform are remarkable. The first is the public language runtime provided by the platform, that is, the public language runtime platform. On the other hand, it is its large and comprehensive unified programming class. Visual C # implements many powerful functions by calling these classes and objects.

In Internet applications, email has always taken an important place. There are a lot of e-mails on the Internet every day. We need to spend time sending and receiving emails every day, but sometimes we cannot receive one email, but sometimes many. It would be nice if we could do an email notification program. This article introduces how to use C # as an email notification program, and also shows you some features of C # programming.

I. Introduction:

This email notification program is a TCP/IP program used to notify the user of new emails on the mail server. The program also has an genie to notify users of the specific number of new mails, so the user interface is relatively novel. The program automatically connects to the mail server every five minutes to check new emails. You can also configure the program: set the time interval, set the mail server address and port number, and set the user name and password. This program is still in the system tray, so it runs in the background.

2. engineering steps:

1. Create a C # Windows application project under VS.net and name it "EmailNotifier ".

Figure 1


2. design the main interface and the wizard interface:

Set the ShowInTaskbar attribute of the main form to false, the FormBorderStyle attribute to FixedDialog, and the Text attribute to "configuration parameter ".

Add five Label controls, five TextBox controls, two Button controls, and one system tray Control to the main form) A context menu Control and a Timer Control ).

Set the attributes of each control: Set the Text attributes of the five labels to "mail server address:", "server port:", "User name:", and "password: "," Time Interval (MS): "; set the Name attribute of the five text boxes: "ServerAdd", "ServerPort", "Username", "Password", and "TimeSpan"; set the Text attribute of the first four Text boxes to null, set the last one to 300000 (because the default interval is 5 minutes), and set the PasswordChar attribute of the fourth text box to "*". in this way, the password is hidden. The Text attribute of the button control is set to "hide" and "check email". The Icon attribute of the system tray is set to an Icon file; add three sub-items "configuration parameters", "check email", and "exit" under the context menu ".

The main interface has been designed, as shown in the figure below:

Figure 2

Then design the wizard interface.

The genie we use is created with Microsoft Agent, which is completely based on COM, so we can use it easily. For more information, see the documents on the Microsoft website. The website is:Http://www.microsoft.com/msagent/downloads.htm. Download the following from there:

(1) Microsoft Agent core establishment

(2) Microsoft Agent Genie: Genie, Merlin, Roby, and Peedy)

(3) There must be at least one Text-to-Speech engine in English (Chinese characters cannot be found yet)

(4) API4.0a

With this, we can apply this ActiveX control in any of our programs.

Add a new form Form2, as shown in the figure below:

Figure 3

On the newly added form, we need to use the Microsoft Agent control, so we will import it to the project, as shown in the figure below:

Figure 4

Now we have corresponding controls in our toolbox, so we can use them directly. (This control is invisible when the program is running)

(If you are interested, go to another article on Microsoft Agent controls:Http://www.computerworld.com.cn/htm/app/aprog/01_12_29_2.aspSee)

3. Important code:

In the code section of the main form, because we need to use network and network stream objects, we must first Add the following important Namespace ):

Using System. Net;

Using System. Net. Sockets;

Using System. IO;

Using System. Text;

Next, add a NetworkStream object to our class to control the stream in the network.

Private NetworkStream netStream;

Add the following code to the constructor of this class to set the clock interval and make the program invisible at the beginning, instead of the system tray.

Timer1.Interval = Int32.Parse (TimeSpan. Text); // set the interval

This. Opacity = 0; // make the form invisible

We use an important function EmailCheck (). This function first creates a new TcpClient object for network connection, and uses a try-catch to connect to the email server entered by the user. If the connection is successful, use the netStream object to get the network stream: netStream = tcpClient. GetStream (); finally, the information is sent and obtained. The function is as follows:

Private void EmailCheck ()

{

File: // newCreate a TcpClient object to establish a connection

TcpClient tcpClient = new TcpClient ();

Try

{

TcpClient. Connect (ServerAdd. Text, Int32.Parse (ServerPort. Text ));

}

Catch

{

MessageBox. Show ("cannot connect to the Host:" + ServerAdd. Text + "and port:" + ServerPort. Text );

}

File: //The email server obtains the corresponding

NetStream = tcpClient. GetStream ();

If (netStream = null)

{

Throw new Exception ("the obtained network stream is null. ");

}

String returnMsg = ReadFromNetStream (ref netStream );

CheckForError (returnMsg );

File: // sendSent username information

WriteToNetStream (ref netStream, "USER" + this. Username. Text );

ReturnMsg = ReadFromNetStream (ref netStream );

CheckForError (returnMsg );

File: // sendSend password information

WriteToNetStream (ref netStream, "PASS" + this. Password. Text );

ReturnMsg = ReadFromNetStream (ref netStream );

CheckForError (returnMsg );

Stat ();

NetStream. Close ();

TcpClient. Close ();

}


 

The preceding functions include ReadFromNetStream (), WriteToNetStream (), Stat (), and checkForError. ReadFromNetStream () is used to read information from the network stream, with a parameter of the ref NetworkStream type; WriteToNetStream () is used to write information to the network stream, with two types of networkref stream, string parameter; Stat () is a function used to report information to the user after the new mail is checked; checkForError () is an error, with a String type parameter. The four functions are as follows:

///

/// This function is used to display the number of new mails

///

Public void Stat ()

{

WriteToNetStream (ref netStream, "STAT ");

String returnMsg = ReadFromNetStream (ref netStream );

CheckForError (returnMsg );

File: // setSeparate the total number of mails from the email size

String [] TotalStat = returnMsg. Split (new char [] {});

Int count = Int32.Parse (TotalStat [1]);

Int totalSize = Int32.Parse (TotalStat [2]);

File: // callUse the genie to inform users of the number of new mails

Form2 agent = new Form2 (count );

}

///

/// This function is used to write data to the network stream.

///

///

///

Private void WriteToNetStream (ref NetworkStream NetStream, string Command)

{

String stringToSend = Command + "";

Byte [] arrayToSend = Encoding. ASCII. GetBytes (stringToSend. ToCharArray ());

NetStream. Write (arrayToSend, 0, arrayToSend. Length );

}

///

/// This function is used to read data from network streams.

///

///

///

Private String ReadFromNetStream (ref NetworkStream NetStream)

{

StringBuilder strReceived = new StringBuilder ();

StreamReader sr = new StreamReader (NetStream );

String strLine = sr. ReadLine ();

While (strLine = null | strLine. Length = 0)

{

StrLine = sr. ReadLine ();

}

StrReceived. Append (strLine );

If (sr. Peek ()! =-1)

{

While (strLine = sr. ReadLine ())! = Null)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.