电子邮件时间表与Quartz + asp.net

本文关键字:asp net Quartz 时间表 电子邮件 | 更新日期: 2023-09-27 18:16:38

我正在使用Quartz做一个简单的邮件发送测试,如下所示,不知何故在跟踪日志中,我得到了这个错误:

12:42:00 [ServerScheduler_Worker-7] DEBUG HelloWorldQuartzDotNet.HelloWorldJob - 
***
Failed: Value cannot be null.
Parameter name: address
***

我相信这与从我的app.config键值中提取值有关。有人能告诉我我做错了什么吗?我可以保证电子邮件发送代码工作良好,因为我提取了电子邮件代码部分,并尝试将其作为一个独立的新项目。

谢谢。

 public void Execute(IJobExecutionContext context)        
    {
        try
        {
            Log.DebugFormat("{0}****{0}Job {1} fired @ {2} next scheduled for {3}{0}***{0}",
                            Environment.NewLine,
                            context.JobDetail.Key,
                            context.FireTimeUtc.Value.ToString("r"),
                            context.NextFireTimeUtc.Value.ToString("r"));
            MailMessage mail = new MailMessage();
            mail.IsBodyHtml = true;
            mail.From = new MailAddress(ConfigurationManager.AppSettings.Get("COMPANYEMAIL"),
                                        ConfigurationManager.AppSettings.Get("COMPANYNAME"));
            mail.To.Add("me1234@hotmail.com");
            mail.Subject = "Daily Email";
            string body = "";
            string filePath =
                HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["EmailTemplates"] + "/");
            if (File.Exists(filePath + "Test.htm"))
            {
                FileStream f1 = new FileStream(filePath + "Test.htm", FileMode.Open);
                StreamReader sr = new StreamReader(f1);
                body = sr.ReadToEnd();
                body = body.Replace("<%UserName%>", "Mr Big");
                f1.Close();
            }
            mail.Body = body;
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess |
                                               DeliveryNotificationOptions.OnFailure;
            var Th = new Thread(() => Email.Send(mail, 3, 3000, true));
            Th.Start();
            Log.DebugFormat("{0}***{0}Daily Email Sent!{0}***{0}", Environment.NewLine);
        }
        catch (Exception ex)
        {
            Log.DebugFormat("{0}***{0}Failed: {1}{0}***{0}", Environment.NewLine, ex.Message);
        }
    }

我的app.config部分:

<quartz>
    <add key="quartz.scheduler.instanceName" value="ServerScheduler"/>
    <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/>
    <add key="quartz.threadPool.threadCount" value="10"/>
    <add key="quartz.threadPool.threadPriority" value="2"/>
    <add key="quartz.jobStore.misfireThreshold" value="60000"/>
    <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/>
    <add key="USER_DEFAULTPHOTO_PATH" value="UserProfilePhoto/default.png" />
    <add key="USER_PROFILEPHOTO_PATH" value="UserProfilePhoto" />
    <add key="MAX_INBOX_COUNT" value="200" />
    <add key="FAILONANYADDRESS" value="false" />
    <add key="COMPANYEMAIL" value="coy1234@hotmail.com" />
    <add key="COMPANYNAME" value="MyApp" />
    <add key="EMAILTEMPLATES" value="EmailTemplates" />
    <system.net>
      <mailSettings>
        <smtp deliveryMethod="Network" from="coy1234@hotmail.com">
          <network host="smtp.live.com" port="25" userName="coy1234@hotmail.com" password="123456789" />
        </smtp>
      </mailSettings>
    </system.net>
  </quartz>

电子邮件时间表与Quartz + asp.net

您的配置完全在石英配置部分,这只意味着石英。网络基础设施。只支持以石英开头的键。应该在那里。这就是为什么ConfigurationManager。AppSettings返回null

正确的配置应该是这样的:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    </configSections>
  <appSettings>
    <add key="USER_DEFAULTPHOTO_PATH" value="UserProfilePhoto/default.png" />
    <add key="USER_PROFILEPHOTO_PATH" value="UserProfilePhoto" />
    <add key="MAX_INBOX_COUNT" value="200" />
    <add key="FAILONANYADDRESS" value="false" />
    <add key="COMPANYEMAIL" value="coy1234@hotmail.com" />
    <add key="COMPANYNAME" value="MyApp" />
    <add key="EMAILTEMPLATES" value="EmailTemplates" />
  </appSettings>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="coy1234@hotmail.com">
        <network host="smtp.live.com" port="25" userName="coy1234@hotmail.com" password="123456789" />
      </smtp>
    </mailSettings>
  </system.net>
  <quartz>
    <add key="quartz.scheduler.instanceName" value="ServerScheduler"/>
    <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/>
    <add key="quartz.threadPool.threadCount" value="10"/>
    <add key="quartz.threadPool.threadPriority" value="2"/>
    <add key="quartz.jobStore.misfireThreshold" value="60000"/>
    <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/>
  </quartz>
</configuration>