Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Service fails to send mail through MailDefinition

Tags:

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);
}

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... 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

like image 1000
bevacqua Avatar asked Aug 25 '11 04:08

bevacqua


1 Answers

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.

like image 197
Ana Zecchini Avatar answered Oct 03 '22 14:10

Ana Zecchini