Пример #1
0
        /// <summary>
        /// 执行远程接口url的定时任务
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task Execute(IJobExecutionContext context)
        {
            YuebonCacheHelper yuebonCacheHelper = new YuebonCacheHelper();
            SysSetting        sysSetting        = yuebonCacheHelper.Get("SysSetting").ToJson().ToObject <SysSetting>();
            Stopwatch         stopwatch         = new Stopwatch();

            stopwatch.Start();
            AbstractTrigger trigger     = (context as JobExecutionContextImpl).Trigger as AbstractTrigger;
            string          sqlWhere    = string.Format("Id='{0}' and GroupName='{1}'", trigger.Name, trigger.Group);
            TaskManager     taskManager = iService.GetWhere(sqlWhere);
            string          httpMessage = "";

            if (taskManager == null)
            {
                FileQuartz.WriteErrorLog($"任务不存在");
                return(Task.Delay(1));
            }
            FileQuartz.InitTaskJobLogPath(taskManager.Id);
            string msg = $"开始时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff")}";

            //记录任务执行记录
            iService.RecordRun(taskManager.Id, JobAction.开始, true, msg);
            if (string.IsNullOrEmpty(taskManager.JobCallAddress) || taskManager.JobCallAddress == "/")
            {
                FileQuartz.WriteErrorLog($"{ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff")}未配置任务地址,");
                iService.RecordRun(taskManager.Id, JobAction.结束, false, "未配置任务地址");
                return(Task.Delay(1));
            }
            try
            {
                Dictionary <string, string> header = new Dictionary <string, string>();
                if (!string.IsNullOrEmpty(taskManager.JobCallParams))
                {
                    httpMessage = HttpRequestHelper.HttpPost(taskManager.JobCallAddress, taskManager.JobCallParams, null, header);
                }
                else
                {
                    httpMessage = HttpRequestHelper.HttpGet(taskManager.JobCallAddress);
                }
                stopwatch.Stop();
                string content = $"结束时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff")} 共耗时{stopwatch.ElapsedMilliseconds} 毫秒,消息:{httpMessage??"OK"}\r\n";
                iService.RecordRun(taskManager.Id, JobAction.结束, true, content);
                if ((MsgType)taskManager.SendMail == MsgType.All)
                {
                    string emailAddress = sysSetting.Email;
                    if (!string.IsNullOrEmpty(taskManager.EmailAddress))
                    {
                        emailAddress = taskManager.EmailAddress;
                    }

                    List <string> recipients = new List <string>();
                    recipients = taskManager.EmailAddress.Split(",").ToList();
                    //recipients.Add(taskManager.EmailAddress);
                    var mailBodyEntity = new MailBodyEntity()
                    {
                        Body       = content + "\n\r请勿直接回复本邮件!",
                        Recipients = recipients,
                        Subject    = taskManager.TaskName,
                    };
                    SendMailHelper.SendMail(mailBodyEntity);
                }
            }
            catch (Exception ex)
            {
                stopwatch.Stop();
                string content = $"结束时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff")} 共耗时{stopwatch.ElapsedMilliseconds} 毫秒\r\n";
                iService.RecordRun(taskManager.Id, JobAction.结束, false, content + ex.Message);
                FileQuartz.WriteErrorLog(ex.Message);
                if ((MsgType)taskManager.SendMail == MsgType.Error || (MsgType)taskManager.SendMail == MsgType.All)
                {
                    string emailAddress = sysSetting.Email;
                    if (!string.IsNullOrEmpty(taskManager.EmailAddress))
                    {
                        emailAddress = taskManager.EmailAddress;
                    }
                    List <string> recipients = new List <string>();
                    recipients = emailAddress.Split(",").ToList();
                    var mailBodyEntity = new MailBodyEntity()
                    {
                        Body       = ex.Message + "\n\r请勿直接回复本邮件!",
                        Recipients = recipients,
                        Subject    = taskManager.TaskName,
                    };
                    SendMailHelper.SendMail(mailBodyEntity);
                }
            }

            return(Task.Delay(1));
        }
Пример #2
0
        public Task Execute(IJobExecutionContext context)
        {
            DateTime          dateTime          = DateTime.Now;
            YuebonCacheHelper yuebonCacheHelper = new YuebonCacheHelper();
            SysSetting        sysSetting        = yuebonCacheHelper.Get("SysSetting").ToJson().ToObject <SysSetting>();
            Stopwatch         stopwatch         = new Stopwatch();

            stopwatch.Start();
            AbstractTrigger trigger     = (context as JobExecutionContextImpl).Trigger as AbstractTrigger;
            string          sqlWhere    = string.Format("Id='{0}' and GroupName='{1}'", trigger.Name, trigger.Group);
            TaskManager     taskManager = iService.GetWhere(sqlWhere);

            if (taskManager == null)
            {
                FileQuartz.WriteErrorLog($"任务不存在");
                return(Task.Delay(1));
            }
            try
            {
                string msg = $"开始时间:{dateTime.ToString("yyyy-MM-dd HH:mm:ss ffff")}";
                //记录任务执行记录
                iService.RecordRun(taskManager.Id, JobAction.开始, true, msg);
                //初始化任务日志
                FileQuartz.InitTaskJobLogPath(taskManager.Id);
                var jobId = context.MergedJobDataMap.GetString("OpenJob");
                //todo:这里可以加入自己的自动任务逻辑
                Log4NetHelper.Info(DateTime.Now.ToString() + "执行任务");


                stopwatch.Stop();
                string content = $"结束时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff")} 共耗时{stopwatch.ElapsedMilliseconds} 毫秒\r\n";
                iService.RecordRun(taskManager.Id, JobAction.结束, true, content);
                if ((MsgType)taskManager.SendMail == MsgType.All)
                {
                    string emailAddress = sysSetting.Email;
                    if (!string.IsNullOrEmpty(taskManager.EmailAddress))
                    {
                        emailAddress = taskManager.EmailAddress;
                    }

                    List <string> recipients = new List <string>();
                    recipients = emailAddress.Split(",").ToList();
                    var mailBodyEntity = new MailBodyEntity()
                    {
                        Body       = msg + content + ",请勿回复本邮件",
                        Recipients = recipients,
                        Subject    = taskManager.TaskName
                    };
                    SendMailHelper.SendMail(mailBodyEntity);
                }
            }
            catch (Exception ex)
            {
                stopwatch.Stop();
                string content = $"结束时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff")} 共耗时{stopwatch.ElapsedMilliseconds} 毫秒\r\n";
                iService.RecordRun(taskManager.Id, JobAction.结束, false, content + ex.Message);
                FileQuartz.WriteErrorLog(ex.Message);
                if ((MsgType)taskManager.SendMail == MsgType.Error || (MsgType)taskManager.SendMail == MsgType.All)
                {
                    string emailAddress = sysSetting.Email;
                    if (!string.IsNullOrEmpty(taskManager.EmailAddress))
                    {
                        emailAddress = taskManager.EmailAddress;
                    }

                    List <string> recipients = new List <string>();
                    recipients = emailAddress.Split(",").ToList();
                    var mailBodyEntity = new MailBodyEntity()
                    {
                        Body       = "处理失败," + ex.Message + ",请勿回复本邮件",
                        Recipients = recipients,
                        Subject    = taskManager.TaskName
                    };
                    SendMailHelper.SendMail(mailBodyEntity);
                }
            }

            return(Task.Delay(1));
        }
Пример #3
0
        static void Main(string[] args)
        {
            try
            {
                //SendMail.SendMail.Send163Demo();
                //SendMail.Send163();
                //SendMail.SendQQ();
                //SendMail.SendHotmailDemo();

                var ccList = new List <string>();
                ccList.Add("*****@*****.**");
                var recipients = new List <string>();
                recipients.Add("*****@*****.**");
                var mailBodyEntity = new MailBodyEntity()
                {
                    Body = "这是一个测试邮件body内容<a href='http://www.baidu.com'>123</a>",
                    Cc   = ccList,
                    //MailBodyType = "html",
                    //MailFiles = new List<MailFile>(new MailFile { MailFilePath = "", MailFileSubType = "", MailFileType = "" }),
                    MailFiles = new List <MailFile>()
                    {
                        new MailFile {
                            MailFilePath = @"D:\文档\Visual Studio 2017\Projects\EMailDemo\EMailDemo.ConsoleApp\bin\File\20180807165402.png", MailFileSubType = "png", MailFileType = "image"
                        },
                        new MailFile {
                            MailFilePath = @"D:\文档\Visual Studio 2017\Projects\EMailDemo\EMailDemo.ConsoleApp\bin\File\TIM截图20180807165402.png", MailFileSubType = "png", MailFileType = "image"
                        }
                    },
                    //MailFilePath = @"D:\文档\Visual Studio 2017\Projects\EMailDemo\EMailDemo.ConsoleApp\bin\File\20180807165402.png",
                    //MailFileSubType = "png",
                    //MailFileType = "image",
                    //MailTextBody = "这里是邮件文本内容<a href='http://www.baidu.com'>2345</a>",
                    Recipients    = recipients,
                    Sender        = "邮件的发件人",
                    SenderAddress = "*****@*****.**",
                    Subject       = "测试邮件是否可以发送的标题",
                };
                var sendServerConfiguration = new SendServerConfigurationEntity()
                {
                    SenderPassword = "******",
                    SmtpPort       = 465,
                    IsSsl          = true,
                    MailEncoding   = "utf-8",
                    SenderAccount  = "*****@*****.**",
                    SmtpHost       = "smtp.qq.com",
                };

                var mailModel = new MailModel()
                {
                    Password        = "******",
                    Port            = 465,
                    EMailPriority   = MailPriority.High,
                    CcAddress       = ccList,
                    ToAddress       = recipients,
                    Body            = "这是一个测试邮件body内容<a href='http://www.baidu.com'>123</a>",
                    DeliveryMethod  = SmtpDeliveryMethod.Network,
                    EnableSsl       = true,
                    FromMailAddress = "*****@*****.**",
                    Host            = "smtp.qq.com",
                    Subject         = "test mail send",
                    UserName        = "******",
                    MailFiles       = new List <string>()
                    {
                        @"D:\文档\Visual Studio 2017\Projects\EMailDemo\EMailDemo.ConsoleApp\bin\File\20180807165402.png", @"D:\文档\Visual Studio 2017\Projects\EMailDemo\EMailDemo.ConsoleApp\bin\File\TIM截图20180807165402.png"
                    },
                };

                //var result=SeedMailHelper.SendMail(mailBodyEntity, sendServerConfiguration);
                //ReceiveEmailHelper.ReceiveEmail();
                //ReceiveEmailHelper.DownloadBodyParts();


                Console.WriteLine("成功!");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }