2

i trying to send html email template, it works perfect, but when i put css internal styles it gives error : input string was not in correct format,

my html template that works good:

<h3>data</h3>
<hr />
<table class="table table-striped table-bordered table-advance table-hover">
    <tr>
        <td>data </td>
        <td>{0}</td>
    </tr>
    <tr>
        <td>data </td>
        <td>{1}</td>
    </tr>
    <tr>
        <td>data</td>
        <td>{2}</td>
    </tr>
    <tr>
        <td>data</td>
        <td>{3}</td>
    </tr>
    <tr>
        <td>data</td>
        <td>{4}</td>
    </tr>
    <tr>
        <td>data</td>
        <td>{5}</td>
    </tr>
    <tr>
        <td>data</td>
        <td>{6}</td>
    </tr>
</table>

my html template that gives Input string was not in a correct format error :

<h3>data</h3>
<hr />
<table class="table table-striped table-bordered table-advance table-hover">
    <tr>
        <td>data </td>
        <td>{0}</td>
    </tr>
    <tr>
        <td>data </td>
        <td>{1}</td>
    </tr>
    <tr>
        <td>data</td>
        <td>{2}</td>
    </tr>
    <tr>
        <td>data</td>
        <td>{3}</td>
    </tr>
    <tr>
        <td>data</td>
        <td>{4}</td>
    </tr>
    <tr>
        <td>data</td>
        <td>{5}</td>
    </tr>
    <tr>
        <td>data</td>
        <td>{6}</td>
    </tr>
</table>
<style type="text/css">
    table td{border:solid 1px;}
</style>

my c# code that read and send:

C_GeneralSettings settings = db.C_GeneralSettings.SingleOrDefault();
S_ResidencesReservations item = db.S_ResidencesReservations.SingleOrDefault(x => x.ReserveId == entity.ReserveId);
string subject = "data";
string message = System.IO.File.ReadAllText(Server.MapPath("~/EmailTemplates/ReservationDetails.html"));
message = string.Format(message, entity.ReserveId, entity.IsApproved ? "yes" : "no", entity.PayementPeriodEndDate, entity.ReservationDate.ToString("MM/dd/yyyy"), entity.Name, entity.Email, entity.CellPhone);

HelperMethods.SendEmail(message, entity.Email, subject, settings);

and my send mail method:

public static void SendEmail(string messageContent, string toEmail, string subject, C_GeneralSettings emailEntity)
{
    //Check if email service is allowed
    bool allowEmailService = emailEntity.AllowEmailService;
    if (!allowEmailService)
        return;

    //Construct mail message
    string from = emailEntity.Email;
    string displayName = emailEntity.DisplayName;
    string to = toEmail;

    MailMessage message = new MailMessage();
    message.From = new MailAddress(from, displayName);
    message.To.Add(new MailAddress(to));
    message.Subject = subject;
    message.IsBodyHtml = true;
    message.Body = messageContent;

    //Get mail settings from config file
    string host = emailEntity.Host;
    string username = emailEntity.Email;
    string password = emailEntity.Password;
    int port = emailEntity.Port;
    bool enableSsl = emailEntity.EnableSSL;

    //Send the message
    SmtpClient smtp = new SmtpClient();
    smtp.Host = host;
    smtp.Port = port;
    smtp.Credentials = new System.Net.NetworkCredential(username, password);
    smtp.EnableSsl = enableSsl;

    smtp.Send(message);
}
4
  • {border:solid 1px;} you should encode this perhaps Commented Sep 4, 2016 at 22:51
  • use inline style element..or go through below link ..it may help you Commented Sep 5, 2016 at 6:20
  • campaignmonitor.com/css Commented Sep 5, 2016 at 6:21
  • @MilindRajput the problem is not related to appearance of style or not, the problem is that it gives error while c# reading and process not complete Commented Sep 5, 2016 at 7:41

3 Answers 3

2

The error occurs when i use internal CSS, because i used string.format to bind data,

Internal CSS using curly brackets, so it causes such like this errors, so if you need to use internal CSS with dynamic html email templates, you can use string.replace, for example you html template will be like :

<h3>data</h3>
<hr />
<table class="table table-striped table-bordered table-advance table-hover">
    <tr>
        <td>data </td>
        <td>#name#</td>
    </tr>
</table>
<style type="text/css">
    table td{border:solid 1px;}
</style>

and to bind it in c#

string message = System.IO.File.ReadAllText(Server.MapPath("~/EmailTemplates/Template.html"));
message = message.Replace("#name#", name);
Sign up to request clarification or add additional context in comments.

Comments

1

That should not be a case by using

<style type="text/css">
    table td{border:solid 1px;}
</style>

that is another thing that it will not apply any borders if you view in gmail but in outlook you can view the borders.

But you can give a try by using. message.BodyEncoding = System.Text.Encoding.UTF8

If that does not work you may have some inputs which are not converting correctly.

Comments

0

There are a solution that to put css in inline way, but its tired way, example :

<table dir="rtl" width="100%" cellpadding="0" cellspacing="0" border="0" align="center">
    <tr>
        <td style=""><img style="display:block; width:150px; margin-bottom:10px;" src="http://www.mitghamronline.com/up/uploads/14739221.png" alt=""></td>
    </tr>
</table>

this way is working good and not gives me the previous error, but if someone know a way to write css in internal way, it will be better than this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.