1 创建邮件
/// <param name="service"></param>
/// <param name="fromemail">发件人的Guid的List</param>
/// <param name="tomail">收件人的Guid的List</param>
/// <param name="ccmail">抄送人的Guid的List</param>
/// <param name="entity">需要发送邮件的实体,可以根据此处获取邮件中需要的信息</param>
/// <returns></returns>
private void CreateInformEmail(IOrganizationService service, List<Guid> fromemail, List<Guid> tomail, List<Guid> ccmail, Entity entity)
{
try
{
#region subject/send/to/cc
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);
}
string subjectTxt = string.Empty;
string fName = entity.GetAttributeValue<string>("new_name");
if (fName == null) fName = "";
var owner = entity.GetAttributeValue<EntityReference>("new_business_owner");
string ownerName = string.Empty;
if (owner != null) ownerName = owner.Name;
Entity eml = new Entity("email");
eml.Attributes["from"] = from_list.ToArray();
eml.Attributes["to"] = to_list.ToArray();
eml.Attributes["cc"] = cc_list.ToArray();
eml["regardingobjectid"] = new EntityReference(entity.LogicalName, entity.Id);
eml.Attributes["actualdurationminutes"] = 30;
eml.Attributes["isworkflowcreated"] = false;
//邮件主题
subjectTxt = fName + " 合同到期提醒 Contract Expiration Reminder";
eml.Attributes["subject"] = subjectTxt;
#endregion
#region 邮件模板
StringBuilder body = new StringBuilder();
//body.AppendLine("<p>Dear {0} ,</p>");
body.AppendLine(string.Format("<p>Dear Mr/Ms {0} ,</p>", ownerName));//收件人的 first name
string url = ConfigUtils.CRMExtenalUrl + ConfigUtils.CRMOrganizationName + "main.aspx?etn=" + entity.LogicalName + "&pagetype=entityrecord&id=%7B" + entity.Id + "%7D";
body.AppendLine(string.Format("<p style='text-indent:36.0pt'>{0} 合同30天后即将到期.", fName));
body.AppendLine(string.Format("查看详细信息,请点击链接 <a href='{0}' target='_blank'>{1}</a>.</p>", url, fName));
body.AppendLine(string.Format("<p style='text-indent:36.0pt'> Contract {0} will be expired 30 days later.", fName));
body.AppendLine(string.Format("For more information, please click on <a href='{0}' target='_blank'>{1}</a>.</p>", url, fName));
body.AppendLine("<p>B. Regards</p>");
body.AppendLine("<p>CRM</p>");
eml.Attributes["description"] = body.ToString();
#endregion
Guid email_id = service.Create(eml);
//return email_id;
//发送
SendEmailRequest sendEmailreq = new SendEmailRequest();
sendEmailreq.EmailId = email_id;
sendEmailreq.TrackingToken = "";
sendEmailreq.IssueSend = true;
SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException("Create inform email error: " + ex.Message);
}
}
2.获取需要发邮件的用户
public Guid GetMailUserId(string strDomainName)
{
string sql = @"select SystemUserId,DomainName FROM systemuserBase WITH(NOLOCK)
where DomainName = @domainName ";
SqlParameter[] parameters ={
new SqlParameter("@domainName",SqlDbType.NVarChar,100)
};
parameters[0].Value = strDomainName;
DataSet ds = SqlHelper.ExecuteDataSet(CommandType.Text, sql, parameters);
Guid g_OwnerId = Guid.Parse(ds.Tables[0].Rows[0]["SystemUserId"].ToString());
//EntityReference ref_user = new EntityReference("systemuser", g_OwnerId);
return g_OwnerId;
}
from:https://blog.csdn.net/u012664198/article/details/82191140