MailDefinition / MailMessage and basepath cannot be null

I am creating MailMessagewith the code below and I am getting this error when called mail.CreateMailMessage.

The value cannot be null. Parameter Name: basepath

Does 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;
        }
    }
+4
source share
1 answer

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.

+1
source

Source: https://habr.com/ru/post/1527865/


All Articles