loads of useful information, examples and tutorials pertaining to web development utilizing asp.net, c#, vb, css, xhtml, javascript, sql, xml, ajax and everything else...

 



Advertise Here
C-Sharpener.com - Programming is Easy!  Learn Asp.Net & C# in just days, Guaranteed!

Simple Mail Classes - Easiest way to send email and/or gmail via asp.net

by naspinski 2/3/2008 3:47:00 AM

two classes that you customize and put into your App_Code folder which will allow you to send email with less code and greater ease than ever before

I have all sorts of programs that need to send email, whether its my own project (I use gmail) or a work project (local smtp). I got tired of what was essentially the same code filling up large chunks of my codebehinds.  I had error pages emailing our dev team, registration pages emailing event coordinators, and things such as that - that can turn out to be a lot of code; not even to mention if something needs to be changes on any sort of scale.  So, I made a couple classes that make it even easier to send mail throughout my apps, and this is all you need for code to send an email:

email message = new email();
message.send()
or, for gmail:
gmail message = new gmail();
message.send()

It is really that easy, you don't even need to include System.Net.Mail; in your page.  But that is using the default settings which you set the first time you set the code, it is just as easy to define many other aspects of the emails, such as to, from, subject, body, etc.  But the beauty comes with the default settings so you code can be as clean as possible.

 

Say you usually send your system emails from the address System@awesomeCo.com , the subject/body is usually "System Error", the recipient is most often webmasters@awesomeCo.com, and your SMTP will likely always be the same (as I would assume it would) - simply set that as your default_from, default_subject, default_body default_to, and default_SMTP to those values, and you will need nothing other than the above code to send an email.  But say on the same app, you want to send something to customerservice@awesomeCo.com you want the subject to be "complaint" and the body to be something you collected from a TextBox (txtComplaint) control in your code - that is no problem at all

email message = new email("customerservice@awesomeCo.com", "complaint", txtComplaint.Text);
message.send();

All the other default settings will still take place unless you override them, and all settings can be overwritten.

 

There is also a few methods added like .addTo(string emailAddress) and .addCC(string emailAddress) which do exactly what you would think they do.  The gmail class works exactly the same as the email class.

 

It's no coding breakthrough, but it is convenient!

 

 



kick it on DotNetKicks.com

Related posts


Comments are closed