Description
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.
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 is improved from previous versions to more reliably retrieve the parts of the message. 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 = "[email protected]"; Enter your email address To use, run the QuoteResponder on one machine and send an email from another or from a wireless device.
Requirement
Requires .NET SDK
How To Compile?
csc /r:System.dll /r:System.Net.dll /r:System.IO.dll /r:System.Timers.dll /r:quoter2.dll
/r:SMTPMailer.dll /r:message.dll /r:POP3.dll QuoteResponder.cs
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= 30000;
tmr.Enabled = true;
Console.WriteLine("Quote Responder running...");
while(true){}
}
public static void OnTimedEvent(object source, EventArgs e)
{
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 = [email protected];
resp.To = req.From;
resp.Subject = q.Name;
resp.Body = q.ToString();
mlr.Send(resp);
Console.WriteLine(q.ToString());
}
}
}