我们来看看系统默认的邮件通知,不是很人性化
1、打开之前的项目,选中createTask1,为ListItemId绑定新成员
2、绑定为字段
3、新增加一个CodeActivity用于发送邮件
4、在createTask的MethodInvoking中添加createTask1_TaskProperties1.SendEmailNotification = false;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | private void createTask1_MethodInvoking(object sender, EventArgs e) { //生成任务ID createTask1_TaskId1 = Guid.NewGuid(); //任务标题 createTask1_TaskProperties1.Title = "经理审批"; //设置任务操作人 createTask1_TaskProperties1.AssignedTo = @"i:0#.w|canomo\name"; //设置任务完成度 createTask1_TaskProperties1.PercentComplete = 0; //设置任务开始日期 createTask1_TaskProperties1.StartDate = DateTime.Today; //设置任务到期日期为从工作流任务开始后7天 createTask1_TaskProperties1.DueDate = DateTime.Now.AddDays(7.0); //设置任务审批权限 if (!String.IsNullOrEmpty(createTask1_TaskProperties1.AssignedTo)) { //审批人 createTask1_SpecialPermissions1.Add(this.createTask1_TaskProperties1.AssignedTo, SPRoleType.Contributor); createTask1_SpecialPermissions1.Add(@"c:0(.s|true", SPRoleType.Reader); } //设置任务表单 createTask1_TaskProperties1.TaskType = 1; SPListItem item = workflowProperties.Item; //设置任务数据 createTask1_TaskProperties1.ExtendedProperties["ows_txtTitle"] = item["Title"].ToString(); //关闭系统邮件 createTask1_TaskProperties1.SendEmailNotification = false; } |
5、双击CodeActivity,添加邮件代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | private void codeActivity1_ExecuteCode(object sender, EventArgs e) { SPWeb web = workflowProperties.Web; SPListItem item = workflowProperties.Item; SPUser authorUser = workflowProperties.OriginatorUser; SPUser approvalUser = web.EnsureUser(createTask1_TaskProperties1.AssignedTo); string mailTitle = string.Format("审批通知:{0}", item["Title"].ToString()); string taskUrl = string.Format(@"{0}/_Layouts/listform.aspx?PageType=4&ListId={1}&ID={2}", workflowProperties.WebUrl, workflowProperties.TaskListId, createTask1_ListItemId1); StringBuilder sb = new StringBuilder(); sb.Append(@"<!doctype html><html><head><meta charset='utf-8'> <style>table { border-top: solid 1px #000; border-left: solid 1px #000; }table td { padding: 5px; border-right: solid 1px #000; border-bottom: solid 1px #000; }</style></head><body><table border='0' cellpadding='0' cellspacing='0'><tbody>"); sb.Append(string.Format("<tr><td>标题</td><td>{0}</td></tr>", item["Title"].ToString())); sb.Append(@"<tr><td>...</td><td>...</td></tr></tbody></table>"); sb.Append(string.Format("<p><a href='{0}' target='_blank'>点击打开审批</a></p>", taskUrl)); sb.Append(@"</body></html>"); MailMessage message = new MailMessage(); message.IsBodyHtml = true; message.Subject = mailTitle; message.Body = sb.ToString(); message.From = new MailAddress("[email protected]", authorUser.Name); message.To.Add(new MailAddress(approvalUser.Email, approvalUser.Name)); try { SPOutboundMailServiceInstance smtpServer = workflowProperties.Site.WebApplication.OutboundMailServiceInstance; SmtpClient smtp = new SmtpClient(smtpServer.Server.Address); smtp.Send(message); } catch (Exception ex) { //string sMessage = ex.Message; } } |
6、完成,部署测试如下: