BugTracker 加入发Mail的功能
2014-01-06 09:24 stoneniqiu 阅读(1155) 评论(3) 编辑 收藏 举报BugTracker部署好之后,发现增加bug不能mail提醒。于是补上这个功能记录在此,方法是次要的,主要是找到地方。需要3步。吐槽下Asp的代码风格看的真心蛋疼....
一、发送mail(主要是找到位置)
1.在App_Code中加入MailHelper.cs .
using System; using System.Net.Mail; public class MailHelper { /// <summary> /// 异步发送邮件 /// </summary> /// <param name="toMails"></param> /// <param name="subject"></param> /// <param name="content"></param> public static void SystemSendMail(string toMails, string subject, string content) { Action invokeAction = () => WebMailTo(toMails, subject, content); invokeAction.BeginInvoke(Callback, invokeAction); } private static void Callback(IAsyncResult ar) { var action = ar.AsyncState as Action; if (action != null) action.EndInvoke(ar); } /// <summary> /// Webs the mail to. /// </summary> /// <param name="toEmails">To emails.</param> /// <param name="subject">The subject.</param> /// <param name="emailText">The email text.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> public static bool WebMailTo(string toEmails, string subject, string emailText) { #region 此处参数使用时根据需要替换成自己的 const string server = "172.17.xxx.95"; //此处代表Mail Server地址 const string formEmail = xxxxx.xxx@xxx.com.cn"; //此处代表系统发邮件的时候的发件人地址 const string formDispayName = "BugTracker"; //系统发件人的显示名称 const string formPassword = "xxxxx"; //此处代表系统发邮件的时候的发件人的密码 const string formDomain = "xxxwj"; //域名 #endregion const string systermtxt = "<br/>该邮件为系统自动发送,请勿回复!详情请点击" + "<a href='http://cnwj6iapc016:84/'>这里~</a>"; var mailMessage = new MailMessage { IsBodyHtml = true }; mailMessage.To.Add(toEmails); mailMessage.From = new MailAddress(formEmail, formDispayName); mailMessage.Subject = subject; mailMessage.Body = emailText + systermtxt;//此处可以传递一个html mailMessage.Priority = MailPriority.High; var client = new SmtpClient { Host = server, UseDefaultCredentials = false, Credentials = new System.Net.NetworkCredential(formEmail, formPassword, formDomain), DeliveryMethod = SmtpDeliveryMethod.Network, EnableSsl = false }; bool isSendOk; try { client.Send(mailMessage);//发送Mail isSendOk = true; } catch (Exception) { isSendOk = false; } return isSendOk; } }
2.给在App_Code的dbutil.cs加入个功能函数。
public static string GetStringById(int id,StringType stringType) { using (SqlConnection conn = get_sqlconnection()) { var sql = ""; switch (stringType) { case StringType.Email: sql = "select us_email from users where us_id=" + id; break; case StringType.ProjectDefaultUserId: sql = "select pj_default_user from projects where pj_id=" + id; break; case StringType.UserName: sql = "select us_username from users where us_id=" + id; break; } DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(sql, conn); int c = da.Fill(ds, "table"); DataRow[] dr = ds.Tables[0].Select(); conn.Close(); return c != 0 ? dr[0][0].ToString() : ""; } } public enum StringType { Email, ProjectDefaultUserId, UserName }
3.在App_Code的bug.cs中修改insert_bug 方法。
sql = sql.Replace("$short_desc", short_desc.Replace("'", "''")); sql = sql.Replace("$tags", tags.Replace("'", "''")); sql = sql.Replace("$reported_user", Convert.ToString(security.user.usid)); sql = sql.Replace("$project", Convert.ToString(projectid)); sql = sql.Replace("$org", Convert.ToString(orgid)); sql = sql.Replace("$category", Convert.ToString(categoryid)); sql = sql.Replace("$priority", Convert.ToString(priorityid)); sql = sql.Replace("$status", Convert.ToString(statusid)); sql = sql.Replace("$assigned_user", Convert.ToString(assigned_to_userid)); sql = sql.Replace("$udf", Convert.ToString(udfid)); sql = sql.Replace("$pcd1", project_custom_dropdown_value1); sql = sql.Replace("$pcd2", project_custom_dropdown_value2); sql = sql.Replace("$pcd3", project_custom_dropdown_value3); // 发送mail 这里才是补充的代码。 var str = string.Format("<div style='padding:20px;border:2px solid green'>{0}维护了与你相关的新的Bug,详细情况请登录Bug管理系统</div>", security.user.username); var pid = DbUtil.GetStringById(projectid, StringType.ProjectDefaultUserId); if (pid == "") { pid = "0"; } var defaultuserid = Convert.ToInt16(pid); if (defaultuserid != 0) { MailHelper.SystemSendMail(DbUtil.GetStringById(defaultuserid, StringType.Email), "BugTracker:你有新的bug了", str); } if (defaultuserid != assigned_to_userid) { MailHelper.SystemSendMail(DbUtil.GetStringById(assigned_to_userid, StringType.Email), "BugTracker:你有新的bug了", str); }
这样子就ok了。增加不过的时候,会发送给抄送人和工程负责人。
同理,如果要在评论更显得 时候也加入mail通知。评论对应的数据库中的表是bug_posts. 插入的方法在App_Code/bug.cs中的
public static int insert_comment(.....) 方法中。
在return之前加入下面的代码:
sql = sql.Replace("$cc", cc.Replace("'", "''")); sql = sql.Replace("$internal", btnet.Util.bool_to_string(internal_only)); DataRow[] drs = DbUtil.GetBugDataRows(bugid); if (drs.Count() != 0) { var buguserId = Convert.ToInt16(drs[0]["bg_reported_user"]);// 发布bug的人 var bugassigneduserId = Convert.ToInt16(drs[0]["bg_assigned_to_user"]);//bug抄送的人 var projectId = Convert.ToInt16(drs[0]["bg_project"]); const string titile = "Bug有新的评论"; var bugdesc = drs[0]["bg_short_desc"].ToString(); var content = string.Format("<div style='padding:20px;border:3px solid blueviolet;'>Bug {0}(Id :{1})有新的评论:<br/><p style='color:gray;font-size:small;background: #ffffe0'>{2}</p></div>", bugdesc, bugid, comment_formated.Replace("'", "''")); if (this_usid != buguserId)//避免是同一人 而重复发送。 { MailHelper.SystemSendMail(DbUtil.GetStringById(buguserId,StringType.Email),titile,content); } if (bugassigneduserId != 0 && this_usid != bugassigneduserId)//避免是同一人 而重复发送。 { MailHelper.SystemSendMail(DbUtil.GetStringById(bugassigneduserId, StringType.Email), titile, content); } if (projectId != 0) { var pduid =Convert.ToInt16(DbUtil.GetStringById(projectId, StringType.ProjectDefaultUserId)) ; if (pduid != 0 && this_usid != pduid && pduid != bugassigneduserId)//避免是同一人 而重复发送。 { MailHelper.SystemSendMail(DbUtil.GetStringById(pduid, StringType.Email), titile, content); } } }
另外需要在dbutil.cs中加入一些方法
public static string GetStringById(int id,StringType stringType) { using (SqlConnection conn = get_sqlconnection()) { var sql = ""; switch (stringType) { case StringType.Email: sql = "select us_email from users where us_id=" + id; break; case StringType.ProjectDefaultUserId: sql = "select pj_default_user from projects where pj_id=" + id; break; case StringType.BugUserId: sql = "select bg_reported_user from bugs where bg_id=" + id; break; } var ds = new DataSet(); var da = new SqlDataAdapter(sql, conn); var c = da.Fill(ds, "table"); var dr = ds.Tables[0].Select(); conn.Close(); return c != 0 ? dr[0][0].ToString() : "0"; } } public static DataRow[] GetBugDataRows(int bugId) { using (SqlConnection conn = get_sqlconnection()) { var sql = "select * from bugs where bg_id=" + bugId; var ds = new DataSet(); var da = new SqlDataAdapter(sql, conn); da.Fill(ds, "table"); var drs=ds.Tables[0].Select(); conn.Close(); return drs; } }
最后效果:
二、自动登录
自动登录也要改改,每次都要点一次。刚开始以为在login打头的文件中,试了几次登录函数是在default.aspx 页面中。
1、在Page_Load中的 if (username_cookie != null) 中加入最后三句,就是判断下cookie。asp中的控件赋值比较神器.... input元素直接可以点出value。
if (username_cookie != null) { // Set the user name from the last logon. user.Value = username_cookie["name"]; var pcookie = Request.Cookies["pwd"]; if (pcookie != null && pcookie.Value!="") { pw.Value = pcookie.Value; on_logon(); } }
2、再在下面的on_logon() 中的 if (authenticated) 中加入下面的代码。 初始的时候加上cookie
var httpCookie = Response.Cookies["pwd"]; if (httpCookie == null) { var currentCookie = new HttpCookie("pwd") {Value = pw.Value, Expires = DateTime.Now.AddDays(30)}; Response.Cookies.Add(currentCookie); } else { httpCookie.Value = pw.Value; }
3、处理退出,不然的话人家登录了很难切换用户。
用户点击右上角的logoff的时候,其实是先跳转到了logoff.aspx页面。在页面跳转到defau.aspx之前,清空cookie的值。
var httpCookie = Response.Cookies["pwd"]; if (httpCookie != null) { httpCookie.Value = null; } Response.Redirect("default.aspx?msg=logged+off");
感觉下次就会用上。
你的关注和支持是我写作的最大动力~
书山有路群:452450927