0

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);
        }

    }
Mac
  • 3
  • 2
  • you can put html content in one file and read in one varible and after you can manipulate that – Hitesh Thakor Aug 31 '16 at 11:45
  • What error is it showing? and is it a compilation error? – Mr_Hmp Aug 31 '16 at 11:45
  • can you please show me the example looking at what i have done so far. I have no idea on how i can do that – Mac Aug 31 '16 at 11:46
  • 1
    My suggestion would be to keep the html template in a file and populate it later in time. You can even write the template following razor template and populate it using [razorengine](https://github.com/Antaris/RazorEngine) – Swagata Prateek Aug 31 '16 at 11:47
  • @Mr_Hmp I can't get the html work as it shows error all over – Mac Aug 31 '16 at 11:48
  • That is because you are writing the complete HTML in one line. I will recommend you to write this HTML in a file and read the content from that HTML. – Mr_Hmp Aug 31 '16 at 11:51

2 Answers2

1

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 > '";
Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59
0

This is a mess.

First of all, you're gonna wanna strip that HTML code of all line breaks and place it into a single line.

Next, you're going to want to escape your string by prepending a backslash to every instance of a quotation mark within your string. (This will stop the compiler from thinking you've ended your string).

See example:

var escapedString = "The woman said: \"Hello, young man.\"";

Then, you're gonna want to use some form of string formatting to include all of those other variables within the HTML string. (I recommend string interpolation)

ThePerplexedOne
  • 2,920
  • 15
  • 30