Question

I'm creating a MailMessage with the code below and I'm receiving this error when calling mail.CreateMailMessage.

Value cannot be null. Parameter name: basepath

Anyone know what could be wrong?

public void SendReciept(string reciever)
    {
        MailDefinition mail = new MailDefinition();
        mail.IsBodyHtml = true;

        mail.BodyFileName = "~/file.txt";
        mail.Subject = "Subject";
        mail.From = "noreply@xxx.com";
        mail.Priority = System.Net.Mail.MailPriority.Normal;

        MailMessage message = mail.CreateMailMessage(reciever, RecieptReplacements, new System.Web.UI.Control());

        ...
    }

    ListDictionary RecieptReplacements
    {
        get
        {
            ListDictionary replacements = new ListDictionary();

            replacements.Add("<%Name%>", "Name");

            return replacements;
        }
    }
Was it helpful?

Solution

You need to create a Web User Control (.ascx file), and then load that control and pass it to CreateMailMessage.

// Create a dummy page and use it to load a dummy control so that CreateMailMessage() will not complain
Page page = new Page();
DummyControl control = (DummyControl)page.LoadControl("~/DummyControl.ascx");           
MailMessage message = mail.CreateMailMessage(reciever, RecieptReplacements, dummycontrol);

Take a look at this answer, which explains why and provides the above solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top