ASP.NET郵件提醒功能按每月的哪天進行發送

前臺全部代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MailPJQGD.aspx.cs" Inherits="MailPJQGD" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick">
            </asp:Timer>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

 

后臺代碼:

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.Data.SqlClient;
using System.Net.Mail;

public partial class MailPJQGD : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    //public void SendMail(string strSmtpServer, string UserName, string Password, string strFrom, string strto, string strSubject, string strBody)

    public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
    {
        //生成一個使用Smtp發送郵件的客戶端對象;
        System.Net.Mail.SmtpClient Client = new SmtpClient(strSmtpServer);
        Client.Port = 25;
        Client.UseDefaultCredentials = false;//表示以当前登录用户的默认凭据进行身份验证 
        //Client.Credentials = new System.Net.NetworkCredential(UserName, Password);
        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);  //发送
    }
    public static int iii = 0;
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (iii == 0)
        {
            string day = DateTime.Now.Day.ToString();
            if (day == "15")
            {
                string server = "192.168.2.8";//定义服务器地址
                string sendEmail = "mis10@tjpcb.com";//定义发件人邮箱
                string sendEmailPassWord = "123456";//定义发件人邮箱密码
                string getEmail = "lishuanzhu@tjpcb.com";//获取收件人的邮箱地址
                string titleEmail = "配件請購單需務數量明細導出";//定义邮件的主题
                string context = "";//定义邮件的内容
                string str = "server=192.168.3.127;database=FlowMaster;UId=sa;password=bruce_zhao888418";
                SqlConnection sqlcon = new SqlConnection(str);
                sqlcon.Open();
                string sqlstr = @"
select  distinct  * from (
select  nows,ApplyerDeptName,pinming1,norm1,Material1,quantity1
from WLQGD_View 
where applydatetime>='2010-8-15' and applydatetime<='2010-9-14' and  Status = '100'  and
(quantity1<>'0' and quantity1 is not null and quantity1<>'' and pinming1<>'' and pinming1 is not null)
union all
select  nows,ApplyerDeptName,pinming2,norm2,Material2,quantity2 
from WLQGD_View 
where applydatetime>='2010-8-15' and applydatetime<='2010-9-14' and  Status = '100' and
(quantity2<>'0' and quantity2 is not null and quantity2<>'' and pinming2<>'' and pinming2 is not null)
union all
select  nows,ApplyerDeptName,pinming3,norm3,Material3,quantity3 
from WLQGD_View 
where applydatetime>='2010-8-15' and applydatetime<='2010-9-14' and Status = '100' and
(quantity3<>'0' and quantity3 is not null and quantity3<>'' and pinming3<>'' and pinming3 is not null)
union all
select  nows,ApplyerDeptName,pinming4,norm4,Material4,quantity4 
from WLQGD_View
where applydatetime>='2010-8-15' and applydatetime<='2010-9-14' and  Status = '100' and
(quantity4<>'0' and quantity4 is not null and quantity4<>'' and pinming4<>'' and pinming4 is not null)
union all
select  nows,ApplyerDeptName,pinming5,norm5,Material5,quantity5 
from WLQGD_View 
where applydatetime>='2010-8-15' and applydatetime<='2010-9-14' and  Status = '100' and
(quantity5<>'0' and quantity5 is not null and quantity5<>'' and pinming5<>'' and pinming5 is not null)
union all
select  nows,ApplyerDeptName,pinming6,norm6,Material6,quantity6 
from WLQGD_View 
where applydatetime>='2010-8-15' and applydatetime<='2010-9-14' and  Status = '100' and
(quantity6<>'0' and quantity6 is not null and quantity6<>'' and pinming6<>'' and pinming6 is not null)
union all
select  nows,ApplyerDeptName,pinming7,norm7,Material7,quantity7 
from WLQGD_View 
where applydatetime>='2010-8-15' and applydatetime<='2010-9-14' and  Status = '100' and
(quantity7<>'0' and quantity7 is not null and quantity7<>'' and pinming7<>'' and pinming7 is not null)
union all
select  nows,ApplyerDeptName,pinming8,norm8,Material8,quantity8 
from WLQGD_View 
where applydatetime>='2010-8-15' and applydatetime<='2010-9-14' and  Status = '100' and
(quantity8<>'0' and quantity8 is not null and quantity8<>'' and pinming8<>'' and pinming8 is not null)
)A order by nows asc

";
                SqlDataAdapter sqlda = new SqlDataAdapter(sqlstr, sqlcon);
                DataSet ds = new DataSet();
                string tableStr = @"<table border='0' cellpadding='0'  cellspacing='0' style='font-family:宋体; font-size:14px; color:Blue;'>
           <tr><td>您好:</td></tr>
           <tr><td>&nbsp;&nbsp;&nbsp;如下為2010-8-15至2010-9-14日,請購單需備數量明細,請收悉!</td></tr>
        </table><br/><p></p><table border='0' cellpadding='0'  cellspacing='0' style='font-family:宋体; font-size:14px; color:Blue;' align='left'>
         <tr><td>軟硬體維護組<br /> 敬呈 </td></tr>
       </table><br/><p></p><br/><p></p><table border='1' cellpadding='0' cellspacing='0' align='left'>
                    <tr style='color: #FFFFFF;background-color: #FF9900;' align='center'><td>填单时间</td><td>申請課別</td><td>品名</td><td>規格</td><td>品牌材質</td><td>需備數量</td></tr>";
                sqlda.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = ds.Tables[0].Rows.Count; i > 0; i--)
                    {
                        string nows = ds.Tables[0].Rows[i - 1][0].ToString();
                        string ApplyDepName=ds.Tables[0].Rows[i - 1][1].ToString();
                        string pingming1 = ds.Tables[0].Rows[i - 1][2].ToString();
                        string norm1 = ds.Tables[0].Rows[i - 1][3].ToString();
                        string Material1= ds.Tables[0].Rows[i - 1][4].ToString();
                        string quantity1 = ds.Tables[0].Rows[i-1][5].ToString();
                        tableStr += @"
<tr align='center'><td style='font-size:12px;width:200px;'>" + nows + @"</td><td style='font-size:12px;width:200px;'>" + ApplyDepName + @"</td><td style='font-size:12px;width:200px;'>" + pingming1 + @"</td><td style='font-size:12px;width:200px;'>" + norm1 + @"&nbsp;</td><td style='font-size:12px;width:200px;'>" + Material1 + @"&nbsp;</td><td style='font-size:12px;width:200px;'>" + quantity1 + @"</td></tr>
";
                    }
                    tableStr += @"</table>";
                    SendSMTPEMail(server, sendEmail, sendEmailPassWord, getEmail, "配件請購單需務數量明細導出", tableStr);
                    SendSMTPEMail(server, sendEmail, sendEmailPassWord, "mis12@tjpcb.com", titleEmail, tableStr);
                    SendSMTPEMail(server, sendEmail, sendEmailPassWord, "qianshengjun@tjpcb.com", titleEmail, tableStr);
                }
            }
            iii++;
            Label1.Text = "success";
        }
    }

}

posted @ 2010-09-15 10:21  Aliceblogs  阅读(865)  评论(0编辑  收藏  举报