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

bev*_*qua 6 c# email wcf

    // 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("<%ServerApplication%>", request.ServerApplicationContract.Id);
    data.Add("<%ServerApplicationName%>", request.ServerApplicationContract.ApplicationName);
    data.Add("<%AccountUsername%>", request.AccountUsername);

    data.Add("<%ServerInfo%>", "/server/{0}/info".FormatWith(request.ServerApplicationContract.Id.ToLower()));

    return definition.CreateMailMessage(user.Email, data, owner).Send(subject, ApplicationConfiguration.MailSenderDisplayName);
}
Run Code Online (Sandbox Code Playgroud)

我明白了:

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

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

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

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

小智 4

我遇到了同样的问题,我了解到发生这种情况是因为在尝试执行您的定义时找不到控件的路径。CreateMailMessage。您想要创建一个空白的 Web 用户控件。也许这不是最优雅的解决方案,但它确实有效。

这就是我所做的:

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

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

Page page = new Page();

Test test = (Test)page.LoadControl("Test.ascx");
Run Code Online (Sandbox Code Playgroud)

3)将您的定义.CreateMailMessage 行更新为:

return definition.CreateMailMessage(user.Email, data, test).Send(subject, ApplicationConfiguration.MailSenderDisplayName);
Run Code Online (Sandbox Code Playgroud)

您将不再遇到基本路径空异常。