【问题标题】:Cannot implicitly convert to type string to System.Net.Mail.MailAddress无法将类型字符串隐式转换为 System.Net.Mail.MailAddress
【发布时间】:2014-06-16 14:13:56
【问题描述】:

我正在开发一个正在更新跟踪表单的库存应用程序系统,并且我最近从 System.Web.Mail 转换为 System.Net.Mail。

但是,关于我在物品转移失败时处理的异常 if-else 语句,我得到:

“无法将类型'string'隐式转换为'System.Net.Mail.MailAddressCollection” 以下代码块出错:

else //If the Item Transfer Fails
{
   _formToUpdate.WorkFlowStep = "d";
   _formToUpdate.InventoryCertifier_Initials = dbcontext.Custodies.Where(x => x.Custody_eraider == User.Identity.Name.ToLower()
   _formToUpdate.InventoryCertifierInitials_DtTm = DateTime.Now;
   _formToUpdate.IsFormLockedforEditing = true;

//The notification lists are different in both cases.
NotificationEmail.To = dbcontext.Custodies.Where(x => x.Custody_eraider ==
_formToUpdate.TransferFrom_Sign).Single().Custody_Email + ","
 + dbcontext.Custodies.Where(x => x.Custody_eraider == _formToUpdate.TransferTo_Sign).
Single().Custody_Email + "," + dbcontext.Custodies.Where(x => x.Custody_eraider ==
_formToUpdate.Form_Approv_Reject_By).Single().Custody_Email + ","
+ dbContext.Custodies.Where(x => x.Custody_eraider == _formToUpdate.InventoryCertifier_
Initials).Single().Custody_Email;
}

错误只发生在以 NotificationEmail.To = ... 开头的代码部分中

我只是不太确定如何正确修改该声明。

任何帮助将不胜感激。如果我需要提供更详细的信息,请告诉我。谢谢!

【问题讨论】:

  • 顺便说一句,我已经将 NotificationEmail 声明为: var NotificationEmail = new MailMessage();我没有把那部分代码放进去,因为下面大约有 100-120 行不适用于我遇到的问题。只是想澄清 NotificationEmail 已声明。

标签: type-conversion system.net.mail


【解决方案1】:

错误告诉您无法将字符串转换为MailAddressCollection

System.Net.Mail.MailMessage 类中,the To property is a MailAddressCollection,而不是System.Web.Mail.MailMessage 中的字符串。

您需要将电子邮件地址添加到该集合中。 MailAddressCollection 有一个 Add 方法,看起来它接受格式与以前相同的字符串,因此您的代码应该如下所示:

NotificationEmail.To.Add(/* your expression here */);

【讨论】:

  • 好的,谢谢!这解决了我的问题,并帮助我清除了其他文件中的许多其他类似错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-09
  • 2014-05-15
  • 2014-02-04
  • 2011-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多