I code web or Windows applications for fun—or even for profit, but it's usually still fun too. As part of this, I've acquired a small collection of code samples that others might find useful, which I'm sharing here.
This page will eventually be better-organized, but I thought it would be useful to have such things on the internet sooner rather than later, instead of worrying about perfecting a nice layout/syntax highlighting/etc. setup.
Any code here can be used by you wherever, for whatever purposes, with no restrictions. If you feel like giving me credit, that'd be great; if not, that's fine too.
- Sending Email in C# through Gmail
-
There's a lot of misinformation out there about how to send email in C# (or generally in .NET) through Gmail. Part of the confusion, I think, stems from the fact that .NET 1.0 had this atrocious System.Web.Mail namespace, which is now depracated on account of sucking too much, and doesn't work with Gmail very well at all.
Here's a code file demonstrating how to send email using Gmail in C# 3.0. It even includes programmatically sending email from one Gmail account to another, without transmitting credentials—useful, e.g., for Windows applications, since decompilation would then reveal the credentials to the world.
GmailSample.cs
- C#
WebCrawler Class
-
Many of my projects have required me to crawl the web and scrape information from various sites, while keeping track of the cookies involved. The usual .NET WebClient class does not provide this crucial cookie functionality, and so it is fairly useless for sustained web-crawling of many sites.
My WebCrawler class provides a robust, easy-to-use object representing a web-crawling session. Once instantiated, it will keep track of the results of all requests using an internal cookie store, so that all calls to this instance's SendRequest method look as if they are subsequent calls from a real, human user.
WebCrawler.cs (XML documentation included)
- Google Analytics in ASP.NET
-
Finding a robust way of integrating Google analytics into ASP.NET, especially with all of the ASP.NET AJAX JavaScript trickery flying around, took me a little while.
Thus, I share my code with you.
The main idea is that every page on your site derives from a new base class (you might already have created one of these), which handles the process of injecting Google analytics script references and inline script.
This can be made to work seamlessly using Web.config, by setting the pageBaseType attribute of your <pages /> element.
Thus this solution isn't tied to any specific master page, which is another route you might think to take.
GoogleAnalyticsInAspDotNet.cs (heavily commented)
- My C# Extension Methods
-
Everyone has one: a collection of extension methods to make life easier. Besides some simple formatting helper methods, we have a series of extension methods on the Control class to make using Invoke andBeginInvoke much less painful, plus some extensions of Form to make modal messageboxes a little easier to code.
Extensions.cs (XML documentation incomplete)