使用API发送邮件并携带附件
有很多需求需要在APP层发邮件,但是系统自带的SendMailServiceHelper.Send()需要参数view,怎么办呢?
下面的代码展示在操作服务插件中用当前登陆用户发送邮件,并包含pdf附件。pdf有单据的默认套打模板生成。
1. 在当前用户的个人信息中配置邮箱信息。
2. 插件代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kingdee.BOS;
using Kingdee.BOS.Util;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.Metadata.FormElement;
using Kingdee.BOS.BusinessEntity;
namespace K3Cloud.ServicePlugIn
{
/// <summary>
/// 在操作服务插件中发送邮件
/// </summary>
public class SendMailDemo : AbstractOperationServicePlugIn
{
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="formId">单据formId</param>
/// <param name="ids">单据内码数组</param>
private void SendMail(string formId, string[] ids)
{
if (ids.IsEmpty()) return;
string pkValue = ids.FirstOrDefault();
var view = CreateView(this.Context, formId, pkValue);
Kingdee.BOS.Core.Import.IImportView importView = view as Kingdee.BOS.Core.Import.IImportView;
if (importView == null)
{
return;
}
importView.AddViewSession();//重要,不能删除
try
{
string[] toEmails = new string[] { "T1@kingdee.com", "T2@kingdee.com", "T3@kingdee.com" };
ISendMailService service = ServiceFactory.GetService<ISendMailService>(this.Context);
//当前用户给toEmails发送邮件并包含pdf的附件。
//pdf有单据的默认套打模板生成
var result = service.Send(view, "邮件标题", "邮件内容", toEmails, ids, true, false);
if (result.Successful)
{
Kingdee.BOS.Log.Logger.Info("SendMail", "邮件发送成功");
}
else
{
Kingdee.BOS.Log.Logger.Info("SendMail", "邮件发送失败。原因:" + result.Message);
}
}
catch (Exception ex)
{
Kingdee.BOS.Log.Logger.Error("SendMail", "邮件发送失败", ex);
}
importView.RemoveViewSession();
}
/// <summary>
/// 创建View
/// </summary>
/// <param name="ctx"></param>
/// <param name="formId"></param>
/// <param name="pkValue"></param>
/// <returns></returns>
private IDynamicFormView CreateView(Context ctx, string formId, string pkValue)
{
IMetaDataService metaDataService = Kingdee.BOS.App.ServiceHelper.GetService<IMetaDataService>();
FormMetadata metadata = (FormMetadata)metaDataService.Load(ctx, formId);
var openParameter = CreateOpenParameter(ctx, metadata, pkValue);
var provider = metadata.BusinessInfo.GetForm().GetFormServiceProvider();
string importViewClass = "Kingdee.BOS.Web.Import.ImportBillView,Kingdee.BOS.Web";
Type type = Type.GetType(importViewClass);
IDynamicFormViewService billView = (IDynamicFormViewService)Activator.CreateInstance(type);
billView.Initialize(openParameter, provider);
billView.LoadData();
return (IDynamicFormView)billView;
}
private BillOpenParameter CreateOpenParameter(Context ctx, FormMetadata metadata, string pkValue)
{
var form = metadata.BusinessInfo.GetForm();
BillOpenParameter openPara = new BillOpenParameter(form.Id, metadata.GetLayoutInfo().Id);
openPara.Context = ctx;
openPara.PageId = Guid.NewGuid().ToString();
openPara.Status = OperationStatus.VIEW;
openPara.CreateFrom = CreateFrom.Default;
openPara.DefaultBillTypeId = string.Empty;
openPara.PkValue = pkValue;
openPara.FormMetaData = metadata;
openPara.SetCustomParameter(Kingdee.BOS.Core.FormConst.PlugIns, form.CreateFormPlugIns());
openPara.ServiceName = form.FormServiceName;
return openPara;
}
}
}
使用API发送邮件并携带附件
本文2024-09-23 03:45:42发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-160938.html