/// <summary> /// 执行发送邮件的动作 /// </summary> /// <param name="mailInfo">mail相关信息的实例</param> /// <returns>Successful表示发送成功</returns> /// <returns>Failed表示发送失败</returns> public string SendMail(MailInfo mailInfo) { try { SmtpClient smtpClient = new SmtpClient(); MailMessage mailMessage = new MailMessage(); smtpClient.Host = mailInfo.Host; smtpClient.UseDefaultCredentials = true; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; if (mailInfo.Mailfrom != "" && mailInfo.Mailpassword != "") { smtpClient.Credentials = new NetworkCredential(mailInfo.Mailfrom, mailInfo.Mailpassword); } mailMessage.From = new MailAddress(mailInfo.Mailfrom); mailMessage.To.Add(mailInfo.Mailto); mailMessage.Subject = mailInfo.Subject; mailMessage.Body = mailInfo.Body; mailMessage.SubjectEncoding = Encoding.UTF8; mailMessage.BodyEncoding = Encoding.UTF8; mailMessage.Priority = MailPriority.High; mailMessage.IsBodyHtml = true; smtpClient.Send(mailMessage); return "successful"; } catch (Exception e) { return "failed"; } }
/// <summary> /// 执行编辑邮件相关信息的操作 /// </summary> /// <param name="projectInfo">传入projectInfo的实例对象</param> /// <returns>返回编辑后的MailInfo实例对象</returns> public MailInfo EditBody(ProjectInfo projectInfo,string mailPath) { string path = string.Empty; System.IO.StreamReader sr = new System.IO.StreamReader(mailPath); string body = string.Empty; body = sr.ReadToEnd(); MailInfo mailInfo = new MailInfo(); string header = ""; try { header = projectInfo.Result == "successful" ? Encoding.UTF8.GetString(new byte[] {0xF0, 0x9F, 0x98, 0x83}) : Encoding.UTF8.GetString(new byte[] {0xF0, 0x9F, 0x91, 0xBF}); header += projectInfo.Nameproperty + " revision" + projectInfo.Revision + " build " + projectInfo.Result; body = body.Replace("$PROJECTNAME$", projectInfo.Nameproperty); body = body.Replace("$LOG$", projectInfo.Log); body = body.Replace("$VERSION$", projectInfo.Revision); body = body.Replace("$AUTHER$", projectInfo.Author); body = body.Replace("$DATE$", projectInfo.Changetime); body = body.Replace("$RESULT$", "Build " + projectInfo.Result); body = body.Replace("$DURATION$", projectInfo.Duration); body = projectInfo.Log == "" ? body.Replace("$LOGLABEL$", "") : body.Replace("$LOGLABEL$", "Build Log"); mailInfo.Subject = header; mailInfo.Body = body; mailInfo.Mailto = projectInfo.MailTo; mailInfo.Host = projectInfo.MailHost; if (projectInfo.UserName.Contains("#author#")) { mailInfo.Mailfrom = projectInfo.Author.Remove(0, 3) + Regex.Match(projectInfo.UserName, "(?:@)(.+)").Value; } else { mailInfo.Mailfrom = projectInfo.UserName.Replace("#",""); mailInfo.Mailpassword = projectInfo.Password; } return mailInfo; } catch (Exception exception) { return mailInfo; } }
/// <summary> /// 执行编辑邮件相关信息的操作 /// </summary> /// <param name="projectInfo">传入projectInfo的实例对象</param> /// <returns>返回编辑后的MailInfo实例对象</returns> public MailInfo EditBody(ProjectInfo projectInfo, Dictionary<string, Dictionary<string, string>> allStat, string mailPath) { string path = string.Empty; System.IO.StreamReader sr = new System.IO.StreamReader(mailPath); string body = string.Empty; body = sr.ReadToEnd(); MailInfo mailInfo = new MailInfo(); try { Dictionary<string, string> selectStat = allStat[projectInfo.Author.Split('\\')[1]]; int[] allUpdate = new int[allStat.Count]; int i = 0; int selectWeek = 0, selectSuccess = 0, selectFailure = 0; foreach (var key in allStat.Keys) { allUpdate[i] = Int32.Parse(allStat[key]["Week"]); if (key == projectInfo.Author.Split('\\')[1]) { selectWeek = Int32.Parse(allStat[key]["Week"]); selectSuccess = Int32.Parse(allStat[key]["Success"]); selectFailure = Int32.Parse(allStat[key]["Failure"]); } i++; } //计算个人本周编译次数,成功失败次数,以及其所占有的比率 double successRate = (double)selectSuccess / selectWeek; double failureRate = (double)selectFailure / selectWeek; Array.Sort(allUpdate); Array.Reverse(allUpdate); var emoji = projectInfo.Result == "successful" ? Encoding.UTF8.GetString(new byte[] {0xF0, 0x9F, 0x98, 0x83}) : Encoding.UTF8.GetString(new byte[] {0xF0, 0x9F, 0x91, 0xBF}); var header = emoji + projectInfo.Nameproperty + " revision" + projectInfo.Revision + " build " + projectInfo.Result + ". This week " + projectInfo.Author.Split('\\')[1] + " build " + selectWeek.ToString() + " times, " +selectSuccess.ToString()+" passed, "+selectFailure.ToString()+" failed."; body = body.Replace("$PROJECTNAME$", projectInfo.Nameproperty); body = body.Replace("$LOG$", projectInfo.Log); body = body.Replace("$VERSION$", projectInfo.Revision); body = body.Replace("$AUTHOR$", projectInfo.Author); body = body.Replace("$DATE$", projectInfo.Changetime); body = body.Replace("$RESULT$", "build " + projectInfo.Result); body = body.Replace("$DURATION$", projectInfo.Duration); body = body.Replace("$SERVER_VERSION$", "Send by LuckyCI v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()); body = projectInfo.Log == "\n" ? body.Replace("$LOGLABEL$", "") : body.Replace("$LOGLABEL$", "Build Log"); mailInfo.Subject = header; mailInfo.Body = body; mailInfo.Mailto = projectInfo.MailTo; mailInfo.Host = projectInfo.MailHost; if (projectInfo.UserName.Contains("#author#")) { mailInfo.Mailfrom = projectInfo.Author.Remove(0, 3) + Regex.Match(projectInfo.UserName, "(?:@)(.+)").Value; } else { mailInfo.Mailfrom = projectInfo.UserName.Replace("#",""); mailInfo.Mailpassword = projectInfo.Password; } return mailInfo; } catch (Exception ex) { return mailInfo; } }
//成功失败次数直接封装 /// <summary> /// 最新编辑邮件模板 /// </summary> /// <param name="projectInfo"></param> /// <param name="statisticsTimes"></param> /// <param name="mailPath"></param> /// <param name="shortName"></param> /// <returns></returns> public MailInfo NewEditBody(ProjectInfo projectInfo,string[] statisticsTimes, string mailPath, string shortName) { string path = string.Empty; System.IO.StreamReader sr = new System.IO.StreamReader(mailPath); string body = string.Empty; body = sr.ReadToEnd(); MailInfo mailInfo = new MailInfo(); try { //计算个人本周编译次数,成功失败次数,以及其所占有的比率 double successRate = double.Parse(statisticsTimes[2]) / double.Parse(statisticsTimes[1]); double failureRate = double.Parse(statisticsTimes[3]) / double.Parse(statisticsTimes[1]); var emoji = projectInfo.Result == "successful" ? Encoding.UTF8.GetString(new byte[] { 0xF0, 0x9F, 0x98, 0x83 }) : Encoding.UTF8.GetString(new byte[] { 0xF0, 0x9F, 0x91, 0xBF }); var header = emoji + projectInfo.Nameproperty + " reversion " + ((projectInfo.ProjectType == "git") ? (projectInfo.GitVersion.Substring(0, 8)) : projectInfo.GitVersion) + " build " + projectInfo.Result + ". This week " + projectInfo.Author + " build " + statisticsTimes[1] + " times, " + statisticsTimes[2] + " passed, " + statisticsTimes[3] + " failed."; body = body.Replace("$PROJECTNAME$", projectInfo.Nameproperty); body = body.Replace("$LOG$", projectInfo.Log); body = body.Replace("$VERSION$", projectInfo.Revision); body = body.Replace("$AUTHOR$", projectInfo.Author); body = body.Replace("$DATE$", projectInfo.Changetime); body = body.Replace("$RESULT$", "build " + projectInfo.Result); body = body.Replace("$DURATION$", projectInfo.Duration); body = body.Replace("$SERVER_VERSION$", "Send by LuckyCI v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()); body = projectInfo.Log == "\n" ? body.Replace("$LOGLABEL$", "") : body.Replace("$LOGLABEL$", "Build Log"); mailInfo.Subject = header; mailInfo.Body = body; mailInfo.Mailto = projectInfo.MailTo; mailInfo.Host = projectInfo.MailHost; if (projectInfo.UserName.Contains("#author#")) { mailInfo.Mailfrom = (shortName != "") ? (shortName + Regex.Match(projectInfo.UserName, "(?:@)(.+)").Value) : (projectInfo.Author + Regex.Match(projectInfo.UserName, "(?:@)(.+)").Value); } else { mailInfo.Mailfrom = projectInfo.UserName.Replace("#", ""); mailInfo.Mailpassword = projectInfo.Password; } return mailInfo; } catch (Exception ex) { return mailInfo; } }