插件案例 —— 后台发邮件、云之家消息、机器人消息原创
金蝶云社区-MiLai
MiLai
20人赞赏了该文章 4,073次浏览 未经作者许可,禁止转载编辑于2020年08月04日 12:57:41


业务背景


有的时候针对业务监控,我们还需要对特殊的业务进行发邮件给上级部门或者主管,以便能做到更及时,无失误。
而对于每个公司,每个业务员信息维护方式一样,基于个人信息的邮箱地址,我们可以自行在发预警的时候也发送邮件。
下面我介绍一下如果自己扩展二开发送邮件功能。(以采购价格失效为例)当然了,你也放在执行计划中。


开发案例


image.png


其实扩展开发业务预警代码,在执行完业务后,同时进行邮件发送。

由于系统发送邮件需要指定发送人,所以我们可以在管理系统参数中默认预置发送人、发送服务器等信息。

最终调用MailUtils.SendEmail服务


发邮件

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Mail;
using System.Text;
using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Contracts.Messages;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.Log;
using Kingdee.BOS.Msg;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
//继承 AbstractWarnServicePlugIn,方法
            //发送邮件,获取默认发送人 //可以使用系统中默认的”邮件服务设置“,配值默认的发件人
            ISystemParameterService sysService = Kingdee.BOS.Contracts.ServiceFactory.GetSystemParameterService(this.Context);
            DynamicObjectinquirySysParam = sysService.Load(this.Context, 0, 0, "管理系统参数");
            string mailFid = "";
            if (inquirySysParam != null && inquirySysParam.DynamicObjectType.Properties.ContainsKey("SendMail"))
                mailFid = Convert.ToString(inquirySysParam["SendMail_Id"]);
            else
                return;
            //根据配值ID,获取对应的配值服务信息 //发件人账号、密码、发件地址、SSL等
            string strSql = string.Format(@"SELECTFEMAILADDRESS,FUSERNAME,FPASSWORD,TSER.FMAILSERVER,FMESSAGETYPE,FSMTPPORT,FSSLFROM T_BAS_MAILVIRTUALACCOUNT TMAIL
INNER JOIN T_BAS_MAILSERVERTSER ON TMAIL.FMAILSERVER = TSER.FID WHERE TMAIL.FID = {0}", mailFid);
            DynamicObjectCollectionmailInfos = DBUtils.ExecuteDynamicObject(this.Context, strSql, null, null, System.Data.CommandType.Text, null);
            if (mailInfos == null || mailInfos.Count <= 0)
                return;
            DynamicObject mailInfo = mailInfos.FirstOrDefault();
            string senderMailAddress = Convert.ToString(mailInfo["FEMAILADDRESS"]);
            string title = e.WarnMessage.MobileTitle;
            string SMTPHost = Convert.ToString(mailInfo["FMAILSERVER"]);
            int port = Convert.ToInt32(mailInfo["FSMTPPORT"]);
            bool isSSL = Convert.ToInt32(mailInfo["FSSL"]) == 1;
            string userName = Convert.ToString(mailInfo["FUSERNAME"]);
            string pwd = DoProclaimText(Convert.ToString(mailInfo["FPASSWORD"]));
            //系统对发送消息的用户包装UserWarnMessageCollection,可以获取到对应的用户ID
            var lst = e.WarnMessage.UserWarnMessageCollection;
            if (lst == null || lst.Count <= 0)
                return;
            //根据用户ID,找到用户的Email
            string emailSql = string.Format(@"SELECT FUSERID,FEMAIL FROM T_SEC_USER
WHEREFUSERID IN ({0})", string.Join(",", lst.Select(x => x.Key).ToArray()));
            DynamicObjectCollectionmailLst = DBUtils.ExecuteDynamicObject(this.Context, emailSql, null, null, System.Data.CommandType.Text, null);
            foreach (var item in lst)
            {
                try
                {
                    List listTo = new List();
                    long userId = item.Key;
                    var info = mailLst.Where(x => Convert.ToInt64(x["FUSERID"]) == userId);
                    string userEmail = string.Empty;
                    if (info != null && info.Count() > 0)
                        userEmail = Convert.ToString(info.FirstOrDefault()["FEMAIL"]);
                    if (string.IsNullOrEmpty(userEmail))
                        continue;
                    listTo.Add(userEmail); //根据用户,对预警信息进行整理。
                    Kingdee.BOS.Core.Warn.Message.UserWarnMessage msg = item.Value;
                    string message = string.Join("\n", msg.GetMessageItems().ToList().Select(x => x.MessageEntity asMessageEntity).Select(x => x.Content).ToArray()); //发送邮件
                    MailUtils.Sendmail(senderMailAddress, null, listTo, title, message, SMTPHost, port, isSSL, null, userName, pwd);
                }
                catch (Exceptionex)
                {
                    ILogService logService = Kingdee.BOS.Contracts.ServiceFactory.GetLogService(this.Context);
                    Kingdee.BOS.Core.Log.LogObject log = newKingdee.BOS.Core.Log.LogObject();
                    log.Description = ex.Message;
                    logService.WriteLog(this.Context, log);
                }
            }
private string DoProclaimText(string CipherText)
        {
            string result;
            if (StringUtils.IsEmpty(CipherText))
            {
                result = CipherText;
            }
            else
            {
                byte[] bytes = Encoding.BigEndianUnicode.GetBytes(CipherText);
                int num = bytes.Length;
                byte[] array = new byte[num / 2];
                for (int i = 0; i < num; i += 4)
                {
                    byte b = bytes[i + 1];
                    byte b2 = bytes[i + 3];
                    int num2 = (int)(b & 15) << 4;
                    int num3 = (int)(b & 240);
                    int num4 = (int)(b2 & 15);
                    int num5 = (b2 & 240) >> 4;
                    array[i / 2] = Convert.ToByte(num2 | num5);
                    array[i / 2 + 1] = Convert.ToByte(num3 | num4);
                }
                result = Encoding.BigEndianUnicode.GetString(array, 0, array.Length);
            }
            return result;
        }



发云之家消息


private void SendYZJMessage(Context ctx, DynamicObject inquirySysParam, string msg)
        {
            if (inquirySysParam != null && inquirySysParam.DynamicObjectType.Properties.ContainsKey("YT_YZJSendId"))
            {
                DynamicObjectCollection dynamicObjectCollection = inquirySysParam["YT_YZJSendId"] as DynamicObjectCollection;
                if (this.publicAccountService == null)
                {
                    this.publicAccountService = ServiceFactory.GetPublicAccountService(ctx);
                }
                this.yzjPublickAccount = this.publicAccountService.GetPublicAccountByID(ctx, 10038L);
                if (this.yzjPublickAccount != null)
                {
                    if (dynamicObjectCollection != null && dynamicObjectCollection.Count > 0)
                    {
                        if (this.xunTongService == null)
                        {
                            this.xunTongService = ServiceFactory.GetXunTongService(ctx);
                        }
                        if (this.userXunTongService == null)
                        {
                            this.userXunTongService = ServiceFactory.GetService<IUserXunTongService>(ctx);
                        }
                        List<string> xunTongOpenIds = this.userXunTongService.GetXunTongOpenIds(ctx, (from x in dynamicObjectCollection
                                                                                                      select Convert.ToString(x["YT_YZJSendId_Id"])).ToList<string>());
                        if (xunTongOpenIds.Count > 0)
                        {
                            XTSingleTextMessage xtsingleTextMessage = new XTSingleTextMessage();
                            xtsingleTextMessage.Title = "付款成功";
                            xtsingleTextMessage.Date = DateTime.Now.ToString();
                            xtsingleTextMessage.Text = msg;
                            xtsingleTextMessage.IsOpenUser = true;
                            xtsingleTextMessage.Users = xunTongOpenIds;
                            xtsingleTextMessage.PubAcctCode = Convert.ToString(this.yzjPublickAccount["ACCOUNTID"]);
                            xtsingleTextMessage.PubAcctKey = Convert.ToString(this.yzjPublickAccount["ACCOUNTKEY"]);
                            try
                            {
                                XTSendResult xtsendResult = this.xunTongService.SendSingleMessage(ctx, xtsingleTextMessage);
                            }
                            catch (Exception ex)
                            {
                                Logger.Error("YT", "云之家消息发送失败", ex);
                            }
                        }
                    }
                }
            }
        }


发消息机器人

image.png


利用webhook 发送http请求,传输指定JSON即可。

参考文档:https://www.yunzhijia.com/imbu/help/groupRobot.html

图标赞 20
20人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!