WCF服务无法通过MailDefinition发送邮件

// stuff...... return SendCreationMail(Membership.GetUser((Guid)request.UserEntityId), request, new Control()); } private const string TemplateRoot = "~/app_shared/templates/mail/"; private const string ServerRoot = TemplateRoot + "server/"; public static bool SendCreationMail(MembershipUser user, IServerAccountRequest request, Control owner) { var definition = new MailDefinition { BodyFileName = string.Concat(ServerRoot, "creation.htm"), IsBodyHtml = true }; var subject = "New {0} account created!".FormatWith(request.ServerApplicationContract.Id); var data = ExtendedData(DefaultData, subject, user); data.Add("", request.ServerApplicationContract.Id); data.Add("", request.ServerApplicationContract.ApplicationName); data.Add("", request.AccountUsername); data.Add("", "/server/{0}/info".FormatWith(request.ServerApplicationContract.Id.ToLower())); return definition.CreateMailMessage(user.Email, data, owner).Send(subject, ApplicationConfiguration.MailSenderDisplayName); } 

我明白了:

值不能为空。 参数名称:basepath

在System.Web.Util.UrlPath.Combine(String appPath,String basepath,String relative)
在System.Web.UI.WebControls.MailDefinition.CreateMailMessage(字符串收件人,IDictionary替换,控件所有者)
在Damnation.Website.Main.Common.MailTemplating.Server.SendCreationMail(MembershipUser用户,IServerAccountRequest请求,控件所有者)
在Damnation.Website.Main.Common.MailTemplating.Server.SendCreationMail(IServerAccountRequest request)
在Damnation.Website.Main.Business.Model.ServerAccountRequest.UpdateRequestsAfterServerProcessing(IEnumerable`1 results)

事情是我没有一个实际的控件传递给它,我想知道如何设置一个控制,以避免这种无意义的exception。 我正在使用相对路径……所以这没有任何意义。

具有该服务的应用程序位于ASP.NET WebForms .NET 4下。使用者应用程序也是.NET 4下的控制台应用程序

我遇到了同样的问题,并且我了解到这种情况正在发生,因为在尝试执行definition.CreateMailMessage时无法找到控件的路径。 您想要创建一个空白的Web用户控件。 也许这不是最优雅的解决方案,但它可以完成工作。

这就是我做的:

1)在项目中添加Web用户控件文件,例如Test.ascx。

2)在.svc文件中添加以下代码:

 Page page = new Page(); Test test = (Test)page.LoadControl("Test.ascx"); 

3)将你的definition.CreateMailMessage行更新为:

 return definition.CreateMailMessage(user.Email, data, test).Send(subject, ApplicationConfiguration.MailSenderDisplayName); 

您将不再获得basepath nullexception。

我有同样的问题,但这是因为我在一个项目的.NET控制台应用程序,当我将代码传递给ASP.NET时,我运行良好而不创建页面和测试。