using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Configuration;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Timers;
using System.Threading;
namespace MailApplication
{
class Program
{
public static bool IsSendMail = false;
private int Interval = 1000; //时间间隔,单位毫秒
private int Time = 0; //所显示的时间
public System.Threading.Thread timer = null;
public static int checktimes = 0; //計算檢查次數
public static int copytimes = 0;// //計算copy次數
private void Timer1()
{
while (true)
{
Display(); //显示秒表计数
Thread.Sleep(Interval); //等待1秒后再执行Timer()刷新计数
Time++; //秒数加1
}
}
private void Display()
{
if (Time % 5 == 1)
{
Console.Clear();
Console.SetCursorPosition(0, 0);
Console.Write("治具管理系統正在發送Mail,請不要關閉窗口...");
}
Console.SetCursorPosition(0, 1);
Console.Write("Time:" + Time.ToString() + "秒");
}
public void Start()
{
timer = new Thread(Timer1); //新建一个线程,该线程调用Timer()
timer.Start(); //启动线程
Console.CursorVisible = false; //隐藏光标
//Console.ReadKey(); //等待按任意键退出
}
static void Main(string[] args)
{
Console.Write("治具管理系統發送Mail,請不要關閉窗口...");
//建立線程監控
Program sw = new Program();
sw.Start();
//發送郵件對象
SendEmail objemail = new SendEmail();
//Mail服務器
objemail.Server = ConfigurationManager.AppSettings["MailServer"];
//端口號
objemail.Port = Convert.ToInt32(ConfigurationManager.AppSettings["MailPort"]);
//寄件人地址
objemail.From = ConfigurationManager.AppSettings["MailFrom"];
//寄件人名稱
objemail.FromName = ConfigurationManager.AppSettings["MailFromName"];
//Mail主旨
objemail.Subject = ConfigurationManager.AppSettings["MailSubject"];
// 收件人
//根據配置文件選擇收件人 Y:發送給群組;其他:發送給配置文件中的人員
if (ConfigurationManager.AppSettings["MailToSwitch"] == "Y")
{
//Mail群組
objemail.To = "una_zou@compalcomm.com";
}
else
{
//配置文件中的收件人
objemail.To = ConfigurationManager.AppSettings["MailTo"];
}
objemail.IsHtml = true;
// Email 等級
objemail.Priority = System.Net.Mail.MailPriority.Normal;
//發送
//增加cc人員
//objemail.CC = ConfigurationManager.AppSettings["MailccTo"];
if (ConfigurationManager.AppSettings["MailccSwitch"] == "Y")
{
//Mail群組
objemail.CC = "una_zou@compalcomm.com";
}
else
{
//配置文件中的收件人
objemail.CC = ConfigurationManager.AppSettings["MailccTo"];
}
//獲得Mail的正文
String strBody = GetHtm();
//設置Mail正文
objemail.Body = strBody;
//objemail.Sendccmails();
if (IsSendMail)
{
objemail.Sendmail();
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine("正在執行中...");
}
Console.WriteLine("發送成功...");
sw.timer.Abort(); //终止线程,用于停止秒表
}
public static string GetHtm()
{
// 讀取 HTML 模板
String strPathhtm = "Templete/Schedule.htm";
System.IO.StreamReader strReaderHtm = System.IO.File.OpenText(strPathhtm);
String strHtm = strReaderHtm.ReadToEnd();
//讀取HTML項目內容1
string strPathBlock1 = "Templete/SchedulePlace1.htm";
System.IO.StreamReader strReaderBlock1 = System.IO.File.OpenText(strPathBlock1);
string strHtmBlock1 = strReaderBlock1.ReadToEnd();
strHtm = strHtm.Replace("##siteid##", ConfigurationManager.AppSettings["SysSite"]);
strHtm = strHtm.Replace("#todaydate#", DateTime.Today.ToShortDateString());
//讀取數據庫
DataSet ds = new DataSet();
BLL.Fixture.FixtureMstr FixtureMstrbll = new BLL.Fixture.FixtureMstr();
ds = FixtureMstrbll.GetListMail(DateTime.Now.ToString("yyyy-MM-dd"));
if (ds.Tables[0].Rows.Count > 0)
{
strHtm = strHtm.Replace("##count##", ds.Tables[0].Rows.Count.ToString());
IsSendMail = true;
//替換區域1
string strPlace1 = ReplaceStatistics(strHtmBlock1, ds);
strHtm = strHtm.Replace("#ScheduleByPlace1#", strPlace1);
}
return strHtm;
}
/// <summary>
/// 模板列表
/// </summary>
/// <param name="strHtmBlock"></param>
/// <param name="ds"></param>
/// <returns></returns>
public static string ReplaceStatistics(string strHtmBlock, DataSet ds)
{
string DailySchedule = "";
//循環輸出取出的行值
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
//增加一個地點的模版
DailySchedule += strHtmBlock;
DailySchedule = DailySchedule.Replace("##FIXMSTR_NUMBER##", ds.Tables[0].Rows[i]["FIXMSTR_NUMBER"].ToString());
DailySchedule = DailySchedule.Replace("##FIXMSTR_NAME##", ds.Tables[0].Rows[i]["FIXMSTR_NAME"].ToString());
DailySchedule = DailySchedule.Replace("##FIXMSTR_MODEL##", ds.Tables[0].Rows[i]["FIXMSTR_MODEL"].ToString());
DailySchedule = DailySchedule.Replace("##FIXMSTR_EDITION##", ds.Tables[0].Rows[i]["FIXMSTR_EDITION"].ToString());
DailySchedule = DailySchedule.Replace("##FIXMSTR_CHECKWEEK##", ds.Tables[0].Rows[i]["FIXMSTR_CHECKWEEK"].ToString());
DailySchedule = DailySchedule.Replace("##FIXMSTR_MARK##", ds.Tables[0].Rows[i]["FIXMSTR_MARK"].ToString());
DailySchedule = DailySchedule.Replace("##FIXMSTR_STATUS##", ds.Tables[0].Rows[i]["STATUS"].ToString());
DailySchedule = DailySchedule.Replace("##FIXMSTR_LOCATION##", ds.Tables[0].Rows[i]["LOCATION"].ToString());
DailySchedule = DailySchedule.Replace("##FIXMSTR_CHECKDATE##", ds.Tables[0].Rows[i]["FIXMSTR_CHECKDATE"].ToString());
DailySchedule = DailySchedule.Replace("##FIXMSTR_LASTCHECK##", ds.Tables[0].Rows[i]["FIXMSTR_LASTCHECK"].ToString());
DailySchedule = DailySchedule.Replace("##NextCHECK##", ds.Tables[0].Rows[i]["NextCHECK"].ToString());
}
return DailySchedule;
}
#region 模板分頁
public static DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)
{
if (PageIndex == 0)
return dt;
DataTable newdt = dt.Copy();
newdt.Clear();
int rowbegin = (PageIndex - 1) * PageSize;
int rowend = PageIndex * PageSize;
if (rowbegin >= dt.Rows.Count)
return newdt;
if (rowend > dt.Rows.Count)
rowend = dt.Rows.Count;
for (int i = rowbegin; i <= rowend - 1; i++)
{
DataRow newdr = newdt.NewRow();
DataRow dr = dt.Rows[i];
foreach (DataColumn column in dt.Columns)
{
newdr[column.ColumnName] = dr[column.ColumnName];
}
newdt.Rows.Add(newdr);
}
return newdt;
}
#endregion
}
}
<!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>
<title>治具管理系統</title>
<style type="text/css">
body
{
margin: 0px;
padding: 0px;
border: 0px;
}
table
{
border-collapse: collapse;
}
.ScheduleTitle
{
font-size: 11.0pt;
font-weight: 700;
font-family: Arial, sans-serif;
text-align: center;
vertical-align: middle;
border: 1px solid #000000;
background: #CCFFFF;
}
.SchedulePlace
{
font-size: 11.0pt;
font-weight: 700;
font-family: Arial, sans-serif;
text-align: center;
vertical-align: middle;
border: 1px solid #000000;
}
.ScheduleTopTitle
{
font-size: 11.0pt;
width: 80px;
font-weight: 700;
font-family: Arial, sans-serif;
text-align: center;
vertical-align: middle;
border: 1px solid #000000;
background: #00CCFF;
}
.ScheduletotalTitle
{
font-size: 11.0pt;
width: 700px;
height: 11px;
font-weight: 500;
font-family: Arial, sans-serif;
text-align: center;
vertical-align: middle;
border: 1px solid #000000;
background: #00CCFF;
}
.ScheduleItem
{
color: #0000D4;
font-size: 11.0pt;
font-family: Arial, sans-serif;
text-align: center;
vertical-align: top;
border: 1px solid #000000;
}
.ScheduleRest
{
font-size: 11.0pt;
font-weight: 700;
font-family: Arial, sans-serif;
text-align: center;
vertical-align: middle;
border: 1px solid #000000;
background: #FF99CC;
}
.singleSchedule
{
border-color: Black;
border-style: solid;
border-width: 1px;
border-left: none;
border-right: none;
height: 70px;
}
.STYLE1
{
color: #0000FF;
}
</style>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="1024">
<tr>
<td colspan="10" bgcolor="#FFFFFF" style="text-align: Center; height: 40px; font-size: 16pt">
治具管理系統提醒郵件(<span class="STYLE1">#todaydate#</span>)
</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="1024">
<td colspan="10" bgcolor="#FFFFFF" style="text-align: left; height: 20px; font-size: 12pt">
<p>
Dear all:
</p>
<p>
</p>
</td>
<tr>
<td colspan="10">
<p>
如下是即將到期之設備, 請在到期前三天完成送校。
</p>
詳情請參閱 <a href='##siteid##'>治具管理系統</a>
</td>
</tr>
<tr>
<td colspan="10">
<p>
到期需要校驗數量:##count##
</p>
</td>
</tr>
<tr>
<td width="80" class="ScheduleTitle">
治具編號
</td>
<td width="80" class="ScheduleTitle">
治具名稱
</td>
<td width="80" class="ScheduleTitle">
治具型號
</td>
<td width="80" class="ScheduleTitle">
治具版本
</td>
<td width="80" class="ScheduleTitle">
校驗週期
</td>
<td width="80" class="ScheduleTitle">
治具廠牌
</td>
<td width="80" class="ScheduleTitle">
治具狀態
</td>
<td width="80" class="ScheduleTitle">
庫別
</td>
<td width="80" class="ScheduleTitle">
驗收日期
</td>
<td width="80" class="ScheduleTitle">
最後校驗日期
</td>
<td width="80" class="ScheduleTitle">
下次校驗日期
</td>
</tr>
#ScheduleByPlace1#
</table>
</body>
</html>
<tr>
<td class="ScheduleItem">
##FIXMSTR_NUMBER##
</td>
<td class="ScheduleItem">
##FIXMSTR_NAME##
</td>
<td class="ScheduleItem">
##FIXMSTR_MODEL##
</td>
<td class="ScheduleItem">
##FIXMSTR_EDITION##
</td>
<td class="ScheduleItem">
##FIXMSTR_CHECKWEEK##
</td>
<td class="ScheduleItem">
##FIXMSTR_MARK##
</td>
<td class="ScheduleItem">
##FIXMSTR_STATUS##
</td>
<td class="ScheduleItem">
##FIXMSTR_LOCATION##
</td>
<td class="ScheduleItem">
##FIXMSTR_CHECKDATE##
</td>
<td class="ScheduleItem">
##FIXMSTR_LASTCHECK##
</td>
<td class="ScheduleItem">
##NextCHECK##
</td>
</tr>