Send Email using smtp C#
public void SendEmail(string smtpClient_add, int port_no, string username, string password, string fromEmail, string fromname, string Email_subject, string Email_body, string toEmail, string ccEmail, string pathToAttachment)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(smtpClient_add);
mail.From = new MailAddress(fromEmail, fromname);
mail.To.Add(toEmail);
if (ccEmail != "")
mail.CC.Add(ccEmail);
mail.Subject = Email_subject;
mail.Body = Email_body;
mail.IsBodyHtml = true;
if (pathToAttachment != "")
mail.Attachments.Add(new Attachment(pathToAttachment));
SmtpServer.Port = port_no;
SmtpServer.Credentials = new System.Net.NetworkCredential(username, password);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
Convert DataTable to HTML in C#
public static string ConvertDataTableToHTML(DataTable dt)
{
string html = "<table border='1' cellpadding='0' cellspacing='0'>";
//add header row
html += "<tr>";
for (int i = 0; i < dt.Columns.Count; i++)
html += "<th bgcolor='#507CD1' style='padding: 2px; color: #FFFFFF'>" +
dt.Columns[i].ColumnName + "</th>";
html += "</tr>";
//add rows
for (int i = 0; i < dt.Rows.Count; i++)
{
html += "<tr>";
for (int j = 0; j < dt.Columns.Count; j++)
html += "<td style='padding: 2px;'>" + dt.Rows[i][j].ToString() + "</td>";
html += "</tr>";
}
html += "</table>";
return html;
}