首页 > 解决方案 > 'NEIQC.Models.Common.Email' 的类型初始化程序引发异常

问题描述

使用以下电子邮件功能时,我收到错误消息

'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' 的类型初始化器抛出异常。Source=NEIQC TypeName=NEIQC.Models.Common.Email StackTrace:在 NEIQC.Models.Common.Email.SendMail(String Username, String MailContent) at NEIQC.Controllers.AppController.AddVendorToList(FormCollection vendorList) in InnerException: System.NullReferenceException HResult =-2147467261 Message=对象引用未设置为对象的实例。Source=NEIQC StackTrace:在 NEIQC.Models.Common.Email..cctor() 中的 InnerException:

标签: c#asp.netexceptionnullpointerexception

解决方案


我怀疑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>

推荐阅读