By Bill Farley
This program puts together all the pieces of the previous mail handling examples to respond
to requests for and send out stock quotes. The program will periodically check your mail for
incoming email requests for a stock quote. For any requests, the stock quote is retrieved,
and a reply message is sent to the requestor. The real value of this technique is not so
much for computer-computer requests, but for requests from wireless devices or cell
phones.
Download The Source Code
POP3.cs
QuoteResponder.cs
SMTPMailer.cs
message.cs
quoter2.cs
The retrieval of the quote is handled by the quoter2 class, which is a much improved
version than my previous. Quoter uses a service which returns quotes as a csv string, which
greatly simplifies processing versus parsing thru html. The Timer class is used to generate
the mail check event. The setting for the frequency of checking for messages is set to 30
secs, but you can use any value you like. When the timer times out, the POP3 class is used
to retrieve all the messages with "QUOTE" as the subject. They come back as an ArrayList
of message objects. The list is traversed and quotes are retrieved for each, reply messages
created and mailed using SMTPMailer. The POP3 class and SMTPMailer classes control the
access to your mail server to download messages and mail responses.
The request email must have "QUOTE" in caps without the quotes as the subject, and the
text of the message should be the stocks symbol only. The response message contains the
stock name, symbol, current value, and percent change. Actually there is much more
information returned in the csv string which could be useful, but for this project I just used
the basics.
You must fill in your own server and account information and recompile:
POP3 pop = new POP3("popserver", "user", "password"); Enter your pop server name, user
name, and password
SMTPMailer mlr = new SMTPMailer("smtpserver", "host"); Enter your smtp server name and
host name.
resp.From = "bfarley@cfl.rr.com"; Enter your email address
To use, run the QuoteResponder on one machine and send an email from another or from a
wireless device.
How To Compile?
Download the compile command file - compile45.txt
The compilation instructions for the other files are contained in the files themselves.
//Source Code
using System;
using System.IO;
using System.Timers;
using System.Collections;
class QuoteResponder
{
public static void Main()
{
Timer tmr = new Timer();
Tmr.Tick += new EventHandler(OnTimedEvent);
tmr.Interval= 120000;
tmr.Enabled = true;
Console.WriteLine("Quote Responder running...");
while(true){}
}
public static void OnTimedEvent(object source, EventArgs e)
{
try
{
POP3 pop = new POP3("popserver", "user", "password");
ArrayList newmsgs = pop.GetNewMessages("QUOTE");
IEnumerator msgenum = newmsgs.GetEnumerator();
while (msgenum.MoveNext() ){
Message req = (Message)msgenum.Current;
Console.WriteLine("Request From = " + req.From);
Quoter qtr = new Quoter();
SMTPMailer mlr = new SMTPMailer("smtpserver", "host");
Quote q = qtr.GetQuote(req.Body.Trim() );
Message resp = new Message();
resp.From = "bfarley@cfl.rr.com";
resp.To = req.From;
resp.Subject = q.Name;
resp.Body = q.ToString();
mlr.Send(resp);
Console.WriteLine(q.ToString());
}
Catch(Exception e)
{
Console.WriteLine(e.ToString() );
}
}
}
//End Code