我已经浏览了这个网站上的一些答案,以便在特定日期自动发送邮件。
我正在使用 vs 2010,并且我创建了一个未托管的项目。
我为约会创建了一个表格,其中包含电子邮件 ID、开始日期和开始时间。
邮件应在预约前一小时自动发送到人员的电子邮件 ID。
我该怎么做?
我曾尝试使用窗口服务,但对我不起作用。
protected override void OnStart(string[] args)
{
string dateonly = DateTime.Now.ToString("yyyy-MM-dd");
string timeonly = DateTime.Now.ToString("HH:mm tt");
string source = "Data Source=localhost;Initial Catalog=PRO;Integrated Security=SSPI;";
SqlConnection con = new SqlConnection(source);
con.Open();
SqlCommand cmd = new SqlCommand("select * from Appointments where CONVERT(varchar(10),StartDate,101)=CONVERT(varchar(10),GETDATE(),101)", con);
cmd.Parameters.AddWithValue("@date", dateonly);
cmd.Parameters.AddWithValue("@time", timeonly);
cmd.ExecuteNonQuery();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds); //Filling table with user data
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
client.Port = 587; //Google mail port
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("email", "password");
client.UseDefaultCredentials = false;
client.Credentials = credentials;
for each (Data Row Dr in ds.Tables[0].Rows)
{
MailMessage mm = new MailMessage();
mm.To.Add(dr["Email"].ToString());
mm.From = new MailAddress("email");
mm.Subject = "Sending Auto Mail ";
mm.Body = "This email has been send automatically through Windows Service to remind you about your Appointment in next one hour ";
client.Send(mm);
mm.Dispose();
}
}
protected override void OnStop()
{
}