Q:

The type initializer for 'NEIQC.Models.Common.Email' threw an exception

While using the below Email Function I am getting error as

The type initializer for 'NEIQC.Models.Common.Email' threw an exception.

Function code is below
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);
    }

To this function I am going through from below mentioned code.
strMailContent = Get_Email_Content(strVendorName, strVendorCode, strValidFrom, strValidTo, strCreatedBy);                

 Email.SendMail(strCreatedBy, strMailContent);

I dont know why this error is coming. Please help
UPDATE
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);
    }
}

Inner exception details
System.TypeInitializationException was caught

HResult=-2146233036
  Message=The type initializer for 'NEIQC.Models.Common.Email' threw an exception.
  Source=NEIQC
  TypeName=NEIQC.Models.Common.Email
  StackTrace:
       at NEIQC.Models.Common.Email.SendMail(String Username, String MailContent)
       at NEIQC.Controllers.AppController.AddVendorToList(FormCollection vendorList) in 
  InnerException: System.NullReferenceException
       HResult=-2147467261
       Message=Object reference not set to an instance of an object.
       Source=NEIQC
       StackTrace:
            at NEIQC.Models.Common.Email..cctor() in 
       InnerException: 

A:

I suspect that From settings  is not well filled in the application configuration file as it's the only thing which can lead to a type initialization error + NullPointerException in the code that you shown.
The part of the code which throw the Exception will be  : 
  static string staticFromAddress = ConfigurationManager.AppSettings["From"].ToString();

In this case, the mistake is in web.config
  <appSettings>
    <add key="From" value="somehing"/> <!-- Missing a line like this -->
  </appSettings>

