Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在电子邮件基础C中发送gridview_C#_.net_Email_Gridview - Fatal编程技术网

C# 在电子邮件基础C中发送gridview

C# 在电子邮件基础C中发送gridview,c#,.net,email,gridview,C#,.net,Email,Gridview,我想创建一个包含gridview的邮件。我一直在网上搜索,并尝试了多种解决方案。但不知何故,我误解了或做错了什么。使用当前代码,我得到了错误: System.InvalidOperationException: Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource. 我读过关于使用StringBuilder、StringWriter、HtmlTextWrit

我想创建一个包含gridview的邮件。我一直在网上搜索,并尝试了多种解决方案。但不知何故,我误解了或做错了什么。使用当前代码,我得到了错误:

System.InvalidOperationException: Data source is an invalid type. It must be either 
an IListSource, IEnumerable, or IDataSource. 
我读过关于使用StringBuilder、StringWriter、HtmlTextWriter、RenderControl等的文章,但仍然不知道如何实现这一点

这是我的密码:

        public void Email()
    {            
        string conn = "Data Source=pc-..";
        System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(conn);
        sqlConn.Open();
        SqlCommand sendGrid = new SqlCommand("SELECT * FROM tblSomething", sqlConn);

        GridView grd = new GridView();

        if (sendGrid != null)
        {
            grd.DataSource = sendGrid.ExecuteReader();
            grd.DataBind();
        }

        StringBuilder sb = new StringBuilder();

        StringWriter sw = new StringWriter(sb);
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        grd.RenderControl(htw);

        MailMessage mail = new MailMessage();

        mail.To.Add("ToWhoEmail");
        mail.From = new MailAddress("FromWho");
        mail.Subject = "SubjectSomething";
        mail.IsBodyHtml = true;
        mail.BodyFormat = System.Net.Mail.MailMessage.IsBodyHtml.Html;
        // At this line above i get an error. He doesn't recognize BodyFormat = ...;
        // System.Web.Mail.MailFormat is obsolete.
        // The recommended alternative is: System.Net.Mail.MailMessage.IsBodyHtml
        mail.Body = sb.ToString();
        SmtpClient smtp = new SmtpClient("...");
        smtp.Send(mail);}
我还尝试在不同的cs文件中创建gridview,并使用以下命令调用它: 邮件中的SendGrid.MakeGrid.Body=。。。; 但我得到的只是:电子邮件中的System.Web.UI.WebControls.GridView。 非常感谢您的帮助,提前谢谢您


关于Mati,问题出在这一行:

grd.DataSource = sendGrid;
您试图使用SqlCommand作为网格视图的数据源,这是不可能的。您可以使用数据读取器或数据集:

grd.DataSource = sendGrid.ExecuteReader();

另外,请确保通过包装这些连接和命令来正确处理它们。

在上面的代码中,我没有看到邮件发送的smtp设置……设置端口号和服务器访问凭据,如gmail和端口的用户名和密码

smtp.gmail.com使用身份验证

使用身份验证:是

TLS/STARTTLS的端口:587


SSL:465的端口和使用sqlcommanad作为数据源的网格视图数据源的端口不正确,请使用dataadapter…

我的C代码无法识别IsBodyHtml,因为我想我使用的是System.Web而不是System.Net。但我不这么认为。因为编辑了这么多,Sry不得不删除个人信息。@Darin Dimitrov:+1!我真不敢相信我错过了这个。@Mitch Wheat,这可能是因为OP使用了糟糕的命名约定。就我个人而言,我永远不会给SqlCommand变量sendGrid命名。@Darin Dimitrov:我也不会。我最讨厌的事情之一是事物的正确命名;当我再次尝试时,遇到以下错误:System.Web.HttpException 0x80004005:SendUsing配置值无效。-->System.Reflection.TargetInvocationException:调用的目标已引发异常。-->System.Runtime.InteropServices.ComeException:SendUsing配置值无效。-内部异常堆栈跟踪结束-我以前从未见过此错误,不知道如何处理它。据我所知,我没有使用SendUsing的方法或东西。@mati,但在您发布的代码中,我没有看到您指定SMTP服务器。没有SMTP服务器,您无法发送电子邮件。您好,谢谢您的回复。事实上,我注意到我遗漏了一些东西,我以为是SmtpClient smtp=新SmtpClient;我第一次在System.Net.Mail中使用它,所以我又开始改变。在我的代码中唯一显示为错误的atm是:mail.BodyFormat=System.Net.mail.MailMessage.IsBodyHtml.Html;//System.Web.Mail.MailFormat已过时。//建议的备选方案是:System.Net.Mail.MailMessage.IsBodyHtml关于如何修复此问题的任何想法?MailMessage message=新的MailMessagefrom,to,subject,body;message.IsBodyHtml=false;试试这个,并发送邮件作为邮件信息…可能它会帮助你。嘿,病毒,谢谢你调查这一点。我只是删除了这一行就解决了这个问题,一个正确的解决方案是多么愚蠢。