如何使用asp.net在邮件中发送gridview

时间:2013-12-10 04:50:26

标签: c# asp.net email

我是asp.net的新手。现在经理让我写一个函数来使用asp.net在邮件中发送网格视图。我什么都不做..请给出一步一步的教程链接......

 protected void Page_Load(object sender, EventArgs e)
    {

    }

1 个答案:

答案 0 :(得分:1)

使用此Code

protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = getHTML(GridView1);
    }

    private string getHTML(GridView gv) 
    { 
        StringBuilder sb = new StringBuilder(); 
        StringWriter textwriter = new StringWriter(sb); 
        HtmlTextWriter htmlwriter = new HtmlTextWriter(textwriter); 
        gv.RenderControl(htmlwriter); 
        htmlwriter.Flush(); 
        textwriter.Flush(); 
        htmlwriter.Dispose(); 
        textwriter.Dispose(); 
        return sb.ToString(); 
    }

发送邮件:

 SmtpClient smtpClient = new SmtpClient("mail.MyWebsiteDomainName.com", 25);

    smtpClient.Credentials = new System.Net.NetworkCredential("info@MyWebsiteDomainName.com", "myIDPassword");
                smtpClient.UseDefaultCredentials = true;
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.EnableSsl = true;
                MailMessage mail = new MailMessage();


                //Setting From , To and CC
                mail.From = new MailAddress("info@MyWebsiteDomainName", "MyWeb Site");
                mail.To.Add(new MailAddress("info@MyWebsiteDomainName"));
                mail.CC.Add(new MailAddress("MyEmailID@gmail.com"));


    smtpClient.Send(mail);