尝试运行表单时出现ObjectDisposedException

时间:2013-07-26 13:15:08

标签: 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 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。

Form1

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服务器的人部署它时,它在编译后第一次正确运行,但在此之后的所有时间它第一次发送一个缓存的电子邮件,表明第一次运行表内容。

非常感谢任何帮助。如果我没有提供足够的信息,请告诉我。

0 个答案:

没有答案