0

我正在使用 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>
4

1 回答 1

0

您的配置完全在石英配置部分中,这仅适用于 Quartz.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>
于 2013-06-02T16:12:43.433 回答