usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.ServiceProcess;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Net;usingSystem.Xml;usingSystem.Timers;usingSystem.Configuration;//using System.Web.Mail;
usingSystem.Net.Mail;namespaceSharePointWorklow
{public partial classService1 : ServiceBase
{//static private long FIVE_DAYS = 432000000L;
publicService1()
{
InitializeComponent();
}public void Debug(string[] args)
{this.OnStart(args);
}protected override void OnStart(string[] args)
{//Debugger.Launch();
System.Timers.Timer TimeCheck = newSystem.Timers.Timer();
TimeCheck.Interval= Double.Parse(ConfigurationSettings.AppSettings["INTERVAL"]);//TimeCheck.Interval = 60000;
TimeCheck.Elapsed += newSystem.Timers.ElapsedEventHandler(timer1_Tick);
TimeCheck.AutoReset= true;
TimeCheck.Enabled= true;
timer1_Tick(this, ElapsedEventArgs.Empty);
TimeCheck.Start();
}public staticString MailBody(XmlNode node, Double delayDays)
{
String URL= @"http://ent261.sharepoint.hp.com/teams/jupiter_2/Lists/Training%20Record%20FY14/MyItems.aspx";
String start= "Congratulations for completing your training course: \n\nCourse Name:" + node.Attributes["ows_LinkTitle"].Value + ";\n";
String time= "Time:" + node.Attributes["ows_CreateTime"].Value;if (node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"] != null)
{
time= "From:" + node.Attributes["ows_CreateTime"].Value + "; \nTo:" + node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value + ";\n\n";
}
String mention= "We mentioned you have not finished your follow-up with your PL for" + (int)delayDays + "days.\n";
String end= "Please check this link for finishing the following up of this course: \n";
String mailBody= start + time + mention + end +URL;returnmailBody;
}/*public static void SendE_Mail(XmlNode node, Double day, String name, String ccName, String fromName)
{
String time = "Time: " + node.Attributes["ows_CreateTime"].Value;
String deadline = "";
String deadline1 = "";
if (node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"] != null) //未修改过的training按照end time计算deadline
{
DateTime a = Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value).AddDays(42);
DateTime b= new DateTime(2015,3,31);
if (Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value) < new DateTime(2015,4,1) && Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value) > new DateTime(2015, 3, 1))
{ //判断training是否是3月-4月的training
a = b.AddDays(42);
}
deadline = "before " + a.ToShortDateString().ToString() + @" ";
deadline1 = "before " + a.ToShortDateString().ToString() + @"";
time = "From: " + node.Attributes["ows_CreateTime"].Value + " to: " + node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value + "
";
}
if(node.Attributes["ows_Update_x0020_Date"] != null) //修改过的training按照修改日期计算deadline
{
if (Convert.ToDateTime(node.Attributes["ows_Update_x0020_Date"].Value) > Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value))
{
DateTime a = Convert.ToDateTime(node.Attributes["ows_Update_x0020_Date"].Value).AddDays(21);
//Console.WriteLine(a.ToShortDateString());
deadline = "before " + a.ToShortDateString().ToString() + @" ";
deadline1 = "before " + a.ToShortDateString().ToString() + @"";
}
}
//string ls_Subject = "" + (String)k2 + ", " + (String)k1 + @" Takes " + node.Attributes["ows_LinkTitle"].Value + @"";
string ls_Subject = "" + node.Attributes["ows_LinkTitle"].Value + @"Training Follow-up Reminder: Required Completion Date "+ deadline1 +@"";
SmtpMail.SmtpServer = "smtp-americas.hp.com";
MailMessage lo_Message = new MailMessage();
lo_Message.From = "[email protected]";
//lo_Message.To = node.Attributes["ows_Name"].Value.Substring(node.Attributes["ows_Name"].Value.IndexOf('#') + 1);
lo_Message.Cc = "[email protected]";
lo_Message.Subject = ls_Subject;
lo_Message.Priority = MailPriority.High;
lo_Message.Body =
@"
Hello " + (String)name + @" |
This is a kind reminder that you have not completed your training follow-up with your PM for " + (int)day + @" days.Please follow the link below and take actions " + deadline + @". |
Course Name: " + node.Attributes["ows_LinkTitle"].Value + @" |
*Complete training follow-up in time is a mandatory action, according to Jupiter ground rules*
FROM
" + (String)fromName + @"
TO
" + (String)fromName + @"
CC
" + (String)ccName + @"
";
lo_Message.BodyEncoding = System.Text.Encoding.UTF8;
lo_Message.BodyFormat = MailFormat.Html;
SmtpMail.Send(lo_Message);
}
public static void SendE_Mail_Second(XmlNode node, Double day, String name, String ccName, String fromName)
{
string ls_Subject = "" + node.Attributes["ows_LinkTitle"].Value + @" Training Follow-up Final Notification";
String time = "Time: " + node.Attributes["ows_CreateTime"].Value;
String deadline = "";
if (node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"] != null) //未修改过的training按照end time计算deadline
{
DateTime a = Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value).AddDays(42);
DateTime b= new DateTime(2015,3,31);
if (Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value)<=new DateTime(2015,3,31))
{
a = b.AddDays(42);
}
deadline = "before " + a.ToShortDateString().ToString() + @" ";
time = "From: " + node.Attributes["ows_CreateTime"].Value + " to: " + node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value + "
";
}
if (node.Attributes["ows_Update_x0020_Date"] != null) //修改过的training按照修改日期计算deadline
{
if (Convert.ToDateTime(node.Attributes["ows_Update_x0020_Date"].Value) > Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value))
{
DateTime a = Convert.ToDateTime(node.Attributes["ows_Update_x0020_Date"].Value).AddDays(21);
//Console.WriteLine(a.ToShortDateString());
deadline = "before " + a.ToShortDateString().ToString() + @" ";
}
}
SmtpMail.SmtpServer = "smtp-americas.hp.com";
MailMessage lo_Message = new MailMessage();
lo_Message.From = "[email protected]";
lo_Message.Cc = "[email protected]";
lo_Message.Subject = ls_Subject;
lo_Message.Priority = MailPriority.High;
lo_Message.Body =
@"
Hello " + (String)name + @" |
This is a kind reminder that you have not completed your training follow-up action with your PM " + deadline + @". |
Course Name: " + node.Attributes["ows_LinkTitle"].Value + @" |
time " + time + @"
My Training Items*Complete training follow-up in time is a mandatory action, according to Jupiter ground rules*
FROM
" + (String)fromName + @"
TO
" + (String)fromName + @"
CC
" + (String)ccName + @"
";
lo_Message.BodyEncoding = System.Text.Encoding.UTF8;
lo_Message.BodyFormat = MailFormat.Html;
SmtpMail.Send(lo_Message);
}
protected override void OnStop()
{
string ls_Subject = "Training Course Reminding Service Shut Down";
SmtpMail.SmtpServer = "smtp-americas.hp.com";
MailMessage lo_Message = new MailMessage();
lo_Message.From = "[email protected]";
lo_Message.To = "[email protected]";
lo_Message.Cc = "[email protected]";
lo_Message.Subject = ls_Subject;
lo_Message.Priority = MailPriority.High;
lo_Message.Body =
@"
Hello, |
This is a kind reminder that the training course reminding service has been shut down; |
Please review with PM if need it restart |
";
lo_Message.BodyEncoding = System.Text.Encoding.UTF8;
lo_Message.BodyFormat = MailFormat.Html;
SmtpMail.Send(lo_Message);
}*/
public static voidSendE_Mail(XmlNode node, Double day, String name, String ccName, String fromName)
{
String time= "Time:" + node.Attributes["ows_CreateTime"].Value;
String deadline= "";
String deadline1= "";if (node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"] != null) //未修改过的training按照end time计算deadline
{
DateTime a= Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value).AddDays(42);
DateTime b= new DateTime(2015, 3, 31);if (Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value) < new DateTime(2015, 4, 1) && Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value) > new DateTime(2015, 3, 1))
{//判断training是否是3月-4月的training
a = b.AddDays(42);
}//deadline = "before " + a.ToShortDateString().ToString() + @" ";
deadline = "before" + a.Year.ToString() + @"-" + a.Month.ToString() + @"-" + a.Day.ToString() + @"";//deadline1 = "before " + a.ToShortDateString().ToString() + @"";
deadline1 = "Before" + a.Year.ToString() + @"-" + a.Month.ToString() + @"-" + a.Day.ToString() + @"";
time= "· From " + @"" + node.Attributes["ows_CreateTime"].Value + @"" + @" to " + @"" + node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value + @"" + @"
";
}if (node.Attributes["ows_Update_x0020_Date"] != null) //修改过的training按照修改日期计算deadline
{if (Convert.ToDateTime(node.Attributes["ows_Update_x0020_Date"].Value) > Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value))
{
DateTime a= Convert.ToDateTime(node.Attributes["ows_Update_x0020_Date"].Value).AddDays(21);//Console.WriteLine(a.ToShortDateString());//deadline = "before " + a.ToShortDateString().ToString() + @" ";
deadline = "before" + a.Year.ToString() + @"-" + a.Month.ToString() + @"-" + a.Day.ToString() + @"";//deadline1 = "before " + a.ToShortDateString().ToString() + @"";
deadline1 = "Before" + a.Year.ToString() + @"-" + a.Month.ToString() + @"-" + a.Day.ToString() + @"";
}
}string ls_Subject = "" + node.Attributes["ows_LinkTitle"].Value + @"Training Follow-up Reminder: Required Completion Date" + deadline1 + @"";
MailMessage mailMsg= newMailMessage();
mailMsg.From= new MailAddress("[email protected]");
mailMsg.To.Add(fromName);
mailMsg.CC.Add(ConfigurationSettings.AppSettings["PM"]);if (ccName != null)
{
mailMsg.CC.Add(ccName);
}
mailMsg.Subject=ls_Subject;
mailMsg.BodyEncoding=Encoding.UTF8;
mailMsg.IsBodyHtml= true;
mailMsg.Priority=MailPriority.High;
mailMsg.Body=
@"
Hello" + (String)name + @" |
This is a kind reminder that you have not completed your training follow-up with your PM for " + (int)day + @"days. Please follow the link below and take actions" + deadline + @". |
· Course Name: " + @"" + node.Attributes["ows_LinkTitle"].Value + @"" + @" |
*Complete training follow-up in time is a mandatory action, according to Jupiter ground rules*
";
SmtpClient smtp= newSmtpClient();
smtp.Host= "smtp-americas.hp.com";
smtp.Send(mailMsg);
}public static voidSendE_Mail_Second(XmlNode node, Double day, String name, String ccName, String fromName)
{string ls_Subject = "" + node.Attributes["ows_LinkTitle"].Value + @"Training Follow-up Final Notification";
String time= "Time:" + node.Attributes["ows_CreateTime"].Value;
String deadline= "";if (node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"] != null) //未修改过的training按照end time计算deadline
{
DateTime a= Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value).AddDays(42);
DateTime b= new DateTime(2015, 3, 31);if (Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value) <= new DateTime(2015, 3, 31))
{
a= b.AddDays(42);
}//deadline = "before " + a.ToShortDateString().ToString() + @" ";
deadline = "before" + a.Year.ToString() + @"-" + a.Month.ToString() + @"-" + a.Day.ToString() + @"";
time= "· From " + @"" + node.Attributes["ows_CreateTime"].Value + @"" + @" to " + @"" + node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value + @"" + "
";//Console.WriteLine("deadline"+deadline);//Console.WriteLine("a"+a);//Console.WriteLine("a.ToString()"+a.ToString());//Console.WriteLine("sucre style:" + a.Year+"-"+a.Month+"-"+a.Day);
}if (node.Attributes["ows_Update_x0020_Date"] != null) //修改过的training按照修改日期计算deadline
{if (Convert.ToDateTime(node.Attributes["ows_Update_x0020_Date"].Value) > Convert.ToDateTime(node.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value))
{
DateTime a= Convert.ToDateTime(node.Attributes["ows_Update_x0020_Date"].Value).AddDays(21);//Console.WriteLine(a.ToShortDateString());//deadline = "before " + a.ToShortDateString().ToString() + @" ";
deadline = "before" + a.Year.ToString() + @"-" + a.Month.ToString() + @"-" + a.Day.ToString() + @"";
}
}
MailMessage mailMsg= newMailMessage();
mailMsg.From= new MailAddress("[email protected]");
mailMsg.To.Add(fromName);
mailMsg.CC.Add(ConfigurationSettings.AppSettings["PM"]);if (ccName != null)
{
mailMsg.CC.Add(ccName);
}
mailMsg.Subject=ls_Subject;
mailMsg.BodyEncoding=Encoding.UTF8;
mailMsg.IsBodyHtml= true;
mailMsg.Priority=MailPriority.High;
mailMsg.Body=
@"
Hello" + (String)name + @" |
This is a kind reminder that you have not completed your training follow-up action with your PM" + deadline + @". |
· Course Name: " + @"" + node.Attributes["ows_LinkTitle"].Value + @"" + @" |
" + time + @" |
· My Training Items |
*Complete training follow-up in time is a mandatory action, according to Jupiter ground rules* |
";
SmtpClient smtp= newSmtpClient();
smtp.Host= "smtp-americas.hp.com";
smtp.Send(mailMsg);
}protected override voidOnStop()
{string ls_Subject = "Training Course Reminding Service Shut Down";
MailMessage mailMsg= newMailMessage();
mailMsg.From= new MailAddress("[email protected]");
mailMsg.To.Add("[email protected]");
mailMsg.CC.Add("[email protected]");
mailMsg.Subject=ls_Subject;
mailMsg.Priority=MailPriority.High;
mailMsg.Body=
@"
Hello Zhao, Xu-Guang (Sucre, SSIT), |
This is a kind reminder that the training course reminding service has been shut down; |
Please review with PM if need it restart. |
";
mailMsg.BodyEncoding=Encoding.UTF8;
mailMsg.IsBodyHtml= true;
SmtpClient smtp= newSmtpClient();
smtp.Host= "smtp-americas.hp.com";
smtp.Send(mailMsg);
}public void timer1_Tick(objectsender, EventArgs e)
{
KMService.Lists lists= newKMService.Lists();
lists.Url= "http://ent261.sharepoint.hp.com/teams/jupiter_2/_vti_bin/Lists.asmx";
lists.Credentials=CredentialCache.DefaultCredentials;
XmlNode nodelistitems= null;//AllItem viewName = 728101AF-6727-430B-9220-35304FC7A3E5
nodelistitems = lists.GetListItems("8C851226-1615-44E0-B98E-08718E772D04", "728101AF-6727-430B-9220-35304FC7A3E5", null, null, "", null, null);//nodelistitems = lists.GetListItems("8C851226-1615-44E0-B98E-08718E772D04", null, null, null, "", null, null);
XmlNode rsData= nodelistitems["rs:data"];//ows_CreateTime = start time//ows_End_x0020_Time_x0020_of_x0020_Tr = end time
foreach (XmlNode zRow inrsData.ChildNodes)
{if (zRow.GetType().Name.ToString() != "XmlWhitespace")
{if(zRow.Attributes["ows_CreateTime"]!=null){if (Convert.ToDateTime(zRow.Attributes["ows_CreateTime"].Value) >= new DateTime(2015, 3, 1))
{//判断Log training日期是否为3月1号之后
if (zRow.Attributes["ows_Follow_x002d_up_x0020_needed"] != null)
{//Console.WriteLine(zRow.OuterXml);
if ((zRow.Attributes["ows_Follow_x002d_up_x0020_needed"].Value == "Y"
|| zRow.Attributes["ows_Follow_x002d_up_x0020_needed"].Value == "Yes")&& (zRow.Attributes["ows_Complete_x0020_following_x002d_u"].Value == "N" /*&&
zRow.Attributes["ows_Name"].Value.Substring(zRow.Attributes["ows_Name"].Value.IndexOf('#') + 1) == "[email protected]"*/))
{//UTC时间
TimeSpan span = Convert.ToDateTime(zRow.Attributes["ows_CreateTime"].Value) -DateTime.UtcNow;if (zRow.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"] != null)
{
span= DateTime.Today - Convert.ToDateTime(zRow.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value);
}
DateTime Apr= new DateTime(2015,4,1);//4月1号?????
if (Convert.ToDateTime(zRow.Attributes["ows_CreateTime"].Value) < Apr) //3月-4月的training统一按4月1号计算
{
span= DateTime.Today -Apr;
}if (zRow.Attributes["ows_Update_x0020_Date"] != null) //修改过状态的training按照修改日期计算(如果修改时间在training完成时间之后)
{//to do! end time?
if (Convert.ToDateTime(zRow.Attributes["ows_Update_x0020_Date"].Value) > Convert.ToDateTime(zRow.Attributes["ows_End_x0020_Time_x0020_of_x0020_Tr"].Value))
{
span= DateTime.Today - Convert.ToDateTime(zRow.Attributes["ows_Update_x0020_Date"].Value);
}
}
String name= zRow.Attributes["ows_Name"].Value;//筛选 过滤到了邮箱名
String[] primaryEmail = name.Split('#');
String localName= primaryEmail[1];
String finalName= null;
String ccName= null;
String fromName= null;
KMService.Lists namelists= newKMService.Lists();
namelists.Url= "http://ent261.sharepoint.hp.com/teams/jupiter_2/_vti_bin/Lists.asmx";
namelists.Credentials=CredentialCache.DefaultCredentials;
XmlNode nodenamelistitemsF= null;//XmlNode nodenamelistitems = null;//7CBDCEBF-6850-4C02-826A-8BF6DA51D677 is Table "Team Member";
nodenamelistitemsF = namelists.GetListItems("7CBDCEBF-6850-4C02-826A-8BF6DA51D677", null, null, null, "", null, null);
XmlNode rsnameDataF= nodenamelistitemsF["rs:data"];foreach (XmlNode znameRow inrsnameDataF.ChildNodes)
{if (znameRow.GetType().Name.ToString() != "XmlWhitespace")
{if (znameRow.Attributes["ows_Title"] != null)
{//Console.WriteLine(znameRow.Attributes["ows_Title"].Value);
if (localName.Equals(znameRow.Attributes["ows_Title"].Value))
{
finalName= znameRow.Attributes["ows_name1"].Value;
fromName=localName;if (znameRow.Attributes["ows__x65b0__x5efa__x680f_1"] != null)
{
String cName= znameRow.Attributes["ows__x65b0__x5efa__x680f_1"].Value;
String[] arrayname= cName.Split('#');if (arrayname.Length == 2)
{
ccName= arrayname[1];//Console.WriteLine(ccName);
}
}
}
}
}
}//对finalName进行筛选 过滤
if (finalName != null)
{
String[] primaryName= finalName.Split('#');
finalName= primaryName[1];
}//case for time period
/*if ((int)span.TotalDays > 0 && (int)span.TotalDays <= int.Parse(ConfigurationSettings.AppSettings["first span"])) SendE_Mail(zRow, span.TotalDays + 1, finalName, ccName, fromName);
if ((int)span.TotalDays >= int.Parse(ConfigurationSettings.AppSettings["first span"]) && (int)span.TotalDays <= int.Parse(ConfigurationSettings.AppSettings["second span"])) SendE_Mail_Second(zRow, span.TotalDays + 1, finalName, ccName, fromName);*/
//case for time dot
if ((int)span.TotalDays > 0 &&(int)span.TotalDays == int.Parse(ConfigurationSettings.AppSettings["first span"])) SendE_Mail(zRow, span.TotalDays + 1, finalName, ccName, fromName);if ((int)span.TotalDays >= int.Parse(ConfigurationSettings.AppSettings["first span"])&&(int)span.TotalDays == int.Parse(ConfigurationSettings.AppSettings["second span"])) SendE_Mail_Second(zRow, span.TotalDays + 1, finalName, ccName, fromName);
}
}
}
}
}
}//OnStop();
}
}
}