Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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# 尝试运行窗体时发生ObjectDisposedException_C#_Forms_Smtp_Objectdisposedexception - Fatal编程技术网

C# 尝试运行窗体时发生ObjectDisposedException

C# 尝试运行窗体时发生ObjectDisposedException,c#,forms,smtp,objectdisposedexception,C#,Forms,Smtp,Objectdisposedexception,我是一名新的C程序员,我正在尝试制作一个程序来发送包含MSSQL表内容的电子邮件。 主程序如下所示: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace NotifySFUpdate { static class Program { /// <summary> /// The m

我是一名新的C程序员,我正在尝试制作一个程序来发送包含MSSQL表内容的电子邮件。 主程序如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace NotifySFUpdate
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
调试时,它显示行应用程序.runnewform1;正在抛出ObjectDisposedException

表格1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.IO;
using System.Text;

namespace NotifySFUpdate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            using (sfDataContext db = new sfDataContext())
            {
                db.queryName();
                var x = from s in db.zz
                        select s;


                MailMessage message = new MailMessage();
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "hostaddress";
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                System.Net.NetworkCredential smtpuser = new System.Net.NetworkCredential("website", "password");
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = smtpuser;



                string outputSubject = "Pages to publish";

                string template;
                using (StreamReader sr = new StreamReader(@"C:\locationofTemplate"))
                {
                    template = sr.ReadToEnd();
                }

                StringBuilder sbNew = new StringBuilder();
                foreach (zz e in x)
                {
                    sbNew.Append(e.URL);
                    sbNew.Append("<br/>");
                }

                template = template.Replace("{pages}", sbNew.ToString());
                message.Subject = outputSubject;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.Body = template;
                message.IsBodyHtml = true;
                message.From = new MailAddress("website@myserver", "nameofInstitution");
                message.To.Add("*myemailAddress*");
                smtp.Send(message);

                db.zz.DeleteAllOnSubmit(x);
                db.SubmitChanges();

            }
            try
            {
                this.Close();
            }
            catch
            {
            }
            Application.Exit();

        }
    }
}
当运行Web服务器的人部署它时,它会在编译后第一次正确运行,但在编译后的所有时间,它都会发送一封缓存的电子邮件,说明它第一次运行时的表内容


任何帮助都将不胜感激。如果我没有提供足够的信息,请告诉我。

为什么要为此使用表单?注意:我意识到异常是由try块触发的。这是为了弄清楚它为什么不能正确运行。@Daniel我将作为一项计划任务从Web服务器上运行它。您有比使用表单更简单/更好的解决方案吗?windows表单适用于桌面应用程序。也许您需要一个windows服务或控制台应用程序项目。