简体   繁体   中英

Send HTML email with Mysql data into a table from within application

I have an application that i'm using to send an email that contains an entire table and i don't know how to get that dynamic data there. I'M SURE i have to use a for loop but i don't know how to word it to get the rows of data.

Here is what i have so far:

    msg.To.Add(selected);//put in custom account info
        msg.Subject = "Message Subject";
        msg.IsBodyHtml = true;
        msg.Body = "<html>"+
        "<body>"+
        "<center>"+
        "<h2>Your entire inventory:</h2>"+
        "<table>"+
        "<th>Product</th>"+
        //for each loop

        "<th>Quantity</th>"+
        //for each loop
            "</body>"+
            "</html>";

Let me know if you need any other code.

Personally, I would avoid building HTML using string concatenation. Not only can it make your code awkward, but you also risk accidental mistakes in encoding your output. One alternative is to use LINQ to XML to build your HTML. The code would look something like this:

var html = new XDocument(
    new XElement("html",
        new XElement("body",
            new XElement("h2", "Your entire inventory:"),
            new XElement("table",
                new XElement("tr",
                    new XElement("th", "Product"),
                    from item in orders
                    select new XElement("td", item.ProductName)),
                new XElement("tr",
                    new XElement("th", "Quantity"),
                    from item in orders
                    select new XElement("td", item.Quantity))))));

msg.Body = html.ToString();

I assume you already have the MySQL Connector for .NET . Here's some code that should get you going in the right direction. If you don't know how to read from a database using C# you should read up on that. MySQL Connector Documentation

Just a simple Product class to hold what you need.

public class Product
{
    public String ProductName { get; set; }
    public Int32 Quantity { get; set; }
}

Here is just a quick database class overview.

 public class DB
{
    string connString = "Your Database Connection String";

    public List<Product> GetProductsAndQuantity()
    {
        string query = "SELECT product, quantity FROM yourTable";
        List<Product> prodList = new List<Product>();

        using (SqlConnection connection = new SqlConnection(connString))
        {

            SqlCommand cmd = new SqlCommand(query, connection);
            connection.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            try
            {
                while (reader.Read())
                {
                    // Create new Product
                    Product p = new Product();
                    p.ProductName = (String)reader[0];
                    p.Quantity = (Int32)reader[1];
                    // Add product to list
                    prodList.Add(p);
                }
            }
            finally
            {
                reader.Close();
            }
        }

        return prodList;
    }
}

This is where you're building your email.

DB db = new DB();

List<Product> prodList = db.GetProductsAndQuantity();
            msg.Body...
            msg.Body += " <table> <tr><th>Product</th> <th>Quantity</th></tr> ";

            foreach (Product p in prodList)
            {
                msg.Body += "<tr><td>" + p.ProductName + "</td><td>"
                         + p.Quantity + "</td></tr>";
            }

            msg.Body += "</table> ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM