我想每天下午5点发送邮件 [英] I want to send mail daily at 5:00PM

查看:79
本文介绍了我想每天下午5点发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



为了每天下午5:00发送邮件,我使用了Windows服务。我已在Windows应用程序中测试了发送邮件应用程序,然后我在Windows服务中使用过。我认为在服务中使用代码有问题,因为服务无法发送邮件。



我还有一个问题。假如我在服务中做了一些修改我应该卸载并重新安装服务还是重新启动服务就足够了?



这是我在windows中使用的下面的代码服务。





Hi,
In order to send mail daily at 5:00PM I have used Windows services. I have tested the sending mail application in windows application and then I have used in Windows services. Im thinking that there is something wrong on using the code in the service because from the service im unable to send mails.

I have one more question. Suppose if I made some modifications in the service should I uninstall and reinstall the service or restarting the service is enough?

Here is the code below that i have used in windows service.


public partial class DailyAttendanceService : ServiceBase
    {
        List<int> parentId = new List<int>();
        List<string> strEmailIds = new List<string>();
        Timer timer = new Timer();
        DataTable dt;
        DataSet ds;
        public DailyAttendanceService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            timer.Interval = 1000;
            timer.Enabled = true;
           timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            DateTime dt1 = Convert.ToDateTime(DateTime.Now.ToString("HH:mm"));
            DateTime dt2 = Convert.ToDateTime("05:00 PM");
            int result = DateTime.Compare(dt1, dt2);
            if (result == 0)
            {
                SendEmail();

            }
        }

        protected override void OnStop()
        {
            timer.Enabled = false;
        }

        public void SendEmail()
        {
            DateTime SubjectDate = Convert.ToDateTime("05/01/2013");
            string spName = "spAttendanceDet";
            SqlParameter pSubjectDate = new SqlParameter("@SubjectDate", SqlDbType.DateTime);
            SqlParameter pTotal = new SqlParameter("@total", SqlDbType.Int);
            SqlParameter pSemister = new SqlParameter("@Semister", SqlDbType.VarChar, 50);
            pSubjectDate.Value = "01 May 2013";
            pTotal.Value = 7;
            pSemister.Value = "2-1";


            ds = SqlHelper.ExecuteDataset(Helper.ConnectionString, CommandType.StoredProcedure, spName, pSubjectDate, pTotal, pSemister);
            dt = new DataTable();
            // da.Fill(ds);
            dt = ds.Tables[0];
            //DataRow dr1 = new DataRow();
            //dr1 = dt.Select("FKParentId" +)
            foreach (DataRow dr in dt.Rows)
            {
                int present = Convert.ToInt32(dr[0]);
                int total = Convert.ToInt32(dr[1]);
                int abscent = Convert.ToInt32(dr[2]);
                int fkParentId = Convert.ToInt32(dr[5]);
                string pEmailId = new ParentBO().GetParentById(fkParentId).PEmailId;


                string from = "abc@gmail.com"; //Replace this with your own correct Gmail Address

                string to = pEmailId; //Replace this with the Email Address to whom you want to send the mail




                StringBuilder strBuilder = new StringBuilder();
                strBuilder.Append("<Table>");
                strBuilder.Append("<tr>");
                strBuilder.Append("<td>");
                strBuilder.Append("Daily Attendance Report");
                strBuilder.Append("</td>");
                strBuilder.Append("</tr>");
                strBuilder.Append("<tr>");
                strBuilder.Append("<td  colspan=3>");
                strBuilder.Append("--------------------------------------------------------");
                strBuilder.Append("</td>");
                strBuilder.Append("</tr>");
                strBuilder.Append("<tr>");
                strBuilder.Append("<td>");
                strBuilder.Append("Present");
                strBuilder.Append("</td>");
                strBuilder.Append("<td>");
                strBuilder.Append("Total");
                strBuilder.Append("</td>");
                strBuilder.Append("<td>");
                strBuilder.Append("Abscent");
                strBuilder.Append("</td>");
                strBuilder.Append("</tr>");

                strBuilder.Append("<tr>");
                strBuilder.Append("<td>");
                strBuilder.Append(present);
                strBuilder.Append("</td>");
                strBuilder.Append("<td>");
                strBuilder.Append(total);
                strBuilder.Append("</td>");
                strBuilder.Append("<td>");
                strBuilder.Append(abscent);
                strBuilder.Append("</td>");
                strBuilder.Append("</tr>");

                strBuilder.Append("</Table>");

                System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                mail.To.Add(to);


                mail.From = new MailAddress(from, "College", System.Text.Encoding.UTF8);
                mail.Subject = "Daily Attendance Report";
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                mail.Body = strBuilder.ToString();

                mail.BodyEncoding = System.Text.Encoding.UTF8;
                mail.IsBodyHtml = true;
                mail.Priority = MailPriority.High;

                SmtpClient client = new SmtpClient();
                //Add the Creddentials- use your own email id and password

                client.Credentials = new System.Net.NetworkCredential(from, "xyz");

                client.Port = 587; // Gmail works on this port
                client.Host = "smtp.gmail.com";
                client.EnableSsl = true; //Gmail works on Server Secured Layer
                try
                {
                    client.Send(mail);
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    string errorMessage = string.Empty;
                    while (ex2 != null)
                    {
                        errorMessage += ex2.ToString();
                        ex2 = ex2.InnerException;
                    }

                } // end try
            }


        }
    }

推荐答案

1.修改后您无需重新安装服务。停止服务,复制新的服务exe并再次启动服务。



2.我相信你的主要线程在你的计时器经过时间到期之前就完成了。一些如何保持主线程运行的方法。你需要找到自己的方式。



点击此链接



http://stackoverflow.com/questions/3344633/how-to- make-a-win-service-run-long-term-threading [ ^ ]
1.You don''t need to reinstall service after modification. Stop the service, copy your new service exe and start service again.

2.I believe your primary thread is finished before your timer Elapsed time reached. Some how you need to keep primary thread running. You need to find your way for that.

Follow this link

http://stackoverflow.com/questions/3344633/how-to-make-a-win-service-run-long-term-with-threading[^]


这篇关于我想每天下午5点发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆