WCF Service fails to send mail through MailDefinition

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.

Tags:

C#

Email

Wcf