
【场景】发送自定义邮件二开逻辑
如携带自定义单据的附件、携带自定义单据的套打导出文件
【案例】

```csharp
using Kingdee.BOS;
using Kingdee.BOS.Core.Attachment;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Import;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.NotePrint;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.FileServer.Core.Object;
using Kingdee.BOS.FileServer.ProxyService;
using Kingdee.BOS.Msg;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace DynamicFormPlugIn.Mail
{
[Kingdee.BOS.Util.HotUpdate]
[System.ComponentModel.Description("发送邮件二开相关")]
public class FormPlugIn_SendCustomMail : AbstractDynamicFormPlugIn
{
public override void BarItemClick(BarItemClickEventArgs e)
{
if (!string.Equals(e.BarItemKey, "tb_SendCustomMail", StringComparison.OrdinalIgnoreCase))
return;
SendCustomMail();
}
private void SendCustomMail()
{
EmailMessageInfo mailInfo = null;
//<0>发送人邮箱信息相关
//使用当前用户的邮箱
mailInfo = Kingdee.BOS.ServiceHelper.SendMailServiceHelper.GetEmailMessageInfoByUserId(this.Context, this.Context.UserId);
/*
//使用参数设置-基础管理-BOS平台 虚拟邮箱配置
mailInfo = Kingdee.BOS.ServiceHelper.SendMailServiceHelper.GetEmailMessageInfoByBosVirtual(this.Context, this.Context.UserId);
//使用参数设置-流程中心-工作流 虚拟邮箱配置
mailInfo = Kingdee.BOS.ServiceHelper.SendMailServiceHelper.GetEmailMessageInfoByBosVirtual(this.Context, this.Context.UserId);
*/
//<1>接收人邮箱信息相关
var userContracts = Kingdee.BOS.ServiceHelper.SendMailServiceHelper.GetUserContacts(this.Context, new List<long>() { this.Context.UserId });
if (userContracts != null)
{
mailInfo.To = userContracts.Select(x => x.Email).ToList();
//关联单据A的客户的内码
//mailInfo.To = GetCustomerEmailById(pkids);
}
//<2>邮件标题和邮件内容
mailInfo.Subject = "标题";
mailInfo.Body = string.Format("正文:单据编号 {0} 发送邮件", this.Model.GetValue("FBillNo"));
//<2>邮件的携带的文件相关
//生成采购订单的导出文件作为附件发送
string exportFile = GetExportFile("PUR_PurchaseOrder", "100001", "734cffd8-26ef-4131-b0c2-1d26b29d90c0");
if(!exportFile.IsNullOrEmptyOrWhiteSpace())
{
mailInfo.Attachments.Add(exportFile);
}
//获取单据关联的附件
List<string> attachFiles = GetAttachmentFiles("BD_Currency", "704887");
if(attachFiles != null)
{
attachFiles.ForEach(x => mailInfo.Attachments.Add(x));
}
//<3>发送邮件
var resultInfo = Kingdee.BOS.ServiceHelper.SendMailServiceHelper.Send(this.View.Context, mailInfo, new PrintExportForEmailInfo());
}
/// <summary>
/// 根据客户的id 获取接收邮箱
/// </summary>
/// <param name="pkIds"></param>
/// <returns></returns>
private List<string> GetCustomerEmailById(string[] pkIds)
{
var tpSqlAndParam = StringUtils.GetSqlWithCardinalityAndParam(pkIds, "@ids", 1, true);
string sql = string.Format(@"select {0} TEMP.FCUSTID,TEMP.FCUSTNAME as FNAME,TEMP.FCONTACTNAME AS FCONTACT,TEMP.FEMAIL,TEMP.FISDEFAULT,TEMP.FCONTACTID from (
select C.FCUSTID,CL.FNAME AS FCUSTNAME,ISNULL(contact_L.FNAME,' ') AS FCONTACTNAME,ISNULL(contact.FEMAIL,' ') AS FEMAIL,ISNULL(contact.FCONTACTID,0) AS FCONTACTID
,(Case when ISNULL(ext.FDEFAULTCONTACT,0)=ISNULL(contact.FCONTACTID,0) and ISNULL(ext.FDEFAULTCONTACT,0)>0 then 1 else 0 end) AS FISDEFAULT
from T_BD_CUSTOMER C
INNER JOIN {1} BB ON C.FCUSTID=BB.FID
left Join T_BD_CUSTOMEREXT ext on C.FCUSTID=ext.FCUSTID
left join T_BD_CUSTOMER_L CL on (C.FCUSTID=CL.FCUSTID and CL.FLOCALEID=@FLOCALEID1)
left join T_BD_COMMONCONTACT contact on ((contact.FCOMPANY=C.FCUSTID or contact.FCOMPANY=C.FMASTERID) and contact.FCOMPANYTYPE='BD_Customer')
left join T_BD_COMMONCONTACT_L contact_L on (contact.FCONTACTID=contact_L.FCONTACTID and contact_L.FLOCALEID=@FLOCALEID2 )
) TEMP
ORDER BY TEMP.FCUSTID ASC,TEMP.FISDEFAULT DESC,TEMP.FCONTACTID ASC", "", tpSqlAndParam.Item1);
SqlParam[] paramList = new SqlParam[3];
paramList[0] = tpSqlAndParam.Item2;
paramList[1] = new SqlParam("@FLOCALEID1", KDDbType.Int16, this.Context.UserLocale.LCID);
paramList[2] = new SqlParam("@FLOCALEID2", KDDbType.Int16, this.Context.UserLocale.LCID);
var bdObjs = DBServiceHelper.ExecuteDynamicObject(this.Context, sql, null, null, CommandType.Text, paramList);
List<string> result = new List<string>();
if (bdObjs != null)
{
for (int i = 0; i < bdObjs.Count; ++i)
{
var bdObj = bdObjs[i];
if (bdObj == null)
continue;
string email = ObjectUtils.Object2String(bdObj["FEMAIL"]);
if (email.IsNullOrEmptyOrWhiteSpace())
continue;
result.Add(email);
}
}
return result;
}
#region 生成套打导出文件
/// <summary>
/// 生成导出文件
/// </summary>
/// <param name="formId"></param>
/// <param name="billId"></param>
/// <param name="templateId"></param>
/// <returns></returns>
private string GetExportFile(string formId, string billId, string templateId)
{
return ExportTargetBill(this.Context, formId, billId, templateId);
}
/// <summary>
/// 套打导出指定单据
/// </summary>
/// <param name="formId"></param>
/// <param name="billId"></param>
/// <param name="templateId"></param>
/// <returns></returns>
private static string ExportTargetBill(Context ctx, string formId, string billId, string templateId)
{
IDynamicFormView dynamicFormView = CreateBillView(ctx, formId, billId); //此行为代码模拟打开凭证界面
if (dynamicFormView == null)
return null;
IImportView billView = dynamicFormView as IImportView;
if (billView != null)
{
billView.AddViewSession();
}
try
{
return ExportTargetBillWithDynamic(dynamicFormView, formId, billId, templateId);
}
finally
{
if (billView != null)
{
billView.RemoveViewSession();
}
dynamicFormView.Close();
}
}
private static string ExportTargetBillWithDynamic(IDynamicFormView dynamicFormView, string formId, string billId, string templateId)
{
IDynamicFormViewService viewService = dynamicFormView as IDynamicFormViewService;
if (viewService == null)
return null;
try
{
List<string> billIds = new List<string>() { billId };
List<string> templateIds = new List<string>() { templateId };
PrintExportInfo pExInfo = new PrintExportInfo();
pExInfo.PageId = dynamicFormView.PageId;
pExInfo.FormId = formId;
pExInfo.BillIds = billIds; //单据内码
pExInfo.TemplateIds = templateIds; //套打模板ID
pExInfo.FileType = ExportFileType.PDF; //文件格式
pExInfo.ExportType = ExportType.BillTempId; //导出格式
string fileName = Guid.NewGuid().ToString() + ".PDF";
string temppath = PathUtils.GetPhysicalPath("TempfilePath", fileName);
pExInfo.FilePath = temppath; //文件输出路径
//指定动态文件目录和动态文件名
pExInfo.AutoZip = true;
pExInfo.ExportDynamicDirectory = pExInfo.Id;
pExInfo.ExportDynamicFileName = "{FBillNo}";
viewService.ExportNotePrint(pExInfo);
if (pExInfo.ExportFileInfos == null || pExInfo.ExportFileInfos.Count <= 0)
return tem