1

我是一名新的 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());
        }
    }
}

当我调试它显示行 Application.Run(new Form1()); 正在抛出 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();

        }
    }
}

当运行网络服务器的人部署它时,它在编译后第一次正确运行,但在此之后的所有时间,它第一次发送缓存的电子邮件,其中包含第一次运行时的表格内容。

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

4

0 回答 0