Mac Mac - 3 months ago 11
C# Question

How can i Insert the whole HTML file with styles when sending email

I want to send an email. I have the HTML template which i want to use to send email.I will pass some data from the text boxes Just like confirmation email with your details if you registered on a system. I can't get the format inserted or added as it is giving errors

Here is what i have:

public void SendingEmails()
{
string emailbody = string.Empty;
string mapfile = HttpContext.Current.Server.MapPath("~/EmailTemplate.htm");

using (StreamReader reader = new StreamReader(mapfile))
{
if (reader == null)
{
throw new Exception("File is null");
}
emailbody = reader.ReadToEnd();
}
emailbody = emailbody.Replace("{Url}", _Url);






string Username = "********";
string Password = "********";
string host = "********";
string FromEmailAddress = "********";
string ToEmailAddress = "********";
string emailBody = string.Empty;

var _mail = new MailMessage();
{
SmtpClient smtp = new SmtpClient();
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.Host = host;
smtp.Timeout = 8900000;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(Username, Password);
_mail.From = new MailAddress(FromEmailAddress);
_mail.To.Add(ToEmailAddress);
_mail.Subject = "Email Confirmation";
eBody = emailBody.ToString();
_mail.IsBodyHtml = true;
smtp.Send(_mail);
}

}

Answer

The issue is because you have the HTML in multiple lines and then there are double quotes " within the HTML. You can resolve it by using @ as a string formatter for multiple lines, And changing all the double quotes inside your HTML to single quotes '. Or for some reason if you still need to use double quotes inside your HTML then just escape its meaning by using a backslash \"

I have cleaned up the code for you. Just copy paste this into your code.

string emailBody = @"<div class='WordSection1'>
       < table cellpadding = '0' cellspacing = '0' align = 'left >
                 < tr >
                     < td width = '0' height = '0' ></ td >
                    </ tr >
                    < tr >

                       < td width = '762' height = '1091' style = 'border:1.0pt solid goldenrod;vertical-align:top' >
                             < spanz - index:251658240'>
                               < table width = '90%;' style = 'margin:1em;' >
                                       < tr >
                                           < td >
                                               < div class='shape'>
                                <div><img src = 'cid:imageId' height='50px' style='padding-right:1em; float:right;'></div><p><o:p>&nbsp;</o:p></p>


            <p style = 'font-family:Calibri' >< tr >< td width='50%'><img alt = '' hspace='0' src='' align='baseline' border='0'>
                                    Dear Call Centre,
                               </p>
                               <p><o:p>&nbsp;</o:p></p> 
                               <p style = 'font-family:Calibri' >< tr >< td width='50%'>
                                    This communication serves to notify you that the below claim has been registered on the system:
                               </p>

                                <p><o:p>&nbsp;</o:p></p>    

                                <p><b>Date Submitted</b> : txtDate, 2016</p>
                                <p><b>Insurer</b> : txtMac<span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                </span></p>

                                <p><o:p>&nbsp;</o:p></p>

                                <p style = 'background:lightsteelblue; width:60%' >< b >
                                    < span style='font-size:12.0pt;color:#b8860b;font-family:' Cambria?,?serif?;?=''>Claim Information</span></b>
                                </p>

        <table class='MsoNormalTable' border='0' cellspacing='0' cellpadding='0' style='border-collapse:collapse;mso-padding-alt:0in 0in 0in 0in'>
        <tr style = 'mso-yfti-irow:0;mso-yfti-firstrow:yes;page-break-inside:avoid' >
        < td width='189' valign='top' style='width:141.75pt;padding:0in 3.4pt 0in 3.4pt'>
        <p><span class='SpellE'>PNumber</span></p>
        </td>
        <td width = '378' valign='top' style='width:283.45pt;padding:0in 3.4pt 0in 3.4pt'>
        <p>txtNumber</p>
        </td>
        </tr>
            <tr style = 'mso-yfti-irow:2;page-break-inside:avoid' >
                < td width='189' valign='top' style='width:141.75pt;padding:0in 3.4pt 0in 3.4pt'>
                <p>Name</p>
                </td>
                <td width = '378' valign='top' style='width:283.45pt;padding:0in 3.4pt 0in 3.4pt'>
                <p>txtName</p>
                </td>
                </tr>
                <tr style = 'mso-yfti-irow:3;page-break-inside:avoid' >
                < td width='189' valign='top' style='width:141.75pt;padding:0in 3.4pt 0in 3.4pt'>
                <p>Broker Email Address</p>
                </td>
                <td width = '378' valign='top' style='width:283.45pt;padding:0in 3.4pt 0in 3.4pt'>
                <p>txtEmail</p>
                </td>
                </tr>
                <tr style = 'mso-yfti-irow:4;page-break-inside:avoid' >
                < td width='189' valign='top' style='width:141.75pt;padding:0in 3.4pt 0in 3.4pt'>
                <p>Tel</p>
                </td>
                <td width = '378' valign='top' style='width:283.45pt;padding:0in 3.4pt 0in 3.4pt'>
                <p>txtTel</p>
                </td>
                </tr>                   
                <tr style = 'mso-yfti-irow:8;page-break-inside:avoid' >
                < td width='189' valign='top' style='width:141.75pt;padding:0in 3.4pt 0in 3.4pt'>
                <p>Incident Date</p>
                </td>
                <td width = '378' valign= 'top' style= 'width:283.45pt;padding:0in 3.4pt 0in 3.4pt' >
                < p > txtInceptiodate </ p >
                </ td >
                </ tr >
                < tr style= 'mso-yfti-irow:9;mso-yfti-lastrow:yes;page-break-inside:avoid' >
                < td width= '189' valign= 'top' style= 'width:141.75pt;padding:0in 3.4pt 0in 3.4pt' >
                < p > Range </ p >
                </ td >
                < td width= '378' valign= 'top' style= 'width:283.45pt;padding:0in 3.4pt 0in 3.4pt' >
                < p > txtRange </ p >
                </ td >
                </ tr >
                </ table >

                < p >< o:p>&nbsp;</o:p></p>

                <p style = 'background:lightsteelblue; width:60%' >< b >< span style='font-size:
                12.0pt;color:#b8860b;font-family:' Cambria?,?serif?;?=''>The
                Insured<o:p></o:p></span></b></p>

                <table class='MsoNormalTable' border='0' cellspacing='0' cellpadding='0' style='border-collapse:collapse;mso-padding-alt:0in 0in 0in 0in'>
                <tr style = 'mso-yfti-irow:0;mso-yfti-firstrow:yes;page-break-inside:avoid' >
                < td width='189' valign='top' style='width:141.75pt;padding:0in 3.4pt 0in 3.4pt'>
                <p>Insured Name</p>
                </td>
                <td width = '378' valign= 'top' style= 'width:283.45pt;padding:0in 3.4pt 0in 3.4pt' >
                < p > NOC N DROP</p>
                </td>
                </tr>
                <tr style = 'mso-yfti-irow:1;page-break-inside:avoid' >
                < td width= '189' valign= 'top' style= 'width:141.75pt;padding:0in 3.4pt 0in 3.4pt' >
                < p > Contact Person</p>
                </td>
                <td width = '378' valign= 'top' style= 'width:283.45pt;padding:0in 3.4pt 0in 3.4pt' >
                < p >< span class='SpellE'>N</span></p>
                </td>
                </tr>
                <tr style = 'mso-yfti-irow:2;page-break-inside:avoid' >
                < td width='189' valign='top' style='width:141.75pt;padding:0in 3.4pt 0in 3.4pt'>
                <p>Identity Number</p>
                </td>
                <td width = '378' valign= 'top' style= 'width:283.45pt;padding:0in 3.4pt 0in 3.4pt' >
                < p > 0 </ p >
                </ td >
                </ tr >
                < tr style= 'mso-yfti-irow:3;page-break-inside:avoid' >
                < td width= '189' valign= 'top' style= 'width:141.75pt;padding:0in 3.4pt 0in 3.4pt' >
                < p > Suburb </ p >
                </ td >
                < td width= '378' valign= 'top' style= 'width:283.45pt;padding:0in 3.4pt 0in 3.4pt' >
                < p > George, George, Western Cape</p>
                </td>
                </tr>
                <tr style = 'mso-yfti-irow:4;page-break-inside:avoid' >
                < td width= '189' valign= 'top' style= 'width:141.75pt;padding:0in 3.4pt 0in 3.4pt' >
                < p >< span class='SpellE'>Fitment</span> Branch</p>
                </td>
                <td width = '378' valign= 'top' style= 'width:283.45pt;padding:0in 3.4pt 0in 3.4pt' >
                   < p > Wise Cracks</p>
                   </td>
                </tr>
                <tr style = 'mso-yfti-irow:5;page-break-inside:avoid' >
                   < td width= '189' valign= 'top' style= 'width:141.75pt;padding:0in 3.4pt 0in 3.4pt' >
                   < p > Cellphone Numbers</p>
                   </td>
                <td width = '378' valign= 'top' style= 'width:283.45pt;padding:0in 3.4pt 0in 3.4pt' >
                   < p > 0413740740 </ p >
                   </ td >
                   </ tr >
                   < tr style= 'mso-yfti-irow:6;page-break-inside:avoid' >
                   < td width= '189' valign= 'top' style= 'width:141.75pt;padding:0in 3.4pt 0in 3.4pt' >
                   < p > Business Number</p>
                   </td>
                <td width = '378' valign= 'top' style= 'width:283.45pt;padding:0in 3.4pt 0in 3.4pt' >
                   < p > 0413740740 </ p >
                   </ td >
                   </ tr >
                   < tr style= 'mso-yfti-irow:7;mso-yfti-lastrow:yes;page-break-inside:avoid' >
                   < td width= '189' valign= 'top' style= 'width:141.75pt;padding:0in 3.4pt 0in 3.4pt' >
                   < p > Home Number</p>
                   </td>
                <td width = '378' valign= 'top' style= 'width:283.45pt;padding:0in 3.4pt 0in 3.4pt' >
                   < p > 0448751000 </ p >
                   </ td >
                   </ tr >
                   </ table >
                   </ table >
                   </ div > '";