HTML Formatted Emails not displayed in gmail - c#

When Im trying to generate HTML Formatted Result set and sending the resultset as Email using SMTPsettings,Its working fine in Outlook but the same htmlformat is not displayed in gmail It displays as a plain Text.
public void GenerateRpt()
{
DataSet ds= new DataSet();
//Result set is assigned to the dataset object.
if ds[0].Rows.Count == 0)
return;
else
{
try
{
StringBuilder builder = new StringBuilder();
builder.Append("<html xmlns='http://www.w3.org/1999/xhtml'>");
builder.Append("<head>");
builder.Append("<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />");
builder.Append(" <style type='text/css'> #page {} table { border-collapse: collapse;border-spacing: 0;empty-cells: show} ");
builder.Append(" .Default { font-family: Calibri;background-color: transparent;border-style: none;vertical-align: bottom;margin-left: 0in;");
builder.Append(" writing-mode: page;color: #000000;font-size: 11pt; font-style: normal;text-shadow: none; text-decoration: none ! important;font-weight: normal;}");
builder.Append(" .ce2 { background-color: #95b3d7;border-width: 0.0349cm;border-style: solid;border-color: #000000;color: #000000;font-size: 11pt;");
builder.Append("font-style: normal;font-weight: bold;margin-left: 0in;text-shadow: none;font-family: Calibri;text-decoration: none ! important;");
builder.Append("vertical-align: middle;writing-mode: page;text-align: center ! important;}");
builder.Append(" .ce5 { background-color: transparent;border-width: 0.0349cm;border-style: solid;border-color: #000000;color: #000000;font-size: 11pt;");
builder.Append("font-style: normal;font-weight: normal;margin-left: 0in;text-shadow: none;font-family: Calibri;text-decoration: none ! important;");
builder.Append("vertical-align: middle;writing-mode: page;text-align: center ! important;}");
builder.Append(" .ce6 { background-color: #a6a6a6;border-width: 0.0349cm;border-style: solid;border-color: #000000;color: #000000;font-size: 11pt;");
builder.Append("font-style: normal;font-weight: bold;margin-left: 0in;text-shadow: none;font-family: Calibri;text-decoration: none ! important;");
builder.Append("vertical-align: middle;writing-mode: page;text-align: center ! important;}");
builder.Append(" .ce13 { background-color: transparent;border-width: 0.0349cm;border-style: solid;border-color: #000000;color: #000000;font-size: 11pt;");
builder.Append("font-style: normal;font-weight: normal;margin-left: 0in;text-shadow: none;font-family: Calibri;text-decoration: none ! important;");
builder.Append("vertical-align: bottom;writing-mode: page;}");
builder.Append(" .contentText {font-size: 11pt;font-weight: normal;font-style: normal;font-style: Calibri;font-weight: bold;COLOR: #cccccc;");
builder.Append("</style>");
builder.Append("</head>");
builder.Append("<body>");
builder.Append("<table border='0' cellpadding='1' cellspacing='1' width='70%'> ");
builder.Append("<tr class='ro1'><td colspan='4'><b><p>RPT NAME</p></b></td></tr>");
builder.Append("<tr class='ro1'>");
builder.Append("<td class='ce2'><p>COL1</p></td>");
builder.Append("<td class='ce2'><p>COL2</p></td>");
builder.Append("<td class='ce2'><p>COL3</p></td>");
builder.Append("<td class='ce2'><p>COL4</p></td>");
builder.Append("</tr>");
string tempdrow = string.Empty;
int i;
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
builder.Append("<td class='ce5'>");
builder.Append("<p>" + ds.Tables[0].Rows[i]["Col1"].ToString() + "</p>");
builder.Append("</td>");
builder.Append("<td class='ce13' style='text-align: center'>");
builder.Append("<p>" + ds.Tables[0].Rows[i]["Col2"].ToString() + "</p>");
builder.Append("</td>");
builder.Append("<td class='ce13' style='text-align: center'>");
builder.Append("<p>" + ds.Tables[0].Rows[i]["Col3"].ToString() + "</p>");
builder.Append("</td>");
builder.Append("<td class='ce13' style='text-align: center'>");
builder.Append("<p>" + ds.Tables[0].Rows[i]["Col4"].ToString() + "</p>");
builder.Append("</td>");
builder.Append("</tr>");
}
// When No more records in the Dataset show the Grand Total.
builder.Append("<tr class='ro2'>");
builder.Append("<td class='ce13' style='text-align: center'>");
builder.Append("<b><p>GRAND TOTAL </p></b>");
builder.Append("</td>");
builder.Append("<td class='ce13' style='text-align: center'>");
builder.Append("<p>" + ds.Tables[0].Rows[i - 1]["Col2"].ToString() + "</p>");
builder.Append("</td>");
builder.Append("<td class='ce13' style='text-align: center'>");
builder.Append("<p>" + ds.Tables[0].Rows[i - 1]["Col3"].ToString() + "</p>");
builder.Append("</td>");
builder.Append("<td class='ce13' style='text-align: center'>");
builder.Append("<p>" + ds.Tables[0].Rows[i - 1]["Col4"].ToString() + "</p>");
builder.Append("</td>");
builder.Append("</tr>");
builder.Append("</table>");
string sVehRejsubject = System.Configuration.ConfigurationManager.AppSettings["Subject"].ToString();
sendmail(builder.ToString(), sVehRejsubject);
}
catch (Exception ex)
{
ex.Message();
}
}
}
// Mail Sending Part
void sendmail(string strResultSet,string strRptType)
{
try
{
if (strResultSet.ToString() != "")
{
string strMessage1 = string.Empty, TestEmp = "Mani";
strMessage1 = strResultSet.ToString();
strMessage1 += "<br><br><font color='blue'><b>Date:</b></font> " + DateTime.Now.ToString("dd/MM/yyyy") + "<BR>";
strMessage1 += "<br>Regards,<BR><BR>";
strMessage1 += TestEmp + "</b><br>";
<BR></td></tr></table></body></html>";
MailMessage mail = new MailMessage();
//SMTP SETTINGS DEFINED IN APP.CONFIG FILE
System.Net.Mail.SmtpClient _client = new System.Net.Mail.SmtpClient();
string sFrom = System.Configuration.ConfigurationManager.AppSettings["emailFrom"].ToString();
string sTo = System.Configuration.ConfigurationManager.AppSettings["emailTo"].ToString();
string sCC = System.Configuration.ConfigurationManager.AppSettings["emailCC"].ToString();
string sBCC = System.Configuration.ConfigurationManager.AppSettings["emailBCC"].ToString();
string sDisplayName = System.Configuration.ConfigurationManager.AppSettings["emailFromDisplayName"].ToString();
mail.From = new MailAddress(sFrom, sDisplayName);
mail.To.Add(new MailAddress(sTo));
if (sCC.ToString() != "")
{
mail.CC.Add(sCC);
}
if (sBCC.ToString() != "")
{
mail.Bcc.Add(sBCC);
}
mail.IsBodyHtml = true;
mail.Subject = strRptType + " AS ON - " + System.DateTime.Now.ToString("dd/MM/yyyy");
mail.Body = strMessage1;
_client.Send(mail);
}
}
catch (Exception ex)
{
Ex.Message();
return;
}
}

Email providers, does not allow the entire spectrum of html tags to be included in a message. In case you try to include an unsupported tag it will be ignored or automatically changed to another tag.
It is the same with unsupported css attributes.
For example <br/> is not allowed and will be ignored
What you should do is use universally supported Html tags only.
I've found this page which lists the universally supported html tags and css attributes
Good luck !

Related

How to show checkboxes to user that are saved as yes in database at frontend?

This is my grid which is dynamic. It is saving customer description against each checkbox. That is, if I mark the checkbox against Individual then it saves Y in my database otherwise N. so Now I just want to show tick on checkboxes that are saved Y in the database. Below is the code for my dynamic grid:
public string getCustomerStateGrid()
{
string html = ""; int Count = 1;
DataTable dt = getDataTableFromQuery(#"select Id,cuscheckbox,CustomerDescription from CustomerState3");
html = "<h2> Customer State </h2><br/><table class=\"display unbreakable\" id=\"tblCustomerDetail\" style=\"width:100%; border-collapse: collapse;\"><thead>";
#region Header
html += "<tr><th height='40' class=\"Greyheader\" style=\"width:5%\">S.No</th>";
html += "<th height='40' class=\"Greyheader\" style=\"width:30%\">Customer Status</th>";
html += "<th height='40' class=\"Greyheader\" style=\"width:15%\">Customer Description</th>";
//html += "<th class=\"Greyheader\" style=\"width:12%\">Action</th>";
html += "</tr></thead>";
#endregion
#region Body
html += "<tbody>";
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
html += "<tr class=\"GreyBorder\" id='tblCustomerDetail_" + dr["Id"].ToString() + "' pkid=\"" + dr["Id"].ToString() + "\" class=\"DataRow\">";
html += "<td class=\"GreyBorder\" style=\"text-align:center !important;\">" + Count + "</td>";
html += "<td class=\"GreyBorder\"><input id='txtcuscheckbox_" + dr["Id"].ToString() + "' type=\"checkbox\" style=\" text-align: right; width:95;\" value='" + dr["cuscheckbox"].ToString() + "' class=\"mediumTextField Customer Status\" /></td>";
html += "<td class=\"GreyBorder\"><input id='txtCustomerDescription_" + dr["Id"].ToString() + "' type=\"textbox\" style=\" text-align: right; width:95;\" value='" + dr["CustomerDescription"].ToString() + "' class=\"mediumTextField Customer Description\" /></td>";
html += "</tr>";
Count++;
}
}
else
{
html += "<tr class=\"GreyBorder\" ><td style=\"text-align:center !important;\" class=\"GreyBorder\" colspan='6'>No Data.</td></tr>";
html += "</tr>";
}
html += "</tbody>";
#endregion
html += "<tfoot><tr class='GreyBorder'>";
html += "<td class='GreyBorder' colspan='2'></td> ";
html += "</tr></tfoot>";
html += "</table><br/>";
return html.ToString();
}
I just want to customize this line of code
html += "<td class=\"GreyBorder\"><input
id='txtcuscheckbox_" + dr["Id"].ToString() + "'
type=\"checkbox\"
style=\" text-align: right; width:95;\" value='" + d
dr["cuscheckbox"].ToString() + "' class=\"mediumTextField
Customer Status\" /></td>";
so that it shows tick on checkbox. Can anyone modify my code to achieve this result? Is there any way we can do using ternary operator?
Well in essence you need:
var isChecked = 'Y';
html += isChecked == 'Y' ? "<input type=\"checkbox\" checked>" : "<input type=\"checkbox\">";
However, you are mixing front-end with back-end logic. Wouldnt it better to create a JSON endpoint that returns the data to the front-end and have the front-end generate the HTML?
EDIT:
To modify the current logic you could replace the line for creating the textbox with something like this:
var isChecked = dr["cuscheckbox"].ToString().ToUpper() == "Y" ? "checked" : string.Empty;
html += $"<td class=\"GreyBorder\"><input id='txtcuscheckbox_{dr["Id"].ToString()}' type=\"checkbox\" style=\" text-align: right; width:95;\" class=\"mediumTextField Customer Status\" {isChecked}/></td>";

StackFrame does not get all of frames

for effective debugging of my program, I tried to write function that gets an exception and emails me detailed reporting about the exception.
c# code:
public static bool SendExceptionMail(Exception ex, string[] mail, string title)
{
StringBuilder builder = new StringBuilder();
builder.Append("<table>");
var st = new StackTrace(ex, true);
builder.Append("<tr><td colspan='3'><h3>" + ex.GetType().Name + "</h3></td></tr>");
builder.Append("<tr><td style='border: 1px solid #d8d8d8'>function name</td><td style='border: 1px solid #d8d8d8'>function path</td><td style='border: 1px solid #d8d8d8'>line number</td></tr>");
StackFrame[] frames = st.GetFrames();
for (int i = 0; i < frames.Length; i++)
{
builder.Append("<tr>");
var frame = st.GetFrame;
var line = frame.GetFileLineNumber();
var name = frame.GetFileName();
var functionName = frame.GetMethod().Name;
builder.Append("<tr><td style='border: 1px solid #d8d8d8'>" + functionName + "</td><td style='border: 1px solid #d8d8d8'>" + name + "</td><td style='border: 1px solid #d8d8d8'>" + line + "</td></tr>");
builder.Append("</tr>");
}
builder.Append("<tr><td colspan='3'>" + getInnerException(ex) + "</td></tr>");
builder.Append("<tr><td colspan='3'>" + ex.Data + "</td></tr>");
builder.Append("<tr><td colspan='3'>" + ex.StackTrace + "</td></tr>");
builder.Append("<tr><td colspan='3'>" + DateTime.Now + "</td></tr>");
builder.Append("</table>");
return MailBll.Send("Exception in WS - " + frames[0].GetMethod().Name + " function", builder.ToString(), mail);
}
private static string getInnerException(Exception ex)
{
StringBuilder str = new StringBuilder();
do
{
str.Append(ex.Message + "<br/>";
ex = ex.InnerException;
} while (ex != null);
return str.ToString();
}
Clearing function:
public TranStatus Clearing(Entities.Tran tran, int projectId, string Link = null)
{
string d = DateTime.Now.ToString();
try
{
sendTranDetails(tran, projectId, d);
if (!string.IsNullOrEmpty(tran.Token) && !string.IsNullOrEmpty(tran.CreditNum))
{
tran.Token = "";
}
tran.Ip = "";
if ((string.IsNullOrEmpty(tran.CreditNum) || tran.CreditNum.Length < 7) && string.IsNullOrEmpty(tran.Token))
{
return db.getCardErrorMassege("478");
}
if (!string.IsNullOrEmpty(tran.CreditNum) && tran.CreditNum.Length > 4)
{
if (tran.CreditNum.Substring(0, 4) == "0404" || tran.CreditNum.Substring(0, 4) == "0403")
{
tmobile m = db.Mobiles.GetById(int.Parse(tran.Comment2));
return db.getCardErrorMassege("479");
}
}
tterminal terminal;
tproject p = db.GetProjectById(projectId);
tran.ProjectNumber = p.sNumProject;
terminal = db.GetterminalByProjectId(projectId);
tran.Total = (int)tran.Total;
tran.CompanyId = (int)p.iCompanyId;
fixTran(tran);
string intot = Dal.Transaction(terminal.sTerminalId.Substring(0, 7), PasswordBll.DecodeFrom64(terminal.sPasswordService), tran);
string creditStatus = db.getcreditStatusById(intot);
sendEmailTran(d, intot + " - " + creditStatus + "<br/>");
return db.getCardErrorMassege(intot.Substring(0, 3));
}
catch (Exception ex)
{
MailBll.SendExceptionMail(ex);
return db.getCardErrorMassege("484");
}
}
the function works ok, but there are some functions that I cannot get the original function that the exception was thrown from it.
for example: when I get exception of "IndexOutOfRangeException" I got this mail:
but I don't see which line in Clearing function threw the exception and which file it was.
what can be the reason?
Debug or Release build?
If you are using a optimised (release) build, then the compiler is allowed to remove simple functions and in-line them. At this point, they won't have a stack frame. To get a good stack trace, you need to make sure no optimisations are enabled.

Dropdownlist fills multiple times when postback, but I can't put it in !IsPostback

I have a problem I can't figure out. Hope you can help me.
I have an advanced search function, where I first have to choose if I want to search in menus or events by selecting a radiobutton. The problem is, that when I have chosen there is a postback, and I have to fill a dropdownlist with items.
It works fine, but if I search multiple times, the items are filled in again, because it's not in my if(!IsPostback){...} block, but I can't put it there, because there is a postback.
So how do I resolve this? I have tried to clear my items in different places, but that only results in the dropdownlist being empty. Here is some of my code:
if (!IsPostBack)
{
PnlMenu.Visible = false;
PnlEvents.Visible = false;
PnlResult.Visible = false;
//søgemuligheder
RblSearch.Items.Add(new ListItem("MENU", "1"));
RblSearch.Items.Add(new ListItem("EVENTS", "2"));
string _search = Request.QueryString["search"];
DataTable dt = objsearch.Search(_search);
if (!string.IsNullOrEmpty(_search))
{
if (dt.Rows.Count > 0)
{
if (dt.Rows.Count > 1)
{
if (RblSearch.SelectedValue == "1")
{
LitResult.Text += "<h4>Du har søgt på <span style='color: green;'>" + Session["search"].ToString() + "</span> - Der er " + dt.Rows.Count + " menuer, som matcher din søgning:</h4>";
}
if (RblSearch.SelectedValue == "2")
{
LitResult.Text += "<h4>Du har søgt på <span style='color: green;'>" + Session["search"].ToString() + "</span> - Der er " + dt.Rows.Count + " events, som matcher din søgning:</h4>";
}
}
else
{
if (RblSearch.SelectedValue == "1")
{
LitResult.Text += "<h4>Du har søgt på <span style='color: green;'>" + Session["search"].ToString() + "</span> - Der er " + dt.Rows.Count + " menu, som matcher din søgning:</h4>";
}
if (RblSearch.SelectedValue == "2")
{
LitResult.Text += "<h4>Du har søgt på <span style='color: green;'>" + Session["search"].ToString() + "</span> - Der er " + dt.Rows.Count + " event, som matcher din søgning:</h4>";
}
}
foreach (DataRow dr in dt.Rows)
{
LitResult.Text += "<div class='searchresult'><h3>" + dr["fldName"] + "</h3><img style='width: 55%;' src='../img/band/" + dr["fldImg"] + "' /></div>";
}
}
else
{
LitResult.Text += "<h4>Du har søgt på <span style='color: red;'>" + Session["search"].ToString() + "</span> - Der er ingen resultater! Prøv igen!";
}
}
}
//DdlMenu.Items.Clear();
//DdlEvents.Items.Clear();
//Menu
if (RblSearch.SelectedValue == "1")
{
DdlMenu.Items.Add(new ListItem("- - - VÆLG MENU - - -", "0"));
foreach (DataRow drmenu in objdiner.GetAllDinerCats().Rows)
{
DdlMenu.Items.Add(new ListItem(drmenu["fldName"].ToString(), drmenu["fldId"].ToString()));
}
}
//Events
TxtDatoFra.Attributes.Add("min", DateTime.Now.ToString("yyyy-MM-dd"));
if (RblSearch.SelectedValue == "2")
{
DdlEvents.Items.Add(new ListItem("- - - VÆLG TYPE - - -", "0"));
foreach (DataRow drevent in objevent.GetAllEventKats().Rows)
{
DdlEvents.Items.Add(new ListItem(drevent["fldName"].ToString(), drevent["fldId"].ToString()));
}
}
}
protected void RblSearch_SelectedIndexChanged(object sender, EventArgs e)
{
if (RblSearch.SelectedValue == "1")
{
PnlMenu.Visible = true;
PnlEvents.Visible = false;
DdlEvents.Items.Clear();
}
if (RblSearch.SelectedValue == "2")
{
PnlEvents.Visible = true;
PnlMenu.Visible = false;
DdlMenu.Items.Clear();
}
}

Images not displaying in email (shows as .bin)

I'm creating emails that has to be sent out to technicians. There are about 12 emails (only one will be sent out depending on which printer model was chosen). The emails everything is sending out fine. The only problem I'm having is that the images are not displaying, they are showing as attachments with a .bin extention. I'm using a case statement to send out the emails. Any help on what I'm doing wrong will be greatly appreciated. Thanks.
This is how my code looks like:
In case statement:
string headerPath = Server.MapPath("~\\Images\\EmailImages\\1022_03.png");
LinkedResource img1022_03 = new LinkedResource(headerPath);
img1022_03.ContentId = "1022_03";
string tonerPath = Server.MapPath("~\\Images\\EmailImages\\TonerSignature.png");
LinkedResource TonerSignature = new LinkedResource(tonerPath);
TonerSignature.ContentId = "TonerSignature";
string img1022_11Path = Server.MapPath("~\\Images\\EmailImages\\1022_11.png");
LinkedResource img1022_11 = new LinkedResource(img1022_11Path);
img1022_11.ContentId = "1022_11";
AlternateView avDefault = AlternateView.CreateAlternateViewFromString(SendOMD(), null, MediaTypeNames.Text.Html);
avDefault.LinkedResources.Add(img1022_03);
avDefault.LinkedResources.Add(TonerSignature);
avDefault.LinkedResources.Add(img1022_11);
Mail.AlternateViews.Add(avDefault);
This code is inside a case statement of default. Outside the case I add the smtp details:
System.Configuration.Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration("~/web.config");
MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
if (mailSettings != null)
{
int port = mailSettings.Smtp.Network.Port;
string host = mailSettings.Smtp.Network.Host;
string password = mailSettings.Smtp.Network.Password;
string username = mailSettings.Smtp.Network.UserName;
}
SmtpClient SMTP = new SmtpClient();
SMTP.Send(Mail);
This is what my email method looks like:
private string SendOMD()
{
string techType = radTechnicianType.SelectedValue.ToString();
string techString = "";
if (techType == "Field Technician")
techString = "Please send a technician to replace the toner on the above mentioned machine.";
else if (techType == "Onsite technician")
techString = "Please replace the toner on the above mentioned machine.";
else
techString = "Please send a technician to replace the toner on the above mentioned machine.";
StringBuilder stringBuilt = new StringBuilder();
stringBuilt.AppendLine(#"<style type=""text/css"">");
stringBuilt.AppendLine(".divStyle { font-family:Verdana,sans-serif, Arial, Helvetica; font-size:10pt; font-weight: bold; }");
stringBuilt.AppendLine(".par { font-family:Verdana,sans-serif, Arial, Helvetica; font-size:10pt; width:658px; }");
stringBuilt.AppendLine("table { border: 1px solid #000; border-collapse: collapse; font-family:Verdana; font-size:10pt; background-color:#dcdcdc; }");
stringBuilt.AppendLine("table td { border-left: 1px solid #000; font-family:Verdana,sans-serif, Arial, Helvetica; font-size:10pt; background-color:snow; border-bottom: 1px solid #000; padding-left:2px; margin-left:2px; padding-right:5px; margin-right:5px; }");
stringBuilt.AppendLine("table td:first-child { border-left: none; font-family:Verdana,sans-serif, Arial, Helvetica; font-size:10pt; background:#dcdcdc; background-color:#dcdcdc; border-bottom: 1px solid #000; }");
stringBuilt.AppendLine("</style>");
stringBuilt.AppendLine(#"<img alt="" src=""cid:1022_03"" width=""817"" height=""123"" />");
stringBuilt.AppendLine("<br/>");
stringBuilt.AppendLine(#"<p class=""par"">Dear " + txtClient.Text.Trim() + ",</p>");
stringBuilt.AppendLine("Details:");
stringBuilt.AppendLine("<br/>");
stringBuilt.AppendLine("<table>");
stringBuilt.AppendLine(#"<tr><td style=""background-color:#dcdcdc; widht:350px;"" >Model: </td><td style=""widht:250px;""> " + lstModelType.SelectedValue.ToString() + " </td></tr>");
stringBuilt.AppendLine(#"<tr><td style=""background-color:#dcdcdc; widht:350px;"" >Serial No: </td><td style=""widht:250px;""> " + txtSerial.Text.Trim() + " </td></tr>");
stringBuilt.AppendLine(#"<tr><td style=""background-color:#dcdcdc; widht:350px;"" >IP Address: </td><td style=""widht:250px;""> " + txtIPAddress.Text.Trim() + " </td></tr>");
stringBuilt.AppendLine(#"<tr><td style=""background-color:#dcdcdc; widht:350px;"" >AD Sharename: </td><td style=""widht:250px;""> " + txtADSharename.Text.Trim() + " </td></tr>");
stringBuilt.AppendLine(#"<tr><td style=""background-color:#dcdcdc; widht:350px;"">Department: </td><td style=""widht:250px;""> " + txtDepartment.Text.Trim() + " </td></tr>");
stringBuilt.AppendLine(#"<tr><td style=""background-color:#dcdcdc; widht:350px;"">Colour: </td><td style=""widht:250px;""> " + lstColour.SelectedValue.ToString() + " </td></tr>");
stringBuilt.AppendLine("</table>");
stringBuilt.AppendLine(#"<p class=""par""><em>" + techString + "</em></p>");
stringBuilt.AppendLine("<br/>");
stringBuilt.AppendLine(#"<div class=""divStyle""> Regards, ");
stringBuilt.AppendLine("<br/>");
stringBuilt.AppendLine(lstApprover.SelectedValue.ToString());
stringBuilt.AppendLine("</div>");
stringBuilt.AppendLine(#"<img alt="" src=""cid:TonerSignature"" width=""300"" height=""70"" />");
stringBuilt.AppendLine("<br/>");
stringBuilt.AppendLine(#"<img alt="" src=""1022_11"" width=""815"" height=""54"" />");
return stringBuilt.ToString();
}
You need to Add mime type to your resources.
for ex : LinkedResource img1022_11 = new LinkedResource(img1022_11Path,MediaTypeNames.Image.Gif);
you can add Jpg, Tiff, Gif images, for png files there is a workaround.
use this.
htmlView.LinkedResources.Add(
new LinkedResource("../EmailTemplate/ContactDetails.png")
{
ContentId = "ContactDetails",
TransferEncoding = TransferEncoding.Base64,
ContentType = new ContentType("image/png")
});
instead of Server.MapPath use website path to get the images

Datatable as mail in C# console application

I am trying to create html from my dataset using the following code
public static string getHtml(DataTable dataSet)
{
try
{
string messageBody = "<font>The following are the records: </font><br><br>";
if (dataSet.Rows.Count == 0)
return messageBody;
string htmlTableStart = "<table style=\"float:left; 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>";
foreach (DataColumn dc in dataSet.Columns)
{
messageBody += htmlTableStart;
messageBody += htmlHeaderRowStart;
messageBody += htmlTdStart + dc + htmlTdEnd;
messageBody += htmlHeaderRowEnd;
foreach (DataRow Row in dataSet.Rows)
{
messageBody = messageBody + htmlTrStart;
messageBody = messageBody + htmlTdStart + Row["" + dc + ""] + htmlTdEnd;
messageBody = messageBody + htmlTrEnd;
}
}
messageBody = messageBody + htmlTableEnd;
return messageBody;
}
catch (Exception ex)
{
return null;
}
}
I want to render in a way that
All different tables should come in horizontally. I tried float:left but when i use this html to be send in mail , float-left don't work
Can something be changed in code so that Tables come in hozizontally as
I completed my answer . here is the solution to convert dataset to html
public static string DataTableToHTML(DataTable dataSet)
{
mLogger.Error("DataTableToHTML -------> Start");
try
{
string messageBody = "<font>Following corporate actions are not updated in Security Master: </font><br><br>";
if (dataSet.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;
foreach (DataColumn dc in dataSet.Columns)
{
messageBody += htmlTdStart + dc + htmlTdEnd;
} messageBody += htmlHeaderRowEnd;
foreach (DataRow dr in dataSet.Rows)
{
messageBody = messageBody + htmlTrStart;
foreach (DataColumn dc in dataSet.Columns)
{
messageBody = messageBody + htmlTdStart + dr["" + dc + ""] + htmlTdEnd;
}
messageBody = messageBody + htmlTrEnd;
}
messageBody = messageBody + htmlTableEnd;
mLogger.Error("DataTableToHTML -------> End");
return messageBody;
}
catch (Exception ex)
{
mLogger.Error("Exception in DatatableToHTML" + ex);
return null;
}
}

Categories