using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
namespace SharepointStudy
{
public class TimerExecute
{
public void SendMail()
{
//写其他的业务代码也可以,如获取某个文档库中的记录,符合条件就发邮件
MailMessage message = new MailMessage();
message.From = new MailAddress(xxx@126.com);
message.To.Add(xxx@123.com);
message.Subject = "test";
message.Body = "testbody";
SmtpClient client = new SmtpClient("192.168.0.1");
client.Send(message);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.SharePoint.Administration;
namespace SharepointStudy
{
public class TaskListTimerJob :SPJobDefinition
{
public TaskListTimerJob()
: base()
{
}
public TaskListTimerJob(string _timername, SPWebApplication _wp)
: base(_timername, _wp, null, SPJobLockType.ContentDatabase)
{
this.Title = _timername;
}
public override void Execute(Guid targetInstanceId)
{
//base.Execute(targetInstanceId);
try
{
new TimerExecute().SendMail();
}
catch (Exception ex)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter("c:\timerjob.txt", true))
{
sw.WriteLine("source=" + ex.Source + " Message=" + ex.Message);
}
}
}
}
}
步骤三:
把上面工程的dll拷到C:\WINDOWS\assembly 下,如果考不进去.
项目必须强命名(http://www.cnblogs.com/xx_cs/archive/2011/07/12/2104019.html)
请拷贝时须管理员身份(域帐号且在administrator组也不行),但以管理员身份在vs2010命令提示中运行gacutil -i d:\Demo.dll(我把dll拷贝到d盘的根下了)可以安装成功
步骤四:发布job,写一个winform程序来发布
我理解为创建一个新的任务(TaskListTimerJob),该任务执行了业务类(timerexecute的sendmail方法),发布时须定义任务的执行规则(多久执行一次)
规则如何写可参考(http://blogs.msdn.com/b/markarend/archive/2007/01/16/spschedule-fromstring-recurrencevalue-syntax-format-for-recurrence-value.aspx)
主演:
1:还是要用framework3.5
2:生成的目标平台选x64(http://www.byywee.com/page/M0/S238/238329.html),否则找不站点.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using SharepointStudy;
namespace TimerJobSetUp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
SPSite site = new SPSite("http://gwsps03");
SPWebApplication web = site.WebApplication;
foreach (SPJobDefinition job in web.JobDefinitions)
{
string name = job.Title;
if (name == "MyCustomJobSendTaskMail")
{
job.Delete();
}
}
TaskListTimerJob newJob = new TaskListTimerJob("MyCustomJobSendTaskMail", site.WebApplication);
//SPSchedule schedule = SPSchedule.FromString("daily at 20:46:00");
SPSchedule schedule = SPSchedule.FromString("every 15 minutes");
newJob.Schedule = schedule;
newJob.Update();
//Console.WriteLine("部署成功");
MessageBox.Show("部署成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//Console.Write(ex.Message);
}
}