0

I have a script task that creates an HTML email and I can add a background image but I am having trouble getting the image to stretch to size. Everything I have read suggests using CSS but I am not very versed at HTML/CSS and even less sure of how to implement this in a script task. Here is my code:

  DataTable dt = new DataTable();
sdaGetValidation.Fill(dt);   

StringBuilder sb = new StringBuilder();
sb.AppendLine("<html><body {background='http://myImage.jpg'>");
sb.AppendLine("\t" + "<body>");
sb.AppendLine("\t\t" + "<table>");
sb.Append("<table border='1px' solid line black cellpadding='5' cellspacing='0' ");
sb.Append("style='border: solid 1px Silver; font-size: x-small;'>");

sb.Append("\t\t" + "<tr>");

foreach (DataColumn dc in dt.Columns)
{
    sb.AppendFormat("<td>{0}</td>", dc.ColumnName);
}

sb.AppendLine("<tr>");

foreach (DataRow dr in dt.Rows)
{
    sb.Append("\t\t\t" + "<tr>");

    foreach (DataColumn dc in dt.Columns)
    {
        string cellValue = dr[dc] != null ? dr[dc].ToString() : "";
        sb.AppendFormat("<td>{0}</td>", cellValue);
    }

    sb.AppendLine("</tr>");
}

sb.AppendLine("\t\t\t" + "</table>");
sb.AppendLine("\t" + "</body>");
sb.AppendLine("</html>");

MessageBox.Show(sb.ToString());

This line gets the image to show but it tiles:

sb.AppendLine("<html><body background='http://myImage.jpg'>");

I'm hoping this will be a quick win for someone who knows HTML better than I. Once again I keep reading that I'll need to use CSS but not even sure if that is true or how to implement CSS in this situation.

user3486773
  • 1,174
  • 3
  • 25
  • 50
  • Not sure what you need it for, but you could try: `sb.AppendLine("")` – daniel Feb 09 '16 at 20:06
  • I'm trying to use a company background image and due to the HTML table being so long it's causing the image to tile, either that or it's just trying to fill the screen. I would just like to have it stretch so no matter what screen the email is viewed on it looks essentially the same. Unfortunately a required server is down so I can't test this solution yet but will asap and mark as answer it it works. I want to say I have tried this but my syntax might have been off... – user3486773 Feb 09 '16 at 20:09
  • The code itself seems to add `` twice, just a heads up :) – daniel Feb 09 '16 at 20:10
  • That could be my issue!!! – user3486773 Feb 09 '16 at 20:11
  • So removed the second tag and pasted your code in and it's the same thing where it tiles the image!!! So frustrating!!! – user3486773 Feb 09 '16 at 20:16
  • That should work... does anything change if you do '100% 100%' instead of 'cover' ? – daniel Feb 09 '16 at 20:19
  • Oh, maybe it's because the body itself is looping? What happens if you do: `sb.AppendLine("");` – daniel Feb 09 '16 at 20:21
  • Still the same thing! – user3486773 Feb 09 '16 at 20:26
  • That's odd... I think this is too tough to spot the error in with the code on hand, does adding `sb.AppendLine(" ........."` do anything ? I guess not... – daniel Feb 09 '16 at 20:32

1 Answers1

0

Try to add the style attribute within the body tag:

<body style="background-image:url('http://something.com'); background-size:cover;">
lala1
  • 1
  • 3