Code Part 2
Code:
//Panelists vs. Recipients
//Conceptually, a Panelist is a potential Recipient, but does not become a Recipient
// until and invitation has been sent.
//We can check the list of recipients by calling the GetRecipients(...) method of the
// invitation with a RecipientFilter parameter. In all cases except "Pending" only
// recipients that the software has attempted to send an invitation to will be returned.
//
// The RecipientFilter suports 6 values:
// All -- Get all recipients that have not been deleted and have been
// sent an invitation.
// Current -- Get all recipients thave have not been deleted and have not
// opted out of the invitation.
// NotResponded -- Get all recipients that have not completed the survey.
// OptOut -- Get all recipients that have clicked the opt-out link in the
// invitation email.
// Pending -- Unlike the other filters, when Pending is specified only recipients
// that the software has NOT attempted to send an email to will
// be returned.
// Responded -- Get all recipients that have responded to the invitation.
//Get the list of pending recipients, since these are the people we want to invite. To send
// a reminder message, we could use the list of "NotResponded" recipients.
ReadOnlyCollection<Recipient> recipients = invitation.GetRecipients(RecipientFilter.Pending);
//Now let's get to work sending the invitation.
foreach (Recipient recipient in recipients)
{
//Copy the template, which will be "personalized" by the recipient
InvitationTemplate templateCopy = invitation.Template.Copy();
//Personalize the template, this includes adding the invitation id, any
// user profile properties, etc. Fields in the invitation that can be
// customized are the body and the subject. If the recipient is
// a registered user, you can make the body or subject contain the value
// of a profile field by adding a @@[FIELD_NAME] token to the message or subject.
// For example, to add the user's name and password, you could put something like
// this in the message:
//
// User Name: @@UserName
// Password: @@Password
//
recipient.PersonalizeTemplate(invitation, templateCopy);
//Create and send a mail message.
EmailMessage msg = new EmailMessage();
msg.To = recipient.EmailToAddress;
msg.From = templateCopy.FromName + "<" + templateCopy.FromAddress + ">";
msg.Body = templateCopy.Body;
msg.Subject = templateCopy.Subject;
//Send the message, which exception catching so the errors can be reported.
try
{
//If successful, mark the invitation as successfully sent to this
// recipient.
EmailGateway.Send(msg);
recipient.SuccessfullySent = true;
}
catch (Exception ex)
{
//If successful, mark the invitation as not successfully sent to this
// recipient and record the error, which will appear in the invitation
// management screens in the application.
recipient.SuccessfullySent = false;
recipient.Error = ex.Message;
}
//Store the date/time the attempt was made.
recipient.LastSent = DateTime.Now;
//Save the recipient so the success status, error message, and
// sending time are saved.
recipient.Save();
}
}
}