_cuclife.com
当前位置:cuclife.com > IT > ASP.NET >

ASP.NET邮件按时间发送

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Data.SqlClient;
using System.Net.Mail;


public partial class PJQGD : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
    {
        System.Net.Mail.SmtpClient client = new SmtpClient(strSmtpServer);
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass);
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        System.Net.Mail.MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.IsBodyHtml = true;
        client.Send(message);
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        string week = DateTime.Now.DayOfWeek.ToString();//獲取星期;
        if (week == "Sunday")     //周日就不發送;
        {

        }
        else
        {
            int nowhour = Convert.ToInt32(DateTime.Now.Hour.ToString());  //獲取當前的小時數;
            if (nowhour == 8 || nowhour == 9 || nowhour == 10 || nowhour == 11 || nowhour == 12 || nowhour ==14  || nowhour == 15 || nowhour == 16 || nowhour == 17)
            {
                int it = 1;
                while (it < 1)
                {
                    Thread.Sleep(1000);
                    it++;
                }
                if (it == 1)
                {
                    try
                    {
                        string str = "server=192.168.3.127;database=FlowMaster;UId=sa;password=bruce_zhao888418";
                        SqlConnection sqlcon = new SqlConnection(str);
                        sqlcon.Open();
                        SqlDataAdapter sqlda = new SqlDataAdapter(@"select * FROM PJQGD_View with(updlock) WHERE  (ApplyID = 'maintain' OR  ApplyID = '00181' OR ApplyID = '01147' OR ApplyID = '02657') AND (nows >= '2010/7/13') AND (flag IS NULL or flag = '') AND (Step = 'CD02E866-68B4-47DF-9835-028AE56F7241') and  1=1", sqlcon);
                        DataSet ds = new DataSet();
                        sqlda.Fill(ds);
                        int num = ds.Tables[0].Rows.Count;
                        if (num > 0)
                        {
                            for (int i = 0; i < num; i++)
                            {
                                string Subject = ds.Tables[0].Rows[i][6].ToString();
                                string explain = ds.Tables[0].Rows[i][7].ToString();
                                string pinming1 = ds.Tables[0].Rows[i][8].ToString();
                                string pinming2 = ds.Tables[0].Rows[i][9].ToString();
                                string pinming3 = ds.Tables[0].Rows[i][10].ToString();
                                string pinming4 = ds.Tables[0].Rows[i][11].ToString();
                                string pinming5 = ds.Tables[0].Rows[i][12].ToString();
                                string norm1 = ds.Tables[0].Rows[i][13].ToString();
                                string norm2 = ds.Tables[0].Rows[i][14].ToString();
                                string norm3 = ds.Tables[0].Rows[i][15].ToString();
                                string norm4 = ds.Tables[0].Rows[i][16].ToString();
                                string norm5 = ds.Tables[0].Rows[i][17].ToString();
                                string SequenceID=ds.Tables[0].Rows[i][18].ToString();

                                string server = "192.168.2.8";//定义服务器地址
                                string sendEmail = "定义发件人邮箱
                                string sendEmailPassWord = "123456";//定义发件人邮箱密码
                                string getEmail = "获取收件人的邮箱地址
                                string titleEmail = "統將配件請購單維護已填寫";//定义邮件的主题
                                string context = "表單流水號為:" + SequenceID +"<br>"+ "表單主旨:" + Subject + "<br>" + "說明:" + explain + "<br>" + "產品名1為:" + pinming1 + "產品規格:" + norm1 + "<br>" + "產品名2為:" + pinming2 + "產品規格:" + norm2 + "<br>" + "產品名3為:" + pinming3 + "產品規格:" + norm3 + "<br>" + "產品名4為:" + pinming4 + "產品規格:" + norm4 + "<BR>" + "產品名5為:" + pinming5 + "產品規格:" + norm5;//定义邮件的内容
                                SendSMTPEMail(server, sendEmail, sendEmailPassWord, getEmail, titleEmail, context);
                                SendSMTPEMail(server,sendEmail,sendEmailPassWord,");
                                SendSMTPEMail(server, sendEmail, sendEmailPassWord, "", titleEmail, context);

                            }
                            string sqlstr = @"update PJQGD_View set flag='havingSend' where ((ApplyID='maintain') or (ApplyID='00181') or (ApplyID='01147') or  (ApplyID='02657')) and  (nows >='2010/7/13') and (Step='CD02E866-68B4-47DF-9835-028AE56F7241')";
                            SqlCommand cmd = new SqlCommand(sqlstr, sqlcon);
                            cmd.ExecuteNonQuery();
                        }
                    }
                    catch (Exception er)
                    {
                    }
                }
            }
        }
    }
}

文章来源:网络整理  本站编辑:兰特
上一篇:vs2005调试js
下一篇:三种ASP.NET生成静态页面方法比较
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)