【问题标题】:The type initializer for 'NEIQC.Models.Common.Email' threw an exception'NEIQC.Models.Common.Email' 的类型初始化程序引发异常
【发布时间】:2019-07-08 08:49:08
【问题描述】:

在使用下面的电子邮件功能时,我收到了错误

“NEIQC.Models.Common.Email”的类型初始化程序引发异常。

功能代码如下

public static void SendMail(string Username, string MailContent)
    {
        string error = string.Empty;
        string ToEmail = string.Empty;

        ToEmail = Username + "@ril.com";

        EmailService.Service ObjService = new EmailService.Service();
        ObjService.SendMail(ConfigurationManager.AppSettings["FromMailId"].ToString(), ToEmail, "", "", "Newly Added Vendor Information", MailContent, true, Convert.ToString(ConfigurationManager.AppSettings["SMTPServerAddress"]), out error);
    }

我从下面提到的代码中经历这个功能。

strMailContent = Get_Email_Content(strVendorName, strVendorCode, strValidFrom, strValidTo, strCreatedBy);                

 Email.SendMail(strCreatedBy, strMailContent);

我不知道为什么会出现这个错误。请帮忙

更新

public class Email
{
    #region Variable Declarations
    UserProviderClient ObjUMS = new UserProviderClient();

    //string strReturnMessage = "";
    string strAppURL = ConfigurationManager.AppSettings["APPURL"].ToString();
    int GroupId = Int32.Parse(ConfigurationManager.AppSettings["GroupID"]);
    static string staticFromAddress = ConfigurationManager.AppSettings["From"].ToString();
    string strError = "";
    #endregion

    public static void SendMail(string Username, string MailContent)
    {
        string error = string.Empty;
        string ToEmail = string.Empty;

        ToEmail = Username + "@ril.com";

        EmailService.Service ObjService = new EmailService.Service();
        ObjService.SendMail(ConfigurationManager.AppSettings["FromMailId"].ToString(), ToEmail, "Jyotirmoy.Mohanty@ril.com", "", "Newly Added Vendor Information", MailContent, true, Convert.ToString(ConfigurationManager.AppSettings["SMTPServerAddress"]), out error);
    }
}

内部异常详情

System.TypeInitializationException was caught

HResult=-2146233036 Message='NEIQC.Models.Common.Email' 的类型初始化程序引发异常。 来源=NEIQC TypeName=NEIQC.Models.Common.Email 堆栈跟踪: 在 NEIQC.Models.Common.Email.SendMail(字符串用户名,字符串邮件内容) 在 NEIQC.Controllers.AppController.AddVendorToList(FormCollection vendorList) 中 内部异常:System.NullReferenceException H结果=-2147467261 Message=对象引用未设置为对象的实例。 来源=NEIQC 堆栈跟踪: 在 NEIQC.Models.Common.Email..cctor() 中 内部异常:

【问题讨论】:

  • 你能告诉我们你在这个Common.Email类中的所有静态字段和static constructor吗?
  • @Mr.Fahrenheit:更新了代码
  • @prd: 更新了代码

标签: c# asp.net exception nullpointerexception


【解决方案1】:

我怀疑From 设置没有很好地填充到应用程序配置文件中,因为它是唯一可以导致您显示的代码中出现type initialization error + NullPointerException 的东西。

抛出异常的部分代码是:

  static string staticFromAddress = ConfigurationManager.AppSettings["From"].ToString();

在这种情况下,错误在web.config

  <appSettings>
    <add key="From" value="somehing"/> <!-- Missing a line like this -->
  </appSettings>

【讨论】:

  • 不一样,我正在使用另一个..当我在同一个控制器中使用该功能时,它可以工作但不能在类文件中
  • @BN 检查您是否输入了错误的密钥。并尝试使用static string staticFromAddress = (ConfigurationManager.AppSettings["From"] ?? "defaultmail@mail.com").ToString(); 看看您是否遇到相同的异常
  • 现在这样做Email obj = new Email(); obj.SendMail(strCreatedBy, strMailContent);,但得到错误{"Value cannot be null.\r\nParameter name: String"}
猜你喜欢
  • 1970-01-01
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 2017-05-18
  • 2013-05-15
  • 2013-11-08
  • 2013-10-06
  • 2015-08-31
相关资源
最近更新 更多