Question

I'm trying to send a set of emails using episerver mail. I've written the following as a proof of concept

        ISite site = (Site)SiteHandler.Instance.CurrentSite;
       ICategory category = site.Categories[0]; //..  hack I know

        Message message = new Message(subject: "Test 1",
                                        bodyText: "Test 1",
                                        bodyHtml: "Test 1",
                                        container: GetRecipientContainer(),
                                        fromAddress: "demo@demo.com",
                                        fromName: "Email User",
                                        addParametersToRedirectedLinks: false,
                                        category: category,
                                        site: site);

        var returnMessage = EPiServerMailHandler.Send(message, true);

But it keeps crashing with

Exception has been thrown by the target of an invocation. [Object reference not set to an instance of an object.

Specifically it can't get a reference to the site. SiteHandler.Instance.CurrentSite is null

I've tried this as part of a scheduled task (where it ultimately needs to be) and in the context of a page just to try. It's null in both cases.

Has anyone got any idea of how i get this reference. The documentation is obscure for this. I fear it's not possible.

I'm using episerver 7

Many Thanks

EDIT

Digging further into the SDK - the code

EPiServerMailSection.Instance.CurrentSite.SiteId

will return the correct site id. So the scheduler knows the site it just is failing to pick up the site object.

Also the code

SiteHandler.Instance.GetSiteList().Count()

returns a count of 0 so it's clearly not loading a collection of sites somewhere along the line. This seems to be the key issue with it

Was it helpful?

Solution

OK - I've finally fixed this.

  1. You must have at least one section created in episerver mail. Without this CurrentSite is null
  2. Once you've got the site then the category can be obtained from the below code. The send method then stops erroring. Whether it sends mail or not is an open question at the moment - I seem to have SMPT server/spam filter type issues. Oh well.

    ISite site = (ISite)SiteHandler.Instance.CurrentSite;
    ICategory category = EPiServerMailModule.Instance.GetSiteCategory(site);
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top