c# - '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:
解决方案
我怀疑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>
推荐阅读
- android - 使用 google oauth 登录返回“代码:1”
- azure-devops - Azure DevOps 本地缓存容器作业
- javascript - youtube v3 api 搜索在 Chrome 中失败,但在 Firefox 中有效
- css - CSS - 打印 html 文档
- mysql - 内部连接和 AND 值 IN(查询)上的 MySQL 语法错误
- html - 在 Boostrap 4 中将多个导航选项卡与多个导航栏同步
- excel - SumIF 值 <> 下一列中的“#NV”
- php - 从数组中提取某些键
- sql - MariaDB - 如果记录存在则插入记录或更新时间戳
- javascript - 当我在 Javascript 中使用 setInterval 从另一个方法调用方法时,未捕获的 TypeError 不是函数