using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
public static DataSet getDataSet()
{
SqlConnection sqlConnection = ""; //Connection Details
//select fields to mail example student details
SqlCommand sqlCommand = new SqlCommand("select Name,DOB,Email,Mob from Student", sqlConnection);
SqlDataAdapter sqlDataAdapter = new System.Data.SqlClient.SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
DataSet dataSet = new DataSet();
try
{
sqlDataAdapter.Fill(dataSet, "header");
sqlConnection.Close();
}
catch (Exception Ex)
{
System.Windows.Forms.MessageBox.Show(Ex.Message);
sqlConnection.Close();
return null;
}
return dataSet;
}
public static string getHtml(DataSet dataSet)
{
try
{
string messageBody = "<font>The following are the records: </font><br><br>";
if (dataSet.Tables[0].Rows.Count == 0) return messageBody;
string htmlTableStart = "<table style=\"border-collapse:collapse; text-align:center;\" >";
string htmlTableEnd = "</table>";
string htmlHeaderRowStart = "<tr style =\"background-color:#6FA1D2; color:#ffffff;\">";
string htmlHeaderRowEnd = "</tr>";
string htmlTrStart = "<tr style =\"color:#555555;\">";
string htmlTrEnd = "</tr>";
string htmlTdStart = "<td style=\" border-color:#5c87b2; border-style:solid; border-width:thin; padding: 5px;\">";
string htmlTdEnd = "</td>";
messageBody += htmlTableStart;
messageBody += htmlHeaderRowStart;
messageBody += htmlTdStart + "Employee Name" + htmlTdEnd;
messageBody += htmlTdStart + "DOB" + htmlTdEnd;
messageBody += htmlTdStart + "Email" + htmlTdEnd;
messageBody += htmlTdStart + "Mobile" + htmlTdEnd;
messageBody += htmlHeaderRowEnd;
foreach(DataRow Row in dataSet.Tables[0].Rows)
{
messageBody = messageBody + htmlTrStart;
messageBody = messageBody + htmlTdStart + Row["Name"] + htmlTdEnd;
messageBody = messageBody + htmlTdStart + Row["DOB"] + htmlTdEnd;
messageBody = messageBody + htmlTdStart + Row["Email"] + htmlTdEnd;
messageBody = messageBody + htmlTdStart + Row["Mobile"] + htmlTdEnd;
messageBody = messageBody + htmlTrEnd;
}
messageBody = messageBody + htmlTableEnd;
return messageBody;
}
catch (Exception ex)
{
return null;
}
}
public static void SendAutomatedEmail(string htmlString)
{
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress("[email protected]");
message.To.Add(new MailAddress("[email protected]"));
message.Subject = "Test";
message.IsBodyHtml = true;
message.Body = htmlString;
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("[email protected]", "Password");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception)
{}
}
public static void err() //Call this fun on click or in any eventhandler
{
DataSet dataSet = getDataSet();
string htmlString = getHtml(dataSet);
SendAutomatedEmail(htmlString);
}This code snippet for Send selected database values or data table values in HTML table format through Gmail using C#.
