I want to compose a mail with a table with some basic style. Here is my code:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("abc#domain.com");
mail.To.Add("myname#domain.com");
mail.Body = string.Format(#"<html>
<body>
<style>
table, th, td {
border: 1px solid black;
line-height: 24px;
font-size:14px;
font-family:verdana;
text-align: center;
}
</style>
<p style=""""font-family:verdana""""> Status :</p>
<table style=""""width=50%""""><tr>
<th >No. of Students</th>
<th >No. of Pass</th>
<th >No. of Failures </th>
<tr/><tr style=""""height=120%"""">
<td>{0} </td>
<td style=""""color:green"""">{1} </td>
<td style=""""color:red"""">{2} </td>
</tr></table>
<p style=""""font-family:verdana""""> Please refer to the
Log files for more details.</p>
</body>
</html>",student.Total, student.Pass,student.Fail);
mail.Subject = "Report";
};
mail.IsBodyHtml = true;
This gives me an Exception saying 'Input string is not in correct format'. If I remove the style, paragraph, and keep only , it works but no borders are present in table. How can I style my mail?
I changed the body like this, but the style is not reflected.
mail.Body = string.Format(#"
<p style=""font-family:verdana""> Status :</p>
<table style=""border: 1px solid black"", ""text-align: center""><tr>
<th style=""border: 1px solid black"">No. of new records added</th>
<th style=""border: 1px solid black"">No. of records transferred </th>
<th style=""border: 1px solid black"">No. of records not transferred </th>
<tr/><tr style=""height=120%"">
<td style=""border: 1px solid black"" ""text-align: center"">CompanyName.Value </td>
<td style=""border: 1px solid black"" ""text-align: center"" ""color:green"">1000 </td>
<td style=""border: 1px solid black"" ""text-align: center"" ""color:red"">82 </td>
</tr></table>
<p style=""font-family:verdana""> Please refer to the Log files for more details.</p>
");
Your syntax is wrong while adding style for <table> and <p>. Do it in the following way, for example:
<table style="width: 50%">
Similarly change the syntax wherever you have used style.
Related
I have to send a Table that have a column with autoincrement number and when I sent the mail it shows the second column values but none the auto increment numbers:
This is my html script generated by c# code:
<style>
.tableOrder {
counter-reset: rowNumber;
border-collapse: collapse;
text-align: center;
}
.tableOrder tr .counterColumn {
counter-increment: rowNumber;
}
.tableOrder tr .counterColumn:first-child::before {
content: counter(rowNumber);
min-width: 1em;
margin-right: 0.5em;
}
</style>
</table>
<font> This is the following order of Pickups Arrives </font> <br><br>
<table class="tableOrder" border="1">
<tr style="background-color:#6FA1D2;color:#ffffff;">
<td style="color:#555555;">Order Arrive</td>
<td style="color:#555555;">Pickup Number</td>
</tr>
<tr>
<td class="counterColumn"></td>
<td>1</td>
</tr>
<tr>
<td class="counterColumn"></td>
<td>1233</td>
</tr>
<tr>
<td class="counterColumn"></td>
<td>521346</td>
</tr>
<tr>
<td class="counterColumn"></td>
<td>1246</td>
</tr>
<tr>
<td class="counterColumn"></td>
<td>7594</td>
</tr>
</table>
and when I run my code and sent the email it didn't show the autoincrement values column
I'm using the SmtpClient in C#
MailMessage message = new MailMessage()
{
From = fromEmail,
Subject = "Subject",
IsBodyHtml = true,
Body = htmlString,
};
message.To.Add(toEmail);
message.CC.Add(ccEmail);
I have found a lot of open questions about this issue, but none gave me a solution, so I am kind of re-opening other questions here, sorry.
I have an HTML created (from a VB6 application, though that's not very important because I checked it is correctly formatted) and then sent from a .NET Core worker application.
The IsBodyHtml property is being set to true, so it should work correctly.
The HTML I have generated has this structure:
<table>
<thead>
<tr>
<th colspan="2"> Resultado </th>
<th>Índice del vector</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiempo inicio</td>
<td class="centered">{DateTime}</td>
<td class="centered">0</td>
</tr>
<tr>
<td>Tiempo fin</td>
<td class="centered">{DateTime}</td>
<td class="centered">1</td>
</tr>
<tr>
<td>Tiempo total</td>
<td class="centered">{DateTime(Tiempo fin - Tiempo inicio)}</td>
<td class="centered">2</td>
</tr>
<tr>
<td>Tipo de bases de datos</td>
<td class="centered">{"SQL" | "Access"}</td>
<td class="centered">3</td>
</tr>
<tr>
<td>Copia de Documentos</td>
<td class="centered">{ True | False }</td>
<td class="centered">5</td>
</tr>
<tr>
<td>Carpeta no existente</td>
<td class="centered">"TextMessage"</td>
<td class="centered">11</td>
</tr>
</tbody>
</table>
<style>
* {
font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
table,
td {
border: 1px solid #333;
}
th,
td {
padding: 5px;
inline-size: 7rem;
overflow-wrap: break-word;
}
thead,
tfoot {
background-color: #333;
color: #fff;
}
.centered {
text-align: center;
}
</style>
I checked the Gmail references and it should work since the <style> tag is now supported (I think the only thing that is not very well supported is the font-family property), but it still was sent as raw HTML.
Then I tried sending a simple <h1>This is an H1 element</h1>, and it was still not shown correctly, just as raw HTML.
I also tried sending the former HTML with the <body> tag and the <style> tag in the <head> tag.
[EDIT]
Updated with the C# code for the e-mail sender:
private MailMessage SetMessageInfo(EmailService.MailMessage message)
{
MailMessage clientMessage = new Net_MailMessage();
clientMessage.From = new MailAddress(message.From.Address, message.From.DisplayName);
foreach (EmailService.MailAddress email in message.To)
{
clientMessage.To.Add(new MailAddress(email.Address, email.DisplayName));
}
foreach (EmailService.MailAddress email in message.CC)
{
clientMessage.CC.Add(new MailAddress(email.Address, email.DisplayName));
}
foreach (EmailService.MailAddress email in message.Bcc)
{
clientMessage.Bcc.Add(new MailAddress(email.Address, email.DisplayName));
}
clientMessage.Subject = message.Subject;
clientMessage.SubjectEncoding = message.SubjectEncoding;
clientMessage.Body = message.Body;
clientMessage.BodyEncoding = message.BodyEncoding;
clientMessage.IsBodyHtml = true;
if (message.CustomId != null)
{
clientMessage.Headers.Add("custom-id", message.CustomId);
}
return clientMessage;
}
The EmailService class is an intermediary class we created that has a DTO that is almost a copy of the MailMessage class. I can't show it here, sorry.
I sent an html via outlook.
In the html, I aligned a word to the right, but in the received e-mail, the word is mixed - the first letter has become the last letter.
This only occurs when the first letter is a number.
I sent the following html:
<div dir="rtl" style="margin: 20px auto; width: 650px; text-align: center; font-family: Tahoma;">
<table dir="rtl" style="width: 650px; margin: 0 auto; text-align: right; font-family: Tahoma; font-size: 0; font-weight: normal; color: #000;" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="background-color: #d0f2f6; padding: 15px 20px; margin: 0; color: #135861; font-size: 13px; font-weight: 400;">
שלום
<br /><br />
המספר הוא:<br />
<b dir="rtl" style="font-family:consolas">1fD9xG8j</b>
<br /><br />
</td>
</tr>
</tbody>
</table>
</div>
But I got the following mail:
Why does outlook change the word '1fD9xG8j' to 'fD9xG8j1' ?
This is the code in c# which sent the mail:
var smtp = new SmtpClient(SmtpServer);
var message = new MailMessage();
message.Subject = subject.Trim();
message.Body = body.Trim();
message.IsBodyHtml = true;
message.BodyEncoding = Encoding.UTF8;
smtp.Send(message);
Remove dir="rtl" from Table tag and try.
I saw, that many times, Outlook text editor adds code to the source code. This happens because Outlook is generating the email source code using mostly VML (Vector Markup Language) which causes changes in the code. There are add-ins to import a clean HTML source code, to an Outlook email, in order to get it displayed correctly.
I tried to get around the problem with the following code:
<div dir="ltr" style="font-family:consolas;font-weight:bold;text-align:right">1fD9xG8j</div>
I changed dir="ltr", and added style text-align:right,
and it works!
I'm generating a PDF file based on some HTML, using the pechkin dll.
This is all working nicely except the I need to it add a page break at certain places and I don't know how.
I thought that using a css page-break-before, but that doesn't seem to work.
An example of the HTML I'm using is:
<table style="border-top: 0px solid black; border-bottom: 2px solid black; height: 30px; width: 800px;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="width: 200px;"><strong>Recommendation</strong></td>
<td style="width: 600px;">[6060:Builder Recommendation]</td>
</tr>
</tbody>
</table>
<table style="page-break-before: always;border-top: 0px solid black; border-bottom: 2px solid black; background-color: #99ccff; height: 30px; width: 800px;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td><strong>Summary of Actions</strong></td>
</tr>
</tbody>
</table>
The code I'm using to generate the PDF is as below:
Dim buf As Byte() = Pechkin.Factory.Create(New GlobalConfig().SetMargins(New Margins(20, 20, 20, 20))
.SetDocumentTitle("").SetCopyCount(1).SetImageQuality(100)
.SetLosslessCompression(True)
.SetMaxImageDpi(300)
.SetOutlineGeneration(True)
.SetOutputDpi(1200)
.SetPaperOrientation(True)
.SetPaperSize(PaperKind.A4)
.SetImageQuality(100)
.SetPaperOrientation(False))
.Convert(New ObjectConfig()
.SetPrintBackground(True)
.SetAllowLocalContent(True), strHTML)
Return buf
Any ideas?
Add a div before your table.
<div style='page-break-after:always'></div>
I'm sending the following html content (pasted below) using the .net framework 4.0 using the following code:
public void sendHTMLMessage()
{
From = constants.GetValue(EnvironmentVariables.ConstantNames.EMAILFROM);
MailMessage mail = SetMailMessageParameters();
smpt = new SmtpClient(constants.GetValue(EnvironmentVariables.ConstantNames.SMTP));
Utils.EnvironmentLogger.WriteToMessageLog("Sending email message: " + Subject);
smpt.Send(mail);
}
private MailMessage SetMailMessageParameters()
{
MailMessage mail = new MailMessage();
mail.Subject = Subject;
mail.Body = Body;
mail.IsBodyHtml = true;
mail.From = new MailAddress(From);
List<string> ToArray = Utils.GetArrayFromConstantsClass(To);
foreach (string element in ToArray)
{
mail.To.Add(new MailAddress(element));
}
if (!string.IsNullOrEmpty(Attachment))
{
mail.Attachments.Add(new Attachment(Attachment));
}
return mail;
}
I'm basically just sending a simple e-mail and it works most of the time, but when the HTML gets long the email I receive is just a bunch of junk:
JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7/AEwAaQBnAGgAdABpAG4AZwAgAEQA
aQBnAGkAdABhAGwAIABDAG8AYwBrAHAAaQB0KQovQ3JlYXRvciAo/v8AdwBrAGgAdABt
AGwAdABvAHAAZABmACAAMAAuADEAMgAuADEpCi9Qcm9kdWNlciAo/v8AUQB0ACAANAAu
ADgALgA2KQovQ3JlYXRpb25EYXRlIChEOjIwMTQwNzA4MTAwMDA4LTA0JzAwJykKPj4K
and goes on and on...I have tried different encoding and can't seem to figure out what the issue is. I also tried targeting .net 4.5 and I get the same issue. The HTML opens just fine in a web browser, and I can even send the html through outlook and it works just fine. It's just when I send through the code that it breaks. The other thing that is interesting, if I just send one or two tables (shown below) it sends just fine, but if I send the same table duplicated about 30 times that's when it sends the garbled mess. It does the same thing if I try to simply attach the HTML to the email. Any help would be greatly appreciated.
EDIT I opened the html email while listening with Fiddler and got the following header:
HTTP/1.1 200 OK
Date: Thu, 10 Jul 2014 12:23:50 GMT
Server: Apache/2.2.21 (CentOS)
Content-Disposition: inline;filename*=UTF-8''Daily_Sales_Summary_gbl7-8-2014.html
Cache-control: private
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,POST
Access-Control-Allow-Headers: X-Requested-With
X-XSS-Protection: 0
Vary: Accept-Encoding
Content-Encoding: gzip
Keep-Alive: timeout=15, max=945
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html
Set-Cookie: BIGipServerLIBRARIES_PRD_WEB1=2833575683.20480.0000; expires=Fri, 11-Jul-2014 00:23:51 GMT; path=/
Here is the content of the HTML File:
<html>
<head>
<title>HTML Table Email Test</title>
</head>
<style type="text/css">
body
{
font-family: Arial, Helvetica, Univers;
font-size: 10pt;
font-style: normal;
font-weight: normal;
line-height: 12pt;
text-align: left;
}
b
{
font-weight: bold;
}
table
{
border-collapse: collapse;
}
div.tableContainer
{
font-size: 8pt;
width: 100%;
overflow: auto;
}
div.tableContainer th
{
font-size: 8pt;
font-weight: normal;
text-decoration: underline;
text-align: center;
background-color: #BBC3E2;
color: #000000;
border-color: #808080;
border-style: solid;
border-width: 1px;
position: relative;
padding-right: 3;
padding-left: 3;
cursor: default;
}
div.tableContainer td
{
font-size: 9pt;
text-align: right;
white-space: nowrap;
background-color: #F1EEE3;
color: #000000;
border-color: #808080;
border-style: solid;
border-width: 1px;
position: relative;
padding-right: 3px;
padding-left: 3px;
}
div.tableContainer td.locked, div.tableContainer th.locked
{
background-color: #BBC3E2;
text-align: left;
color: #000000;
position: relative;
text-decoration: none;
}
div.tableContainer tr.total
{
font-weight: bold;
}
</style>
<body>
<div class="tableContainer" id="data">
Day<br>Sales<br>Details<br><br>
<table border="0" cellspacing="0" cellpadding="0" style="width: auto">
<tbody>
<tr><th class="locked"> </th><th>Value<br>1</th><th>Value<br>2</th><th>VAlue<br>3</th><th>Value</th><th>Value</th><th>Value</th><th>Value<br>value</th><th>value<br>value</th><th>value<br>$</th><th>value<br>value</th></tr>
<tr><td class="locked">-Line1</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>0</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">--Line2</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>2</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">--Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>1</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">--Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>####</td><td>0.0</td><td>3</td><td>0.0</td><td>0%</td><td>11</td><td>0%</td></tr>
<tr><td class="locked">-Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>6</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">-Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>4</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">-Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-</td><td>0.0</td><td>0</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>1</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked"> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td></tr>
<tr><td class="locked">Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>2</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">-Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>1</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">-Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>7</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>8</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>8</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked"> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td></tr>
<tr><td class="locked"><b>Line3</b></td><td><b>0.0</b></td><td><b>0.0</b></td><td><b>0.0</b></td><td><b>0%</b></td><td><b>0</b></td><td><b>0</b></td><td><b>0</b></td><td><b>0</b></td><td><b>0</b></td><td><b>0%</b></td></tr>
</tbody>
</table>
Day<br>Sales<br>Details<br><br>
<table border="0" cellspacing="0" cellpadding="0" style="width: auto">
<tbody>
<tr><th class="locked"> </th><th>Value<br>1</th><th>Value<br>2</th><th>VAlue<br>3</th><th>Value</th><th>Value</th><th>Value</th><th>Value<br>value</th><th>value<br>value</th><th>value<br>$</th><th>value<br>value</th></tr>
<tr><td class="locked">-Line1</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>0</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">--Line2</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>2</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">--Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>1</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">--Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>####</td><td>0.0</td><td>3</td><td>0.0</td><td>0%</td><td>11</td><td>0%</td></tr>
<tr><td class="locked">-Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>6</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">-Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>4</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">-Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>-</td><td>0.0</td><td>0</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>1</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked"> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td></tr>
<tr><td class="locked">Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>2</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">-Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>1</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">-Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>7</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>8</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked">Line3</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0%</td><td>0.0</td><td>8</td><td>0.0</td><td>0%</td><td>0</td><td>0%</td></tr>
<tr><td class="locked"> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td></tr>
<tr><td class="locked"><b>Line3</b></td><td><b>0.0</b></td><td><b>0.0</b></td><td><b>0.0</b></td><td><b>0%</b></td><td><b>0</b></td><td><b>0</b></td><td><b>0</b></td><td><b>0</b></td><td><b>0</b></td><td><b>0%</b></td></tr>
</tbody>
</table>
<br>data is effective for close of previous business day<br>
</div>
</body>
</html>
Here's the problem:
Content-Encoding: gzip
Something; your ISP, the POP server, perhaps even SmtpClient.Send is GZipping the HTTP payload. This is allowed in HTTP. From the Server: Apache/2.2.21 (CentOS) I suspect it is the HTTP server.
Any compliant HTTP client should un-GZip the payload when received with this header. The standard .NET HttpClient class will do this.
What is “receiving” the email? A program you wrote? It has to un-GZip the payload. Or contact your server admin and see if there is some way to tell it not to GZip large payloads.
Thanks to everyone for your help. I found out what was causing the issue, although I'm not sure why this fixed the problem. When I change the code to use seven bit encoding it sends the e-mail without any issue. I would love to hear any reason to why this fixes the problem. It feels like a band aid fix. It must have something to do with exchange. Also not sure why I was getting the garbled text even when not sending an attachment. here is the code to fix:
Attachment attachment = new Attachment(this.Attachment);
attachment.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
mail.Attachments.Add(attachment);