Click here to Skip to main content
15,884,793 members
Please Sign up or sign in to vote.
4.38/5 (5 votes)
See more:
i have created a console application to send mail now i have html templates
i am getting data from database according to user now how to bind this data in html.
my code is
C#
private static void Main(string[] args)
        {
            // Get All User
            var userList = BLLMailAlert.GetAllSubscribedUser();
            // Send UserID as Parameter and Find Tenders For Users
            foreach (var user in userList)
            {
                BLLMailAlert.GetAllTenderForPaidUser(user.UserID);

                // For Each AlertId Find Template Name and EmailId
                foreach (var usr in userList)
                {
                    var detail = BLLMailAlert.GetUserAllDetail(usr.AlertID);

                    // According to TemplateName FIND Template and Write Tender in that Template
                    switch (usr.UserTypeTemplate.TemplateType.TemplateTypeName)
                    {
                        case "TemplateForPaidUser.htm":
                        {
                            // Send Template in Mail To Each User
                            var fromEmail = string.Empty;
                            var toEmail = string.Empty;
                            toEmail = usr.Email;
                            fromEmail = Convert.ToString(ConfigurationManager.AppSettings["adminmail"]);
                            const string TemplatePath = "[Path to resource]";
                            var mailContent = new StreamReader(TemplatePath).ReadToEnd();
                            mailContent += detail;
                            BLLMailAlert.SendTenderAlert(fromEmail, toEmail, mailContent);
                        }

                            break;
                    }
                }
            }
        }

 //function to send mail
// I have Class named Mail which i am using to send mail
public static bool SendTenderAlert(string fromEmail, string toEmail, string mailContent)
        {
            try
            {
                var currentDate = System.DateTime.Now.Date;
                Mail.SendHtmlMail(fromEmail, toEmail, "TenderKhabar | Fresh Tenders Of |" + currentDate, mailContent);
                return true;
            }
            catch
            {
                return false;
            }
        }
Posted
Updated 26-Nov-13 20:42pm
v2
Comments
Can you post the SendTenderAlert function code? What is the problem there?
V1CT0R. 27-Nov-13 2:39am    
function SendTenderAlert is just for sending mail the problem is how to write data in html template which i am getting in var detail = BLLMailAlert.GetUserAllDetail(usr.AlertID);
V1CT0R. 27-Nov-13 2:48am    
Like if there are only few fields i can append it in string builder but the detail i am getting is List and i can't use gridview as i am working on console application
You can make one html table by looping through the results.

Hey Victor
To Send HTML Template in Console Application You Can use String Builder and StreamReader and try to Append and replace the Content For more Detail See this StringBuilder.Append Method and StringBuilder.Replace Method
So your Code will be Like
C#
//create String Builder
var sbMail = new StringBuilder();
//for iteration You can use foreach 
foreach(--Your Condition--)
{
  using (var sReader = new StreamReader(TemplatePath))
   {
     sbMail.Append(sReader.ReadToEnd());
     sbMail.Replace("{YourTitle}", DataSource.YourTitle);
     .............
     sbMail.Append("br/");
   }
}

now in html template
try something Like that
<table>>
<tr>
  <td>
     {YourTitle}
  </td>
</tr>
</table>


that will replace {YourTitle} and Append the next {YourTitle} in the Mail
Hop That Helps..........
 
Share this answer
 
v3
You can try somthing like this to mak your mail body nice ...... :D

C#
string GetBody()
{
   string PreDefinedMailFormat = @"<html>
                                <head>
                                    <title></title>
                                </head>
                                <body>
                                    <div>
                                        <span style='font-size:12px;'><span style='font-family:verdana,geneva,sans-serif;'>Hi {1} {2},</span></span></div>
                                    <div>
                                        &nbsp;</div>
                                    <div>
                                        <span style='font-size:12px;'><span style='font-family:verdana,geneva,sans-serif;'><strong>Congratulations!</strong> {0} has received and accepted your invitation.</span></span></div>
                                    <div>
                                </body>
                            </html>
                            ";

   string MailReceiverFName = "Moumit";
   string MailReceiverLName = "Mondal";
   string MailSenderName = "KAKA";

            //Final mail body
   string MailBody = string.Format(PreDefinedMailFormat, MailSenderName, MailReceiverFName, MailReceiverLName);
   return MailBody;
}
 
Share this answer
 
v3
Comments
V1CT0R. 27-Nov-13 5:40am    
@Moumit Mondal the problem is i am getting data in List so i need something like gridview in Console application
Moumit Mondal 28-Nov-13 5:30am    
Whys that??? Sorry @V1CT0R. your problem is not clear to me ... :(
V1CT0R. 28-Nov-13 10:58am    
well suppose I am getting a List Of Customer in That List there are approx 15-20 Customer
and each user has some details like 6 or 7 fields now as we can bind them simply in GridView
by DataBind method and write the table column's name BoundField's DataField I want to do similar thing in Console application........
Moumit Mondal 29-Nov-13 8:23am    
In that case it's out of my hands :( ... I did not worked in console ... But if you wish then can use trick like this...

void ShowData(DataTable dt)
{

foreach (DataColumn item in dt.Columns)
{
Console.Write(item.ColumnName);
Console.Write("\t");
}

Console.Write(Environment.NewLine);

for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
Console.Write(dt.Rows[i][j]);
Console.Write("\t");
}
Console.Write(Environment.NewLine);
}

Console.ReadKey();
}
Vishal Pand3y 30-Nov-13 9:19am    
its better to use HTML template rather than saving Template in database

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900