• datagridview to html table and send email


    代码
    //binding data from xml file to datagridview
    DataSet ds = new DataSet();
    ds.ReadXml(
    "dataFile.xml");//xml file placed in bin/Debug folder
    dataGridView1.DataSource = ds;
    dataGridView1.DataMember
    = "authors";

    private StringBuilder htmlMessageBody(DataGridView dg)
    {
    StringBuilder strB
    = new StringBuilder();
    //create html & table
    strB.AppendLine("<html><body><center><" +
    "table border='1' cellpadding='0' cellspacing='0'>");
    strB.AppendLine(
    "<tr>");
    //cteate table header
    for (int i = 0; i < dg.Columns.Count; i++)
    {
    strB.AppendLine(
    "<td align='center' valign='middle'>" +
    dg.Columns[i].HeaderText
    + "</td>");
    }
    //create table body
    strB.AppendLine("<tr>");
    for (int i = 0; i < dg.Rows.Count; i++)
    {
    strB.AppendLine(
    "<tr>");
    foreach (DataGridViewCell dgvc in dg.Rows[i].Cells)
    {
    strB.AppendLine(
    "<td align='center' valign='middle'>" +
    dgvc.Value.ToString()
    + "</td>");
    }
    strB.AppendLine(
    "</tr>");

    }
    //table footer & end of html file
    strB.AppendLine("</table></center></body></html>");
    return strB;}

    // Create a message with datagridview contents
    // in its body and set up the recipients.
    MailMessage myMessage = new MailMessage();
    try
    {
    myMessage.From
    = "from@yourDomain.com";//place here from address
    myMessage.To = "to@recipientsDomain";//place here to address
    myMessage.Cc = "cc@someDomain.com";//place here copy address
    myMessage.BodyEncoding = Encoding.UTF8;
    myMessage.BodyFormat
    = MailFormat.Html;
    //call method, creating HTML from datagridview
    myMessage.Body = htmlMessageBody(dataGridView1).ToString();
    //place here your subject
    myMessage.Subject = "message subject";

    //if it is needed set up credentials
    myMessage.Fields.Add("http://schemas.microsoft.com/cdo/" +
    "configuration/smtpauthenticate",
    "1");//Basic Authentication
    myMessage.Fields.Add("http://schemas.microsoft.com/cdo/" +
    "configuration/sendusername",
    "userName");//user name
    myMessage.Fields.Add("http://schemas.microsoft.com/cdo/" +
    "configuration/sendpassword",
    "password");//password
    //place here SMTP server
    SmtpMail.SmtpServer = "your SMTP server";
    SmtpMail.Send(myMessage);
    MessageBox.Show(
    "Message sent!");
    }
    catch (Exception ex)
    {
    MessageBox.Show(
    "Error! "+ex.Message);
    }

    //create message with datagridview contents in attachment
    MailMessage myMessage = new MailMessage();
    try
    {
    myMessage.From
    = "from@yourDomain.com";//place here from address
    myMessage.To = "to@recipientsDomain";//place here to address
    myMessage.Cc = "cc@someDomain.com";//place here copy address
    myMessage.BodyEncoding = Encoding.UTF8;
    myMessage.BodyFormat
    = MailFormat.Html;
    myMessage.Body
    = "some body text";//place here some text
    myMessage.Subject = "message subject";//place here your subject
    //creating attachment
    StreamWriter sw = new StreamWriter("file.html",
    true, Encoding.UTF8);//creating html file
    //write datagridview contents to HTML file
    sw.Write(htmlMessageBody(dataGridView1).ToString());
    sw.Close();
    //create attachment
    MailAttachment myAttach = new MailAttachment("file.html");
    myMessage.Attachments.Add(myAttach);
    //add attachment to our message

    //if it is needed set up credentials
    myMessage.Fields.Add("http://schemas.microsoft.com/cdo/" +
    "configuration/smtpauthenticate",
    "1");//Basic Authentication
    myMessage.Fields.Add("http://schemas.microsoft.com/cdo/" +
    "configuration/sendusername",
    "userName");//user name
    myMessage.Fields.Add("http://schemas.microsoft.com/cdo/" +
    "configuration/sendpassword",
    "password");//password
    //place here SMTP server
    SmtpMail.SmtpServer = "your SMTP server";
    SmtpMail.Send(myMessage);
    MessageBox.Show(
    "Message sent!");
    }
    catch (Exception ex)
    {
    MessageBox.Show(
    "Error! " + ex.Message);
    }
  • 相关阅读:
    JDK所有版本
    application.yml配置log日志
    eclipse配置lombok
    Eclipse配置springboot
    java 连接mongodb
    MongoDB shell操作
    mysql插入一万条数据
    Web设计精髓(转)
    SyntaxHighlighter -- 代码高亮插件
    input之placeholder与行高的问题。
  • 原文地址:https://www.cnblogs.com/zzandww/p/1729087.html
Copyright © 2020-2023  润新知