“ NEIQC.Models.Common.Email”的类型初始值设定项引发了异常

时间:2019-02-14 10:42:47

标签: c# asp.net exception nullpointerexception

使用下面的电子邮件功能时,出现错误信息

  

“ 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(字符串用户名,字符串MailContent)        在NEIQC.Controllers.AppController.AddVendorToList(FormCollection vendorList)中   InnerException:System.NullReferenceException        HResult = -2147467261        Message =对象引用未设置为对象的实例。        来源= NEIQC        堆栈跟踪:             在NEIQC.Models.Common.Email..cctor()中        InnerException:

1 个答案:

答案 0 :(得分:1)

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

引发异常的代码部分将是:

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

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

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