项目中客户提出要实现定时自动发送邮件,比如每个周五23:59:00向业务员发本周个人业绩邮件不可以用winform实现,只能用web方式,特向高人求解网上查了一下,但没有找到好的解决方法,在此求救,顿首叩拜高人指点(也欢迎兄弟们顶贴,共同学习)

解决方案 »

  1.   

    只是發郵件,需要web嗎?總不能每周五半夜起來開網站?
      

  2.   

    客户就有这么变态,你能怎么办?人家就是不愿在服务器上装个winform程序
      

  3.   

    <html>
    <head>
    <meta http-equiv="refresh" content="60" /><!--1分钟刷新一次该页面-->
    </head>
    <body>
    <%
    if(DateTime.Now=="指定时间"){
    //发送邮件代码
    }
    %>
    </body>
    </html>
      

  4.   

     string path = "http://localhost:1728/UI/activation.aspx?id=" + UserMd5(activation);
     SmtpClient mailserver = new SmtpClient(); mailserver.Host = "xx.com";   //发送邮件的邮件服务器
            mailserver.Credentials = new System.Net.NetworkCredential("[email protected]", "emailpwd");        mailserver.Timeout = 5000;
            mailserver.EnableSsl = false;
            //mail邮件设置
            MailMessage mail = new MailMessage();
            MailAddress mfrom = new MailAddress("[email protected]");
            mail.From = mfrom;
            mail.To.Add("" + TextBox1.Text + "");
            mail.Subject = " Activate-ONIBJ 用户名激活";
            mail.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312");
            mail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
            mail.IsBodyHtml = false;
            mail.Body = string.Format(@""+path+"");
            mailserver.Send(mail);
    希望对你有帮助
      

  5.   

    我觉得吧这个有点不切实际,既然要依赖于WEB总得有个页面开着吧你或者可以把发邮件的类放在 global中试试或者你做个Window服务吧,但是貌似这个也是得用winform。。阿门
      

  6.   

    我也想着在global里来做,能不能用Timer?
      

  7.   

    我觉得应该用windows 服务来解决的哦!
      

  8.   

    简单.public delegate void sm();
    sm t = new sm(
    ()=>
    {
      while(true)
      {
       if( 时间 = 23:58:00)发邮件;
       thread.sleep(1000)
       }
    }
    )
    t.BeginInvoke();
    写在哪里都可以,最好是写在global.asax中的application_start 中因为这个方法是死循环,这个异步线程会一直执行.
      

  9.   

    如果你觉得1秒的轮询太短了,增加cpu负担可以改成一分钟
    thread.sleep(60 * 1000);
      

  10.   

    在global.aspx中<%@ Application Language="C#" %>
    <%@ Import Namespace="System.Timers" %><script runat="server">    void Application_Start(object sender, EventArgs e) 
        {
            //在应用程序启动时运行的代码
            System.Timers.Timer objTimer = new System.Timers.Timer();
            objTimer.Interval = 3000; //这个时间单位毫秒,比如10秒,就写10000
            objTimer.Enabled = true;
            objTimer.Elapsed += new ElapsedEventHandler(objTimer_Elapsed);
        }    void objTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if(Application["test"]==null)
            {
                Application["test"] = 0;
            }
            else
            {
                Application["test"] = Convert.ToInt32(Application["test"]) + 1;
            }
        }
        
        void Application_End(object sender, EventArgs e) 
        {
            //在应用程序关闭时运行的代码    }
            
        void Application_Error(object sender, EventArgs e) 
        { 
            //在出现未处理的错误时运行的代码    }    void Session_Start(object sender, EventArgs e) 
        {
            //在新会话启动时运行的代码    }    void Session_End(object sender, EventArgs e) 
        {
            //在会话结束时运行的代码。 
            // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
            // InProc 时,才会引发 Session_End 事件。如果会话模式 
            //设置为 StateServer 或 SQLServer,则不会引发该事件。    }
           
    </script>
      

  11.   

    上面写的不错,简单的示例如下    public delegate void SM();
        void Application_Start(object sender, EventArgs e)
        {
            string s = Server.MapPath(".");
            if (s.EndsWith("\\"))
            {
                s += "t.txt";
            }
            else
            {
                s += "\\t.txt";
            }
            //在应用程序启动时运行的代码
            SM t = new SM(
                () =>
                {
                    while (true)
                    {
                        if (!System.IO.File.Exists(s))
                        {
                            System.IO.File.Create(s).Close();
                        }
                        System.IO.FileStream fs = System.IO.File.Open(s, System.IO.FileMode.Append);
                        System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
                        sw.WriteLine(" {0} ", System.DateTime.Now);
                        sw.Flush();
                        fs.Flush();
                        fs.Close();
                        System.Threading.Thread.Sleep(2000);
                    }
                });
            t.BeginInvoke(null, null);
        }
      

  12.   


    我验证过无问题,代码中不能含有httpconxt上下文的一些东西,如.server ,response. reqeust等等
    也可以用Cache来做,使用绝对过期时间和回调,也可以用timer
    下面改了一下算法,对不对就不知道了,反正看懂思路就行了   public delegate void SM();
        void Application_Start(object sender, EventArgs e)
        {        //在应用程序启动时运行的代码
            SM t = new SM(
                () =>
                {
                    while (true)
                    {
                        //发邮件Code 
                       
                        //计算下一个时间间隔
                        string next = String.Format("{0}-{1}-{2} 23:58:00", System.DateTime.Now.Year, System.DateTime.Now.Month, System.DateTime.Now.Day);
                        DateTime dt = System.DateTime.Parse(next);
                        TimeSpan ts1 = new TimeSpan(System.DateTime.Now.Ticks);
                        TimeSpan ts2 = new TimeSpan(dt.Ticks);
                        TimeSpan ts = ts2.Subtract(ts1);
                        if (ts.Days < 0) { 
                            dt = dt.AddDays(1);
                        }
                        if (ts.Days > 1)
                        {
                            dt = dt.AddDays(-1);
                        }
                        TimeSpan ts3 = new TimeSpan(System.DateTime.Now.Ticks);
                        TimeSpan ts4 = new TimeSpan(dt.Ticks);
                        //使用下一个时间间隔唤醒线程
                        System.Threading.Thread.Sleep(ts4.Subtract(ts3).Milliseconds* 1000);
                    }
                });
            t.BeginInvoke(null, null);
        }
      

  13.   

    在页面中引用Application时出错,未将对象引用设置到对象的实例
      

  14.   

    代码中不能含有httpconxt上下文的一些东西,如.server ,response. reqeust等等
      

  15.   

    为什么这样啊,可我有个地方必须用到HttpContext.Current.Request.Form这个也在不能含有之中吗?我是菜鸟,请问你代码中的() =>这个是什么意思啊
      

  16.   

    因为你线程是不依赖于http上下文的,也就是没有访问也会运行的。它如何知道你的http上下文是什么?如果你一定要用.你可以写到文件或是其它静态变量中,然后在代码中调用.
    ()=> 是lambda表达式, 可以用它来代表一个委托实例相当于
    void dosomething(){
    //你的代码
    }SM t = new SM(dosomething);
    SM t = new SM( ()=>{
    //你的代码;
    })
               
      

  17.   

    有以下实现方案:
    (1)做一个winform 发邮件的。然后通过windows计划任务,设置为制定时间,每次自动运行,运行完毕后自动关闭。
    (2)用sqlserver 数据库实现发邮件(网上很多,用sqlserver实现发邮件的存储过程),然后制定一个作业,制定时间运行。
    (3)在 Global.asax 文件里编程。事件:Application_Start。利用Time类编程。比如服务器1秒钟执行一次判断(不会消耗多少内存的),具体实现如下(参考)
    protected void Application_Start(Object sender, EventArgs e)
    {
                               Timer t=new Timer(1000);//设计时间间隔,如果一个小时执行一次就改为36000000
       t.Elapsed+=new ElapsedEventHandler(t_Elapsed); 
       t.AutoReset=true;
       t.Enabled=true;
    }
                    //发邮件
    private void t_Elapsed(object sender, ElapsedEventArgs e)
    {
                        //发送邮件代码,以下是伪代码
                    if (当前时间==周五23:59:00 或者 这个时间附近,自己看着写代码吧)
                        {
                                发送邮件(网上有的是发送邮件的代码)
      
                         }                }
      

  18.   

    发现奇了,这句代码:
    objTimer.Interval = 3000; //这个时间单位毫秒,比如10秒,就写10000 
    不改动不会报错
    当然改为60*1000时就报那个错了
      

  19.   


    在其他机器写个winform, 调用服务器 某个发送邮件的页面或者webservice . 
      

  20.   

    愚见:  用windows任务计划,定时打开一个网页,之后就好办了。。
      

  21.   

    以前一直跑个 控制台应用程序 实现类似功能。
    现在知道了:
    (1)做一个winform 发邮件的。然后通过windows计划任务,
    (2)用sqlserver 数据库实现发邮件
    (3)在 Global.asax 文件里编程本人还是觉得做个 服务比较稳妥!!
      

  22.   

    嘿嘿,我在这学到了点
    所以决定采用(2)用sqlserver 数据库实现发邮件 ,只是要写储存过程了,明天接着做继续求解用web方式实现
      

  23.   

    用sqlserver可以轻松实现,网上代码一大把,也很简单
      

  24.   

    用winform没什么不好,这个网站http://www.kkkweb.cn就是这样做的。效果比较理想
      

  25.   

    应该有数据库支持吧 
    oracle和sql server  里面有个JOB
    可以试试用这个通过定时调用过程或者函数 实现
     好像还可以通过 服务器做个服务来实现 具体怎么实现 google吧
      
      

  26.   

    那你可以做个任务  win自带的啊
      

  27.   

    用数据库邮件已实现,求web实现的例子,作学习备用
      

  28.   

    web有一个办法,利用缓存Cache 过期事件来触发发送邮件事件    
        private void DoCache(DateTime dt)
        {
            Context.Cache.Insert("CacheKey", "随便一个对象", null, dt, Cache.NoSlidingExpiration,
                CacheItemPriority.Default,
                new CacheItemRemovedCallback(CacheItemRemovedCallbackEvent));    }    
       private void CacheItemRemovedCallbackEvent(string key, object item, CacheItemRemovedReason reason)
        { 
            //触发开始发送邮件
             //重新缓存
             DoCache(DateTime.Parse(DateTime.Now.AddDays(7).ToString("yyyy-MM-dd 23:59:00")));  // 下一周这时候    }
      

  29.   

    个人认为用线程+Trimer控件比较可行吧。
      

  30.   

    貌似用web方式无法自动发email
    1.定时由人打开页面让.net程序发
    2.做个windows服务放在后台,自动发。这个不需要人为操作
    3.做个应用程序挂服务器,打开后不关闭
      

  31.   

    是自己的服务器吗?如果是自己的服务器可以在服务器上一直开着一个网页,用web方式实现,js检查时间是否是要发送邮件的时间,如果是就调用方法来发送邮件,目前我是这么实现的。