Developer forum

Forum » Development » C# EmailMarketing send mail

C# EmailMarketing send mail

Jonas Mersholm
Reply

Hi there. I am attempting to schedule an email through the EmailMarketing assembly, but it just wont work out. Heres my code

 

Dynamicweb.Modules.EmailMarketing.Email NewsLetter = new Dynamicweb.Modules.EmailMarketing.Email();
NewsLetter.Template = "Test";
NewsLetter.Subject = "nyhedsbrev";
NewsLetter.TemplateName = "Test";
NewsLetter.CreateMessage(false);
NewsLetter.CreateSendScheduledTask( DateTime.Now.AddMinutes(1) );
​NewsLetter.Save();

 

I would like it to send a mail, using the template named "Test". 

I recieve an error, that the CreateSendScheduledTask method does not exist, even though it is in my intellisense, which means it is atleast in my development assembly V8.

 

Any help would be appreciated.

Best regards

Jonas


Replies

 
Jonas Mersholm
Reply

.

 
Nicolai Høeg Pedersen
Reply

Hi Jonas

Could you post the exception you get?

Thanks, Nicolai

 
Jonas Mersholm
Reply

Sure thing, BTW, how do you make that Syntaxhighlight on your code examples on the forum?

 


Server Error in '/' Application.


Method not found: 'Void Dynamicweb.Modules.EmailMarketing.Email.CreateSendScheduledTask(System.DateTime)'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.MissingMethodException: Method not found: 'Void Dynamicweb.Modules.EmailMarketing.Email.CreateSendScheduledTask(System.DateTime)'.

Source Error: 

 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace: 
 

[MissingMethodException: Method not found: 'Void Dynamicweb.Modules.EmailMarketing.Email.CreateSendScheduledTask(System.DateTime)'.]
   Emailer.EmailCron.ProcessRequest(HttpContext context) +0
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +913
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +165

 


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34009

 
Nicolai Høeg Pedersen
Reply

Hi Jonas

Syntax hightlighting: In the editor you have a "Typografi" dropdown - it contains formatting of various types of code.

I will have someone look into this exception.

BR Nicolai

 
Jonas Mersholm
Reply

And the version number : 8.4.0.3

 
Jonas Mersholm
Reply

Great, thank you Nicolai.

Does that mean, that i am doing it right, generally speaking? Would this sceduele an email to be send after a minute as meant to?

 
Nicolai Høeg Pedersen
Reply

Not sure. But someone will let you know what is wrong :-).

Nicolai

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Jonas,

It would seem that you're working with an older version of the API, probably from during the beta phase of the module. Some of those methods you use are no longer available. The production assemblies might be version 8.4.0.3, but your development assemblies are probably not newer than 8.2.3.x. CreateSendScheduledTask was renamed in 8.3.0.0 to CreateEmailScheduledTask, and CreateMessage was made private and replaced by CreateMessageForOriginal and CreateMessageForVariation in 8.3.1.0. Neither of which you actually need to call yourself.

You should make sure that the development assemblies you use are the same as the ones in the production environment.

In addition, what you're trying to do is not what E-mail Marketing was designed to do. E-mail Marketing is meant to send the contents of a page / item, not a template directly. The Template property can be used to change the layout template of the page when the page is being rendered.

Something like this

public void CreateScheduledEmail()
{
	var email = new Email();
	
	// Set standard properties
	email.SenderEmail = "noreply@domain.com";
	email.SenderName = "My name";
	email.Subject = "My subject";
	//And so on

	// Setup content
	email.PageId = 50;

	// Setup recipients
	var recipientProvider = new AccessUserRecipientProvider();
	recipientProvider.Email = email;
	// All ids are AccessUser ids with the following prefix
	// Users:           'u'
	// Groups:          'g'
	// SmartSearches:   's'
	// Assume users with ids 1000, 1200 and 1201
	// and users in the group with id 1500 are the recipients
	recipientProvider.SetIds(new List< string > { "u1000", "u1200", "u1201", "g1500" });
	email.RecipientProviderConfiguration = recipientProvider.GetParametersToXML();

	// Setup scheduling
	// Schedules the e-mail for single execution next time the scheduler runs.
	// Scheduler runs every 5 minutes, so the execution happens anywhere from Now to Now+5
	email.CreateEmailScheduledTask(DateTime.Now);

	// No need to save the e-mail as CreateEmailScheduledTask does that.
}

This should be enough, but if you run into any issues, please let me know :)

- Jeppe

 
Jonas Mersholm
Reply

Thank you Jeppe. I updated my assemblies and tried your code. I am getting the following compiler errors though

 

Error    1    Property or indexer 'Dynamicweb.Modules.EmailMarketing.EmailRecipientProvider.Email' cannot be assigned to -- it is read only    c:\users\jonas\documents\visual studio 2013\Projects\Emailer\Emailer\EmailCron.ashx.cs    50    13    Emailer

 

Error    2    Property or indexer 'Dynamicweb.Modules.EmailMarketing.Email.RecipientProviderConfiguration' cannot be assigned to -- it is read only    c:\users\jonas\documents\visual studio 2013\Projects\Emailer\Emailer\EmailCron.ashx.cs    59    13    Emailer

 

 

 
Jonas Mersholm
Reply

I noticed an internal set in the assembly, so i have to be in the assembly to actually execute the code. Aw, why so?

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer

With regard to

recipientProvider.Email = email;

My mistake. It is set automatically when getting the recipient provider through the RecipientProvider property on the Email object.

The property RecipientProviderCopnfiguration is settable from 8.4.1.0, but you use 8.4.0.3, right? In that case, you have to get the recipient provider from the RecipientProvider property, and manipulate it directly.

Something like this

var recipientProvider = email.RecipientProvider as AccessUserRecipientProvider;
if (recipientProvider != null)
{
	recipientProvider.SetIds(new List< string > { "u1000", "u1200", "u1201", "g1500" });
}

- Jeppe

Votes for this answer: 1
 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Actually, the property RecipientProviderConfiguration is settable from 8.4.0.9.

 
Jonas Mersholm
Reply

Thank you Jeppe, that did the trick! :)

 - Will this be written into some sort of documentation at any time ?

 
Jonas Mersholm
Reply

One last thing Jeppe. 

The email is send twice, though the method is only called once and the email marketing module states that it has only been send once. Is this a known thing?

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

That is the plan. We already have a few how-to guides for using E-mail Marketing through the UI, and a brief explanation of the placeholder concept; but I would like to add more guides, including API guides.

We will be adding a complete API overview of the E-mail Marketing namespace to our existing API documentation section here on the developer site shortly. Be aware that this section always reflects the newest version of Dynamicweb, so there might not be exactly the same if you use an older version.

This code should not send the e-mail multiple times. Have you set the EmailScheduleRepeatInterval to a value other than 0?

 
Jonas Mersholm
Reply

Thank you for the answer.

 

No, i have not set the EmailScheduleRepeatInterval. Both i, and another test subject recieves two copies of the email. Though the client only states that one have been send out. Weird.

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Like I said, it shouldn't be happening, but we have made a number of fixes to E-mail Marketing and the underlying systems in 8.4.0. Is it possible for you to upgrade to 8.4.0.17 or later and see if that fixes the problem?

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Also, does this also occur if you create an e-mail through the UI and schedule that? What happens if the e-mail is not scheduled but sent directly from your code?

Email.SendEmail(email);
 
Jonas Mersholm
Reply

I get a nullpointer reference at the PageRender step of the SendEmail method.

Our version of DW is 8.4.3, ill do an update at 14:00 Danish Time and see if that fixes the problem.

 
Jonas Mersholm
Reply

FFs. Cant update, need 4.5.1 .net framework. HostNordic support -Here i come...

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Dynamicweb has required .NET 4.5.1 since 8.4.0.0, so if it works with 8.4.0.3, it should work with any later version automatically.

 
Jonas Mersholm
Reply

Hey Jeppe, the problem persists.

It still sends between 2-3 emails to the same receipient every time the scheduler runs. I tried to set up a check through a textfile, writing the current time to the textfile and validating up against it, to secure only one emailsend pr. day. Yet, that one email scheduler i set up, sends the same mail 2-3 times to the same user.

 

Do you have any idea what this coul dbe?

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

I'm not sure from your answer whether you tried to create e-mails from the module itself and not from code. I'd like to know the outcome of a standard (non-scheduled) e-mail sent from E-mail Marketing -- not code -- and a scheduled e-mail also not from code. Are these also sent multiple times?

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

I've tested the following code again on both 8.4.0.21 and 8.4.1.6, and I only get one e-mail to my recipient.

var email = new Email();

// Set standard properties
email.SenderEmail = "noreply@domain.com";
email.SenderName = "My name";
email.Subject = "My subject";
//And so on

// Setup content
email.PageId = 4;

// Setup recipients
var rp = email.RecipientProvider as AccessUserRecipientProvider;
if (rp != null)
{
	rp.ExcludedUsers = "";
	rp.SetIds(new List< string > { "u256" });
}

// Setup scheduling
email.CreateEmailScheduledTask(DateTime.Now);

I have noticed though, that if your code is invoked from a notification subscriber that is called on page load, it's also called when the page content is rendered. Therefore, when you render the content for the first e-mail, the subscriber is invoked again and a new e-mail is scheduled. Maybe this is what you're experiencing?

- Jeppe

 

You must be logged in to post in the forum