私は新しい 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();
}
}
}
ウェブサーバーを実行する人がデプロイすると、コンパイル後に初めて正しく実行されますが、その後は常に、最初に実行したときにテーブルの内容がキャッシュされた電子メールを送信します。
どんな助けでも大歓迎です。十分な情報を提供していない場合は、お知らせください。