WCF服务无法通过MailDefinition发送邮件 [英] WCF Service fails to send mail through MailDefinition

查看:111
本文介绍了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("<%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);
}

我得到:

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

Value cannot be null. Parameter name: basepath

在System.Web.Util.UrlPath.Combine(String appPath,String basepath,
String relative)


System.Web.UI.WebControls.MailDefinition.CreateMailMessage(String
收件人,IDictionary替换,控制所有者)


Damnation.Website.Main.Common。 MailTemplating.Server.SendCreationMail(MembershipUser
用户,IServerAccountRequest请求,控制所有者)

在Damnation.Website.Main.Common.MailTemplating.Server.SendCreationMail(IServerAccountRequest
请求)

在Damnation.Website.Main.Business.Model.ServerAccountRequest.UpdateRequestsAfterServerProcessing(IEnumerable`1
results)

at System.Web.Util.UrlPath.Combine(String appPath, String basepath, String relative)
at System.Web.UI.WebControls.MailDefinition.CreateMailMessage(String recipients, IDictionary replacements, Control owner)
at Damnation.Website.Main.Common.MailTemplating.Server.SendCreationMail(MembershipUser user, IServerAccountRequest request, Control owner)
at Damnation.Website.Main.Common.MailTemplating.Server.SendCreationMail(IServerAccountRequest request)
at Damnation.Website.Main.Business.Model.ServerAccountRequest.UpdateRequestsAfterServerProcessing(IEnumerable`1 results)

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

Thing is I don't have an actual Control to pass to it, I'd like to know how to setup a control that avoids this senseless exception. I'm using a relative path... so this makes no sense.

具有该服务的应用程序在ASP.NET WebForms .NET 4下。消费者应用程序也是.NET 4下的控制台应用程序

The application that has the service is under ASP.NET WebForms .NET 4. The consumer application is a console application also under .NET 4

推荐答案

我也有同样的问题,我了解到,这是因为它不能尝试执行定义时找到控件的路径.CreateMailMessage。您要创建一个空白的Web用户控件。也许这不是最优雅的解决方案,但它的工作。

I had the same problem, and I learned that this is happening because it cannot find the path of the control when trying to execute your definition.CreateMailMessage. You want to create a blank web user control. Maybe this is not the most elegant solution but it does the work.

这是我做的:

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

1) Add a Web User Control file in your project, for example Test.ascx.

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

2) In your .svc file add the following code:

Page page = new Page();

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

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

3) Update your definition.CreateMailMessage line to:

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

您将不再获得基准空值异常。

You will no longer get the basepath null exception.

这篇关于WCF服务无法通过MailDefinition发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆