Welcome

Welcome Anonymous

Thursday, June 24, 2010

How to Read Mails from Outlook and Checking for particular User Mail

Microsoft.Office.Interop.Outlook .Application oApp;
Microsoft.Office.Interop.Outlook._NameSpace oNS;
Microsoft.Office.Interop.Outlook.MAPIFolder oFolder;
Microsoft.Office.Interop.Outlook._Explorer oExp;

oApp=new Microsoft.Office.Interop.Outlook.Application();
oNS = (Microsoft.Office.Interop.Outlook._NameSpace)oApp.GetNamespace("MAPI");
oFolder=oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.
olFolderInbox);

oExp = oFolder.GetExplorer(false);
oNS.Logon(Missing.Value, Missing.Value, false, true);
Microsoft.Office.Interop.Outlook.Items items = oFolder.Items;

//checking for unread mails

foreach (Microsoft.Office.Interop.Outlook .MailItem mail in items)
{

if (mail.UnRead == true)
{

//Paste the code for already opened mails

if (mail.SenderName.Trim() == "crozzX")
{
//Paste the code for a particular User

}
}
else
{
//Paste the code for already opened mails
}
}

How to Turnoff / Restart Computer using C#.NET

Import System.Diagnostics(using System.Diagnostics)in code behind file

Then in shutdown button onclick event write

Process.Start("shutdown", "/s /f");/Force Shutdown
Process.Start("shutdown", "/s /t");/Normal Shutdown
Process.Start("shutdown", "/r ");/Force Restart


Application.Exit();

How to store Excel data in Gridview

Import System.Data.OleDb(using System.Data.OleDb)in code behind file

Then in button onclick event write,

OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=D:\status.xls;Extended Properties=Excel 8.0");
OleDbCommand command = new OleDbCommand("SELECT * FROM [Sheet1$]", connection);
OleDbDataReader dr;

connection.Open();
dr = command.ExecuteReader(CommandBehavior.CloseConnection);

DataTable excelData = new DataTable("ExcelData");
excelData.Load(dr);
GridView1.DataSource = excelData;
GridView1.DataBind();