利用quartz实现邮件的定时发送

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用quartz实现邮件的定时发送相关的知识,希望对你有一定的参考价值。

利用quartz框架,实现邮件定点发送:
1.先建一个winform程序,拖几个lable和textbox,button控件,修改相应的信息
[img]http://images2017.cnblogs.com/blog/1136602/201709/1136602-20170902133256749-663976292.png[/img]
2.点击button按钮,在事件下添加如下代码:[code=csharp]
 private void button1_Click(object sender, EventArgs e)
        {
            string str1 = textBox1.Text;//发件人
            string str2 = textBox2.Text;//抄送人
            string str3 = textBox3.Text;//收件人
            string str4 = textBox4.Text;//密码
            // string str5=    
            QuartzJob quarztJob = new QuartzJob();//定义具体//操作IJob接口中发邮件的实例
            quarztJob.Set1(str1);
            quarztJob.Set2(str2);
            quarztJob.Set3(str3);
            quarztJob.Set4(str4);
            //Rdlc rdlc = new Rdlc();
           // object sender1;
          //  rdlc.Form2_Load(sender, e);
            IScheduler sch = StdSchedulerFactory.GetDefaultScheduler(); //从工厂中获取一个调度器实例化
            sch.Start();
            IJobDetail job1 = JobBuilder.Create<QuartzJob>().WithIdentity("myJob1", "group1")
                             .Build();                            
            ITrigger tri1 = TriggerBuilder.Create().WithIdentity("myTrigger1", "group1").
                             StartNow().                                //一旦加入scheduler,立即生效
                             WithCronSchedule("0 21 13 ? * *").Build();  //corn like表达式,
每天下午一点21分的时候发送邮件
   &&WithSimpleSchedule (x=>x.
            sch.ScheduleJob(job1, tri1);//加载任务和触发器
        } 
 3.接下来在QuartzJob中写发送邮件的具体细节,代码如下:                                      
[/code][code=csharp]
  public class QuartzJob : IJob
    {
        public QuartzJob()
        {

        }
        private static string str11, str22, str33, str44;//设置成静态变量,这样在Button作用下修改str11,str22,str33,
                                                         //str44的属性值的将不会再改变
                                                         //修改变量
        public void Set1(string _str11)
        {
            //private string str11;
            str11 = _str11; //发件人
        }
        public void Set2(string _str22)
        {
            str22 = _str22;//抄送人
        }
        public void Set3(string _str33)
        {
            str33 = _str33;//收件人
        }
        public void Set4(string _str44)
        {
            str44 = _str44;//密码
        }

        public void Execute(IJobExecutionContext context)
        {
            //jobdatamap datamap = context.mergedjobdatamap 
            SendMail();
        }
        // string str11 = "[email protected]", str22 = "";
        public void SendMail()//定义发送邮件
        {
            SmtpClient client = new SmtpClient();
            client.Host = "smtp.qq.com";
            client.UseDefaultCredentials = true;
            client.EnableSsl = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            //  client.Credentials = new NetworkCredential("[email protected]", "20140526XC");
            //_Default default= new _Default();
            //string username = _Default.str1;
            client.Credentials = new NetworkCredential(str11, str44);
            MailMessage message = new MailMessage();
            message.From = new MailAddress(str11);
            message.To.Add(str33);
            message.CC.Add(str22);
            var attach = new Attachment(@"C:\\Users\\Dell\\pictures\\rdlc.jpeg", MediaTypeNames.Image.Jpeg);
            //设置contentid                                                
            attach.ContentId = "pic";
            //zip附件
            var attach2 = new Attachment(@"d:\\b.zip", "application/x-zip-compressed");
            message.Attachments.Add(attach);
            message.Attachments.Add(attach2);
            message.Subject = "morning";
            message.SubjectEncoding = Encoding.UTF8;
            //message.body="你好帅!";
            message.Body = "<img src=\\"cid:pic\\"/><p>附件:picture and zip!</p>";
            message.BodyEncoding = Encoding.UTF8;
            // message.subjectencoding = encoding.utf8;
            // message.bodyencoding = encoding.utf8;
            message.Priority = MailPriority.High;
            message.IsBodyhtml = true;
            client.Send(message);
        }
    }
[/code]总结,我们需要将发送邮件的具体细节放在QuartzJob中,等trigger调度job1时,就可以实现邮件的发送了,希望我的代码能帮到各位亲!

以上是关于利用quartz实现邮件的定时发送的主要内容,如果未能解决你的问题,请参考以下文章

Spring + quartz实现定时发送邮件功能

利用spring+quartz-scheduler+MySql实现定时任务动态调整

利用spring+quartz-scheduler+MySql实现定时任务动态调整

java实现qq邮箱每天定时发送邮件

基于zookeeper和quartz实现分布式定时调度

centos7 利用mailx发送邮件