分享一个封装好的将单据套打EXCEL/PDF发送到指定邮箱的方法
Cloud自带的发送套打信息至邮箱的功能需要自行点击发送邮件按钮并且选择收件人,可以在操作插件中(审核后,如果审核时间较长可考虑异步调用此方法)调用此方法将不需要手工去操作实现自动发送邮件(获取用户设置的套打模板),前提是要先配置好邮件服务器并且保证邮件服务器是正常可用的。
实现如下效果:
C#方法:
//// <summary>
/// 金蝶云发送邮件方法 ctx上下文,fid单据内码,billkey单据主键,billtypeid单据类型id,mailadress收件人地址,title邮件标题,msg邮件内容,yjdata邮件账户设置(BAS_MAILACCOUNTSETTING)数据包,pdf是否发送pdf,excel是否发送excel 引用Kingdee.BOS.ServiceHelper /Kingdee.BOS.Core.Metadata/Kingdee.BOS.Core.Metadata.FormElement
/// </summary>
/// <param name="ctx"></param>
/// <param name="fid"></param>
/// <param name="billkey"></param>
/// <param name="billtypeid"></param>
/// <param name="mailadress"></param>
/// <param name="title"></param>
/// <param name="msg"></param>
/// <param name="yjdata"></param>
/// <param name="pdf"></param>
/// <param name="excel"></param>
/// <returns></returns>
public string Sendmail(Context ctx, string fid, string billkey, string billtypeid, string[] mailadress, string title, string msg, Kingdee.BOS.Orm.DataEntity.DynamicObject yjdata, bool pdf, bool excel)
{
if (yjdata != null)
{
Kingdee.BOS.Orm.DataEntity.DynamicObject yjdzdata = yjdata["FSENDMAILSERVER"] as Kingdee.BOS.Orm.DataEntity.DynamicObject;
string yjdz = null;//邮件地址
int yjdk = 0;//邮件端口
string fwdz = null;//邮件服务器地址
string zh = null;//发送邮件账户
string sqm = null;//授权码
string detdmb = null;//默认套打模板
if (!yjdata["FEmailAddress"].IsNullOrEmptyOrWhiteSpace())
{
fwdz = yjdata["FEmailAddress"].ToString();
}
if (!yjdata["FUserName"].IsNullOrEmptyOrWhiteSpace())
{
zh = yjdata["FUserName"].ToString();
}
if (!yjdata["FPassword"].IsNullOrEmptyOrWhiteSpace())
{
sqm = yjdata["FPassword"].ToString();
}
if (yjdzdata != null)
{
yjdz = yjdzdata["FMailServer"].ToString();
yjdk = Convert.ToInt32(yjdzdata["FSMTPPort"]);
}
IDynamicFormView view = this.CreateBillView(ctx, billkey, fid);//服务插件中创建view
string paratext = UserParamterServiceHelper.Load(ctx, "NotePrintSetup" + view.BillBusinessInfo.GetForm().Id.ToUpper().GetHashCode().ToString(), ctx.UserId, "");//获取套打设置参数
JArray paras = JArray.Parse(paratext);
if (paras != null)
{
var tdmbs = (from pa in paras where pa["key"].ToString() == billtypeid select pa).ToList();
if (tdmbs.Count > 0)
{
detdmb = tdmbs[0]["value"].ToString();
}
}
List<string> bids = new List<string>();
bids.Add(fid);
List<string> tdids = new List<string>();
tdids.Add(detdmb);
//(view as IDynamicFormViewService).MainBarItemClick("YOL_YJ");
Kingdee.BOS.Core.Import.IImportView importView = view as Kingdee.BOS.Core.Import.IImportView;
importView.AddViewSession();
string[] ids = { fid };
string[] cc = null;
string exportFileName = null;
string pdfExportFileName = null;
string excelExportFileName = null;
ResultInfo resultInfo = null;
DynamicFormViewPlugInProxy service = view.GetService<DynamicFormViewPlugInProxy>();
if (service != null)
{
InitializeSendMailServiceEventArgs initializeSendMailServiceEventArgs = new InitializeSendMailServiceEventArgs();
service.FireOnInitializeSendMailService(initializeSendMailServiceEventArgs);
exportFileName = initializeSendMailServiceEventArgs.ExportFileName;
}
try
{
pdfExportFileName = SendMailServiceHelper.GetPdfExportFileName(view, ctx.UserId, ids, detdmb, exportFileName);
}
catch (Exception)
{
return "生成pdf失败";
}
try
{
excelExportFileName = SendMailServiceHelper.GetExcelExportFileName(view, ctx.UserId, ids, detdmb, exportFileName);
}
catch (Exception)
{
return "生成excel失败";
}
try
{
resultInfo = SendMailServiceHelper.Send(view, ctx.UserId, yjdz, yjdk, false, false, fwdz, zh, sqm, title, msg, mailadress, cc, ids, pdf, pdfExportFileName, excel, excelExportFileName, exportFileName);
}
catch (Exception ex)
{
return ex.Message;
}
if (resultInfo.Successful == true)
{
return "邮件发送成功!";
}
else
{
return "邮件发送失败!原因:" + resultInfo.Message;
}
}
else
{
return "参数不能为空!";
}
}
public IBillView CreateBillView(Context ctx, string key, string id)
{
// 读取物料的元数据
FormMetadata meta = MetaDataServiceHelper.Load(ctx, key) as FormMetadata;
Form form = meta.BusinessInfo.GetForm();
// 创建用于引入数据的单据view
Type type = Type.GetType("Kingdee.BOS.Web.Import.ImportBillView,Kingdee.BOS.Web");
var billView = (IDynamicFormViewService)Activator.CreateInstance(type);
// 开始初始化billView:
// 创建视图加载参数对象,指定各种参数,如FormId, 视图(LayoutId)等
BillOpenParameter openParam = CreateOpenParameter(ctx, meta, id);
// 动态领域模型服务提供类,通过此类,构建MVC实例
var provider = form.GetFormServiceProvider();
billView.Initialize(openParam, provider);
billView.LoadData();
(billView as IBillView).CommitNetworkCtrl();
return billView as IBillView;
}
public BillOpenParameter CreateOpenParameter(Context ctx, FormMetadata meta, string id)
{
Form form = meta.BusinessInfo.GetForm();
// 指定FormId, LayoutId
BillOpenParameter openParam = new BillOpenParameter(form.Id, meta.GetLayoutInfo().Id);
// 数据库上下文
openParam.Context = ctx;
// 本单据模型使用的MVC框架
openParam.ServiceName = form.FormServiceName;
// 随机产生一个不重复的PageId,作为视图的标识
openParam.PageId = Guid.NewGuid().ToString();
// 元数据
openParam.FormMetaData = meta;
// 界面状态:新增 (修改、查看)
openParam.Status = OperationStatus.EDIT;
// 单据主键
openParam.PkValue = id;
// 界面创建目的:普通无特殊目的 (为工作流、为下推、为复制等)
openParam.CreateFrom = CreateFrom.Default;
// 基础资料分组维度:基础资料允许添加多个分组字段,每个分组字段会有一个分组维度
// 具体分组维度Id,请参阅 form.FormGroups 属性
openParam.GroupId = "";
// 基础资料分组:如果需要为新建的基础资料指定所在分组,请设置此属性
openParam.ParentId = 0;
// 单据类型
openParam.DefaultBillTypeId = "";
// 业务流程
openParam.DefaultBusinessFlowId = "";
// 主业务组织改变时,不用弹出提示界面
openParam.SetCustomParameter("ShowConfirmDialogWhenChangeOrg", false);
// 插件
List<AbstractDynamicFormPlugIn> plugs = form.CreateFormPlugIns();
openParam.SetCustomParameter(FormConst.PlugIns, plugs);
PreOpenFormEventArgs args = new PreOpenFormEventArgs(ctx, openParam);
foreach (var plug in plugs)
{// 触发插件PreOpenForm事件,供插件确认是否允许打开界面
plug.PreOpenForm(args);
}
if (args.Cancel == true)
{// 插件不允许打开界面
// 本案例不理会插件的诉求,继续....
}
// 返回
return openParam;
}
谢谢鱼哥
【emoji】
分享一个封装好的将单据套打EXCEL/PDF发送到指定邮箱的方法
本文2024-09-16 17:22:18发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-15282.html