Wednesday, 10 November 2010

Embedded Images in Outlook using C#

Embedded Images in Outlook using C#
To monitor all the Host and services we are using Nagios application.
So at some point we felt that why not we should have some kind of application which will take the screen shot of this Nagios with different hosts and send all the screenshots embedded in to outlook instead of as attachment to a group of user.
or
There was an requirement in my project where I need to take the screen shot of the website at regular interval and embedded all the images in outlook as send that email to group of people.
To achieve above requirement I used WATiN ,C# and outlook instance.
Below is the Rough idea how I have done that:
1. Open the Nagios URL using WATiN
2. Login to the web site Uisng WATiN
3. Navigate the Host webpage
4. Take the Screen shot the web page and store at local drive
5. Move through all the host web page and take the screen shot using WATiN
6. Embedded in to Outlook instance and send that email.



Below is the code snippet which I have used to Embedded the images in outlook:

public void Sendemail(string mainUrl,string strToEmail, string[] strFileLocationForAttachamnet)

{

try

{
// Create the Outlook application.

Outlook.Application oApp = new Outlook.Application();

Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem

Outlook.OlItemType.olMailItem);

// Set HTMLBody.

string emailSubject = "**** Auto Generated Email:Nagios Screen Shot Update for

" + mainUrl + " ****";

String sDisplayName = "MyAttachment";

int iPosition = 50;// (int)oMsg.Body.Length + 1;

int iAttachType =(int)Outlook.OlAttachmentType.olByValue; \

string htmlStart ="<html><body><h5><Font

Color=Purpel>Hi,<br/>Please Find below The screen shot of Host

Status as of "+DateTime.Now +"<br/></h5>"; string body=string.Empty;

int i = 1;

foreach (string filelocation in strFileLocationForAttachamnet)

{

if (filelocation != null)

{

Outlook.Attachment oAttach = oMsg.Attachments.Add(filelocation, iAttachType, iPosition,

sDisplayName);

body += "<h4>["+i+"."+"]</h4><img

src=\"cid:" + oAttach.FileName

+"\" /><br/>";

i++;

}

}

string wholeBody = htmlStart + body + "<h5><Font

Color=Purpel>Note:if no Red color means none of the services are

in critical stage.<br/>Regards,<br/>Md.

Jawed<br/>(jawed.md@hp.com)

lt;br/></h5></body></html>";

oMsg.HTMLBody = wholeBody;
// Set the subject.

oMsg.Subject = emailSubject;

// Add a recipient.

Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;

Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(strToEmail); oRecip.Resolve();
// Send

oMsg.Send();

// Clean up.

oRecip = null;

oRecips = null;

oMsg = null;

oApp = null;

}

catch (Exception ex) {
}
}

Thanks,
Md. Jawed

Thursday, 1 April 2010

Read data from XML and store into Data Set Using C#

I am using XML file as config file in my automation tool which I have developed to store all the inputs/settings/run time config value etc etc. Because it easy for manual guys also to just open XML file in Notepad and alter the setting based on their requirement and it’s easy to maintain also.

Below is the code which I am using to Read data from XML file and store in to Data set for further use.

//Local vaible to get xml path
string xmlPath=”Inputs.xml”;
//Read all the value to the xml read command
System.IO.FileStream fsReadXml = new System.IO.FileStream(xmlPath,
System.IO.FileMode.Open);
//Declare a dataset to hold all the XML value
DataSet ds = new DataSet();
try
{
//Read the value from XML reader to the dataset
ds.ReadXml(fsReadXml);

}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
//Close the reader.
fsReadXml.Close();
}
//your dataset (ds) would be having all the data stored from XML file.

Thanks,
Md.Jawed

Wednesday, 31 March 2010

Update the Excel sheet using c#

Some time I got the requiremnt where I need to update the excel sheet corresponding to testcases through the code whether the test case is failed or passed while automation is running and send the updated testcases excel with current status of testcases through the email at the end of automation code.
i hope that this would be very usefull for you.

Below is the code for that purpose :
----------------------------------------------------------------
//write to excel sheet
public void writeToExcelsheet(int rowNumber, string status)
{
try
{
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
using (DbConnection connection = factory.CreateConnection())
{
connection.ConnectionString = Program.excelSheetDeclaration;
using (DbCommand command = connection.CreateCommand())
{
command.CommandText =
"Update [sheet1$] Set Status =\"" + status + "\" WHERE TestCaseID ="+rowNumber;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
}
catch (Exception ex)
{
//
}
} //end of writeToExcelsheet method.
----------------------------------------------------
Thanks!!

Tuesday, 30 March 2010

How to Read data from Excel sheet using DataSet

While coding the Automation the situation came that we need to read data from excel sheet and store at one place to use that data through out the code and storing the data from excel sheet to the data set is a good option. Reading the data one by one from excel sheet would impact the performance of the automation code.
So below is the code which will read the data from excel sheet and would store in to dataset.
--------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Data.Common;
using System.Data.Sql;
//
namespace ReadFromExcel
{
//Class to read excel sheet and store in to dataset
class Program
{
static void Main(string[] args)
{
//Local variable to store the excelsheet location
string excelSheetLocation = "c:\\TestCases.xls";
//Local variable to store the connection string to talk with excel sheet
string excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelSheetLocation + ";Extended Properties=Excel 8.0";
//Declare a dataset
DataSet myDataSet=new DataSet();
//Declare oledb connection
OleDbConnection con = new OleDbConnection(excelConnectionString);
//Open the connection to communicat with excel sheet
con.Open();
//Create Dataset and fill with imformation from the Excel Spreadsheet for easier reference
OleDbDataAdapter myCommand = new OleDbDataAdapter(" SELECT * FROM [sheet1$]", con);
//filled the dataset with the data of excel sheet
myCommand.Fill(myDataSet);
//close the connection
con.Close();
//show the data on console window from dataset
//Total Number of rows in excel sheet
int totalRow = myDataSet.Tables[0].Rows.Count;
//trace through each rows
for (int i = 0; i < totalRow; i++)
{
//trace through each coloumn
for (int j = 0; j < myDataSet.Tables[0].Columns.Count;j++ )
{
//Show the data on to console window
Console.WriteLine(myDataSet.Tables[0].Rows[i][j].ToString());
} //end of for loop
} //end of for loop
} //end of main
} //end of class
} //end
----------------------------------------------------------------------------------------------
Thanks!!