SIGN UP MEMBER LOGIN:    
ARTICLE

Stock Quoter

Posted by Bill Farley Articles | Financial Applications April 16, 2001
This program will retrieve a stock quote from a website and automatically forward it to an email at any frequency you would like.
Reader Level:
Download Files:
 

Description

This is a follow up to my last SMTP example that adds a new twist for a practical use of email from within an application. This program will retrieve a stock quote from a website and automatically forward it to an email at any frequency you would like. The functionality of this example is primitive, but this could easily be built into a full featured server that supported many customers with customized stock lists. This program also demonstrates the use of the .NET Timer object. The Timer object lets you define an event handler that will execute every time the Timer times out.


The Quoter class does the work of retrieving the quote from the web. The class has a method called GetQuote(string stock) that uses an HttpWebRequest object to connect to the site. The stock string parameter is appended to web address. The html returned by the web request is read in one line at a time and parsed to find the appropriate information. The Quote class also overrides the ToString() method to return a string representing the quote to embed as the body of the email message. The info is returned in a Quote object, which is a simple wrapper for the stock quote information The QuoteTest class is a simple demonstrator. It implements the Timer object and defines the event handler. The program then goes into an infinite do-nothing loop waiting for the Timer to time out. When the Timer times out, the event handler gets a new quote, formats an email message, and sends it. You will need to replace the parameters in the SMTPMailer constructor with your own appropriate smtp server name and your host name. This will continue until you Ctrl-C it. The frequency can be set by changing the Timer Interval property. In this example, it is set to 10 minutes.

NOTE: This should be used for demonstration purposes only. Scouring information off a website in this manner most always will violate the site's Terms Of Use. 

Requirement

Requires .NET SDK

How To Compile?

QuoteTest: csc /r:System.dll /r:System.Net.dll /r:System.IO.dll /r:System.Timers.dll /r:Quoter.dll /r:SMTPMailer.dll /r:message.dll QuoteTest.cs
Quoter: csc /r:System.dll /r:System.Net.dll /r:System.IO.dll /target:library quoter.cs
SMTPMailer: csc /r:System.Net.dll /r:System.IO.dll /r:message.dll /target:library SMTPMailer.cs
Message: csc /target:library Message.cs
Compile in this order: Message, SMTPMailer, Quoter, QuoteTest

Source Code

using System;
using System.IO;
using System.Timers;
class QuoteTest
{
public static void Main()
{
Timer tmr =
new Timer();
tmr.Tick +=
new EventHandler(OnTimedEvent);
tmr.Interval= 600000;
tmr.Enabled =
true;
Console.WriteLine("Quote Test running...");
while(true){}
}
public static void OnTimedEvent(object source, EventArgs e)
{
Quoter qtr =
new Quoter();
SMTPMailer mlr =
new SMTPMailer("SMTP Server Name", "Host Name");
Message msg =
new Message();
msg.From = bfarley@cfl.rr.com;
msg.To = bfarley@cfl.rr.com;
msg.Subject = "Quote for TWCUX";
Quote q = qtr.GetQuote("twcux");
msg.Body = q.ToString();
mlr.Send(msg);
Console.WriteLine("Quote has been sent...");
}
}

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor