开发者

WCF Service fails to send mail through 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);
}

I get:

Value cannot be null. Parameter name: basepath

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.开发者_JAVA技巧.. so this makes no sense.

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


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.

This is what I did:

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

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

Page page = new Page();

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

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.


I had the same problem but it was because I was on a project .NET console application, when I passed the code to ASP.NET not I run well without creating Page and Test.


I had the same error but the problem for me was that I was using relative paths for my BodyFileName and EmbeddedObjects. Changing them to absolute paths fixed this error for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜