Archive for September, 2008

C#: Send Email with SMTP

September 30th, 2008

So I’ve actually had to do this before for a project I was working on, and thought that I’d go ahead and post a brief example of how to accomplish it here.

This example sends a simple email to a single recipient by using GMail. Please note that the principle classes used (MailMessage, SmtpClient, and NetworkCredential) are only available in the .NET Library 2.0 or later. For .NET < 2.0 you’ll want to see the System.Web namespace.

// Create the mail object.
System.Net.Mail.MailMessage msgMail =
    new System.Net.Mail.MailMessage(
        "TestEmail@mel-gree.com",   // FROM
        "mel@mel-green.com"         // TO
    );
 
// Set the subject of the message
msgMail.Subject = "Hello from C#";
 
// Set that the body of this message will be HTML
msgMail.IsBodyHtml = true;
 
// Set the body of the email. Since I specified that
// I'm using HTML this will be fully qualified HTML/XHTML
msgMail.Body =
    "" +
      "" +
        "
<h4>Hello There!</h4>
" +
        "
 
 You've been selected to receive this email." +
            "Congratulations!
 
" +
      "" +
    "";
 
// Now I'll create an SMTP object to send the message
// by means of a SMTP server.
System.Net.Mail.SmtpClient smtp =
    new System.Net.Mail.SmtpClient(
            "smtp.gmail.com", // Server address
            587               // Server port
        );
 
// GMail uses SSL
smtp.EnableSsl = true;
 
// GMail uses authentication
smtp.UseDefaultCredentials = false;
smtp.Credentials
    = new System.Net.NetworkCredential (
        "mastermel@gmail.com", // Account Name
        "MyPassword"           // Account Password
      );
 
// Finally, send the message!
smtp.Send(msgMail);

Here’s a few extra things you might want to know about…
» Read more: C#: Send Email with SMTP

C#: Retrieve data from webpage

September 29th, 2008

So I came across a fun assignment this week that I’m sure has been done by many different people in many different programming languages. The challenge was to “scrape” a website for information autonomously and save it off to a file.

I accomplished this by first using a wrapper class for .NET’s own HTTPWebRequest object that simplified posting to a web site and retrieving the result. I then used regular expressions to find the data I wanted, stored it in a string, and later wrote it to a file.

I’m not going provide the specific program I wrote as it’s still proprietary, but I will give a small example of how this can be done. The example will include: posting to a website, retrieving the results (HTML for the page), and parsing the resulting page to find what you want.

The class I used to post to the site was done by Robert May and can be found here: http://geekswithblogs.net/rakker/archive/2006/04/21/76044.aspx

Here is an example of using this class to perform a search at CraigsList under the ‘for sale’ category and retrieving the results:

// Create the post object
PostSubmitter post =
    new PostSubmitter("http://provo.craigslist.org/search/sss");
 
// Add our parameters
post.PostItems.Add(
    "query",
    "Ford Truck"
);
 
// Specify our action type (Post | Get)
post.Type = PostSubmitter.PostTypeEnum.Get;
 
// Retrieve the results
string result = post.Post();

» Read more: C#: Retrieve data from webpage

Green Sudoku, a C# App

September 27th, 2008

So I’ve written a small Sudoku game in C#. I originally started writing it so that I could have an interface to test my Sudoku Solving Algorithm. But as I worked on it, it became more and more a nice little application worthy of its own existence.

It only has 3 puzzles hard-coded into it that I’ve been using for testing purposes, but they’re fun to play, even though they’re a little on the easy side. My plan is to update the game to automatically download puzzles from some of the major Sudoku game websites that are out there. This would allow you to play literally millions of puzzles from several different levels of difficulty.

So I post it here for your puzzle-solving pleasure! Please be patient with it as it has a few bugs I’m still working out. If you happen to find any please let me know by commenting in this post!

This link will provide you with a direct download to a zip file that contains the game and a few files it needs to run. After downloading it just extract it anywhere you’d like and enjoy!

*GreenSudoku.zip (360KB)

Green Sudoku Screenshot

Green Sudoku Screenshot

*Note: You need to have .NET framework version 2.0 or later installed on your system. If you’re not sure whether you have it you can download and run this small program from Microsoft which will install it if it’s not present on your system.

C#: Regular Expressions

September 27th, 2008

I’ve decided to document what little knowledge I have on using Regular Expressions in C#. Nothing grand, just a list of formats, special characters and usage.

Control Characters:

Character Matches
. Any character but the newline (\n)
$ Characters at the end of a string
^ Characters at the beginning of a string. Also used in conjunction with ‘[]’ to specify “not.”
+ One or more of the specified characters
* Zero or more of the specified characters
? Zero or One of the specified characters
\ Used to escape special characters as well as signify special character sets
( ) Used to specify a collection of characters to match
[ ] Used to specify a set of single characters or ranges to match
{ } Used to specify how many times to match a given character(s)
| Used as a logical OR. Allows one or more expressions to be selected for a match

Special Character Sets:

Character Matches
\w Any word character. Same as [A-Za-z0-9_]
\W Any non-word character. Same as [^A-Za-z0-9_]
\s Any whitespace character. Same as [ \t\v]
\S Any non-whitespace character. Same as [^ \t\v]
\d Any digit. Same as [0-9]
\D Any non digit. Same as [^0-9]

» Read more: C#: Regular Expressions

Spore, suprisingly entertaining

September 25th, 2008

So about a week ago my wonderful wife purchased me the latest video game title to splash onto the market, Spore. Although the game was announced over a year ago as the next big simulation franchise from the creative genius of Will Wright, and seemed to be receiving the praises from many a person I know as well as some good scores from online review sites, I admit I had my reservations.

Nevertheless I acquired it (by legal means this time, which is unusual for me). I was hesitant to just install it on my computers due to some discussions I’ve read concerning its use of nasty, nasty DRM software such as SecuROM. I’ve also read ill-reports of EA’s new method of controlling their software by limiting the number of installations possible from a single product key. This made me curious if there was an alternative.

So I did a little looking around and sure enough there was already a pirated image of the install disk that supposedly came without the DRM and without the install cap. This I downloaded and used to install on my gaming rig and laptop, being sure to include EA’s download manager so that I wouldn’t have go through the pain of manually keeping the game up to date and so that I could fully enjoy the benefits of Spore’s use of online shared content.

After a brief amount of setup and configuration I created a new game and entered the world as a small, single-celled organism equipped with a beak, a flagellum, and a pair of eyes. I admit the eyes were kinda silly, but they served to introduce me to the unique, almost cartoony, universe of spore. I quickly progressed through the cell stage, followed by the creature stage and arrived at the tribal stage with a feeling that I was finally getting the handle on this game; after all I was at the top of the food chain and was able to send any wobbly legged or googly eyed creature Maxis could throw at me running for their lives. » Read more: Spore, suprisingly entertaining