Sending Email in .net 2005

Hi everyone…

Follwoing is very handy class to send emails using .net 2005.

It was created using existing .net class and namespace and it exposes all the functionality via public properties and functions.

public class SendEMail
{
#region Private Variable
private string _smtpServer;
private string _message;
private string _fileAttachment;
private MailMessage _mailMessage;
private MailAttachment _attachments;
#endregion
#region Public Properties
public string ToAddress
{
set { _mailMessage.To = value; }
get { return _mailMessage.To; }
}
public string CCAddress
{
set { _mailMessage.Cc = value; }
get { return _mailMessage.Cc; }
}
public string BCCAddress
{
set { _mailMessage.Bcc = value; }
get { return _mailMessage.Bcc; }
}
public string FromAddress
{
set { _mailMessage.From = value; }
get { return _mailMessage.From; }
}
public string SMTPServer
{
set { _smtpServer = value; }
get { return _smtpServer; }
}
public string Message
{
set { _mailMessage.Body = value; }
get { return _mailMessage.Body; }
}
public string Subject
{
set { _mailMessage.Subject = value; }
get { return _mailMessage.Subject; }
}
public string FileAttachment
{
set { _fileAttachment = value; }
get { return _fileAttachment; }
}
#endregion
#region Constructors
public SendEMail()
{
_mailMessage = new MailMessage();
}
#endregion
#region Public Function
public bool SendMail()
{
try
{
if (ToAddress == null || ToAddress == string.Empty)
{
return false;
}
if (Message == null || Message == string.Empty)
{
return false;
}
if (FromAddress == null || FromAddress == string.Empty)
{
return false;
}
if (SMTPServer == null || SMTPServer == string.Empty)
{
SMTPServer = “localhost”;
}
SmtpMail.SmtpServer = SMTPServer;
_mailMessage.BodyFormat = MailFormat.Html;
_mailMessage.Priority = MailPriority.High;
if (FileAttachment != null && FileAttachment != string.Empty)
{
_attachments = new MailAttachment(FileAttachment);
_mailMessage.Attachments.Add(_attachments);
}
SmtpMail.Send(_mailMessage);
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
}

keep smiling 🙂

Advertisements

Connect to ORACLE database using C#

Hi everyone…

you can connect to oracle database either by OleDb object or by DSN. Following code snippet did the trick. Go through it.

using System;
using System.Data;
using System.Data.ADO;
class OracleTest
{
static void Main()
{
//UID – User Id
//PWD – Password
//Server – Your Service name
//Using Microsoft ODBC for Oracle DSN-Less
//const string strConn = “Provider=MSDASQL;DRIVER={Microsoft ODBC for ORACLE};UID=scott;PWD=tiger;Server=fnqa”;
//Using Microsoft OLEDB Provider for Oracle
const string strConn = “Provider=MSDAORA.1;
Data Source=oracle_db;User ID=scott;Password=tiger”;
//Using DSN Connection
//const string strConn =”DSN=oracle_dsn;UID=scott;PWD=tiger” ;
const string strSQL = “Select * From EMP”;
try
{
DataSet ds = new DataSet(“EmployeesSet”);
ADODataSetCommand myCommand = new ADODataSetCommand(strSQL,strConn);
myCommand.FillDataSet(ds,”Buyer”);
Console.WriteLine(ds.Xml);
}
catch(ADOException e)
{
Console.WriteLine(“Exception Occured :{0}”,e.Errors[0].Message);
}
}
}

Courtesy Ravi Shankar

Keep Smiling 🙂

Export DataSet to Excel in C#

Hi Everyone…
I have been exporting data from dataset to excel using nested loops, excel object, accessing each cell of excel sheet and each cell of dataset and so on and so forth until I find this……

Before writing about it a special thanks to Peter A. Bromberg for writing such a nice article which really made at least my life easy….:). In his article he wrote a very simple function to export data without using excel object and other messy stuff. XSL Transformation is applied to dataset and XML for excel is generated.

Below is the complete function which will write an Excel file to your local disk. You have to pass it the DataSet to export and path to where file should be generated. 

public static void CreateWorkbook(DataSet ds, String path)
{
   XmlDataDocument xmlDataDoc = new XmlDataDocument(ds);
   XslTransform xt = new XslTransform();
   StreamReader reader =new StreamReader(typeof (WorkbookEngine).Assembly.GetManifestResourceStream(typeof (WorkbookEngine), “Excel.xsl”));
   XmlTextReader xRdr = new XmlTextReader(reader);
   xt.Load(xRdr, null, null);

   StringWriter sw = new StringWriter();
   xt.Transform(xmlDataDoc, null, sw, null);

   StreamWriter myWriter = new StreamWriter (path + “\\Report.xls“);
   myWriter.Write (sw.ToString());
   myWriter.Close ();
}

and thats it. Your DataSet has been exported to an Excel file which is saved at the path passed to this function.

For original article, please click here


Keep Smiling 🙂

Participation in ImagineCup 2007

ImageCup as you all know is world’s premier technical competition for students from Microsoft. This time two teams from my institute participated in that, though was unable to make it through to world wide final but atleast participated in local competition.

I want to congratulate the students of LUMS who were selected for finals which will held in South Korea, Soel and I also want to say hardluck to my fellows who tried their best to win but unfortunately ……. 😦 

Keep Smiling 🙂

Making HTTP Requests without a Browser

Hi everyone…

Here is way to make HTTP requests without using a browser. 

1.  Before starting add an reference to

using System.Net

2. After that following code snippet will save the html of any webpage which will be mentioned in url variable to a string named as webPageHtml.  

string url = “<you web pages url goes here>”;
WebRequest myWebRequest = HttpWebRequest.Create(url);
Stream myWebStream = myWebRequest.GetResponse().GetResponseStream();
StreamReader myReader = new StreamReader(myWebStream);
string webPageHtml = myReader.ReadToEnd();
myReader.Close();
Console.Write (webPageHtml);
myReader.Close ();

3. Thats it.


Keep Smiling 🙂

Downloading Files using C#/VB.NET

Hi Everyone… Downloading any type of file using http request is not a big at all using either  C# or Visual Basic.NET code. Please follow the steps: 

1. You need to add an reference 

using System.Net;  2. Now you can use the class named as WebClient, so just declare an object of this class and intialize using 0 argumnet constructor. 

WebClinet myClient = new WebClient (); 

3.  Now you can use the method DownloadFile of WebClient class to start downloading the file. 

//myClient.DownloadFile (<Http Address>,<Local Path>)

string httpAddress = http://msdn2.microsoft.com/en-us/library/system.net.webclient.downloadfile(vs.71).aspx;

string localPath = “C:\myDownload”;

myClient.DownloadFile (httpAddress, localPath);

4. Thats it…change the httpAddress of your desired file and localPatth. 

Keep Smiling 🙂

Getting Last Visited Page in PHP/JS

It is very common that we browse through no of pages and each visited page has the button which takes the user back to previous link.

Browser’s Back button is good option with static pages but for dynamic pages it doesn’t refreshes the page. So the better option is to

1. Either save the previous link and set that value to action of back button OR

2. Use the Javascript function “window.back()” which returns the last visited address.

th of these techniques not only takes you to the last visited link but will also refreshes page.

— 

Keep Smiling 🙂