/// <summary> /// 发送邮件 /// </summary> /// <param name="id"></param> /// <param name="link"></param> /// <returns></returns> public ActionResults sendEmial_Business(string id, string link) { ActionResults results = new ActionResults(); try { Entity entity = service.Retrieve("scc_forecasted_statement", new Guid(id), new ColumnSet("scc_issend_ar", "ownerid", "scc_name", "scc_forecast_name", "scc_forecast_type", "scc_predicted_end_date_zz", "scc_remake")); string scc_predicted_end_date = (entity.Contains("scc_predicted_end_date_zz") ? entity.GetAttributeValue<DateTime>("scc_predicted_end_date_zz").AddHours(8).ToString("yyyy-MM-dd") : ""); string scc_remake = entity.GetAttributeValue<string>("scc_remake"); List<Guid> guidArray = new List<Guid>(); List<Guid> ownerArray = new List<Guid>(); List<string> nameArray = new List<string>(); ownerArray.Add(CrmData.GetSingleValueLabel<Guid>("systemuser", "domainname", "scc\\CRM", "systemuserid", service)); QueryExpression qe = new QueryExpression("scc_forecast_line"); qe.ColumnSet = new ColumnSet("scc_forecast_lineid", "scc_type_of_predict", "scc_customer_code", "scc_website_code", "scc_material_coding", "scc_subitems" , "scc_customer", "scc_website", "scc_product", "scc_business_first_levelid", "scc_name", "ownerid", "scc_group"); qe.Criteria.AddCondition("scc_forecasted_statement_forecast_lin", ConditionOperator.Equal, entity.Id); qe.Criteria.AddCondition("scc_scc_status", ConditionOperator.NotEqual, 2); //未完成 qe.Criteria.AddCondition("scc_type_of_predict", ConditionOperator.In, 5); //业务组的类型 var encollect = service.RetrieveAll(qe); foreach (Entity en in encollect.Entities) { #region 循环发送邮件 var scc_type_of_predict = en.GetAttributeValue<OptionSetValue>("scc_type_of_predict").Value; string typename = "业务组", objname = ""; objname = en.GetAttributeValue<EntityReference>("scc_group").Name; EntityReference ownerid = en.GetAttributeValue<EntityReference>("ownerid"); if (ownerid == null) { results.Code = "309"; results.Message = $"销售明细{en.GetAttributeValue<string>("scc_name")}对应的发邮件对象为空,请联系管理员!"; return results; } var linkText = "<a href = " + link + ">点击此处</a>"; var emailtemplateeditorEntity = accountRepository.GetTemplateeditorEntity(service, "销售预测"); if (emailtemplateeditorEntity != null) { List<Guid> newguidA = new List<Guid>(); newguidA.Add(ownerid.Id); var subjectsafehtml = emailtemplateeditorEntity.GetAttributeValue<string>("subjectsafehtml"); var safehtml = emailtemplateeditorEntity.GetAttributeValue<string>("safehtml"); var title = entity.GetAttributeValue<String>("scc_forecast_name"); subjectsafehtml = subjectsafehtml.Replace("【预测名称】", title); safehtml = safehtml.Replace("【姓名】", ownerid.Name).Replace("【预测类型】", typename).Replace("【对象】", objname).Replace("【填写结束日期】", scc_predicted_end_date) .Replace("【预测说明】", scc_remake).Replace("【链接】", linkText); var email = accountRepository.CreateInformEmail1(ownerArray, newguidA, null, entity); email["subject"] = subjectsafehtml; email["description"] = safehtml; var emialId = service.Create(email); SendEmailRequest sendEmailreq = new SendEmailRequest { EmailId = emialId, TrackingToken = "", IssueSend = true }; SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq); newguidA.Clear(); } #endregion } } catch (Exception ex) { results.Code = ResponseCode.Code300; results.Message = ex.Message;return results; } return results; }
获取模板信息
/// <summary> /// 获得模板信息 /// </summary> /// <param name="service"></param> /// <param name="title"></param> /// <returns></returns> public Entity GetTemplateeditorEntity(IOrganizationService service, string title) { QueryByAttribute query = new QueryByAttribute("template"); query.AddAttributeValue("title", title); //query.AddAttributeValue("statecode", 0);//可用 query.ColumnSet = new ColumnSet(true); EntityCollection ec = service.RetrieveMultiple(query); if (ec != null && ec.Entities != null && ec.Entities.Count > 0) { return ec[0]; } return null; }
邮件发送实体赋值:
/// <param name="fromemail">发件人的Guid的List</param> /// <param name="tomail">收件人的Guid的List</param> /// <param name="ccmail">抄送人的Guid的List</param> /// <param name="entity">需要发送邮件的实体,可以根据此处获取邮件中需要的信息</param> /// <returns></returns> public Entity CreateInformEmail1(List<Guid> fromemail, List<Guid> tomail, List<Guid> ccmail, Entity entity) { try { List<Entity> from_list = new List<Entity>(); List<Entity> to_list = new List<Entity>(); List<Entity> cc_list = new List<Entity>(); //发件人 foreach (Guid from in fromemail) { Entity from_party = new Entity("activityparty"); from_party.Attributes["partyid"] = new EntityReference("systemuser", from); from_party.Attributes["partyobjecttypecode"] = 8; from_list.Add(from_party); } //收件人 foreach (Guid to in tomail) { Entity to_party = new Entity("activityparty"); to_party.Attributes["partyid"] = new EntityReference("systemuser", to); to_party.Attributes["partyobjecttypecode"] = 8; to_list.Add(to_party); } ////抄送人 //foreach (Guid cc in ccmail) //{ // Entity cc_party = new Entity("activityparty"); // cc_party.Attributes["partyid"] = new EntityReference("systemuser", cc); // cc_party.Attributes["partyobjecttypecode"] = 8; // cc_list.Add(cc_party); //} Entity email = new Entity("email"); email.Attributes["from"] = from_list.ToArray(); email.Attributes["to"] = to_list.ToArray(); email.Attributes["cc"] = cc_list.ToArray(); //email["regardingobjectid"] = new EntityReference(entity.LogicalName, entity.Id); //email.Attributes["actualdurationminutes"] = 30; //email.Attributes["isworkflowcreated"] = false; return email; } catch (Exception ex) { throw new InvalidPluginExecutionException("Create inform email error: " + ex.Message); } }