Ahmed
Ahmed

Reputation: 361

ASP.NET MVC Invitation sysyem

I need to support invitation in my ASP.NET MVC based site, so the members can invite friends to join.

Does any ready component exist that can do this for me instead of starting from scratch?

Upvotes: 2

Views: 2052

Answers (2)

Filip Ekberg
Filip Ekberg

Reputation: 36287

First of all, there are providers for this which interacts very nice with WebServices and other communication methods. Try searching for an Invite API that suits your needs. I would not set up an "Invite by Email"-controller as the others suggests, there are "dangers" when doing the mailing yourself.

Let's say that the page you are developing peeks and have a lot of visitors, for instance 2000 visitors at once and all of them want to invite 10 friends, now thats 20 000 invites. If you were to request 20 000 SMTP Sends from your Server, a lot of servers will blacklist you, now thats not good.

So, you need to create a little bit more advance invite process, you could store all invites in a database and use a scheduler to send 10 mails per minute, or use a third party provider which can handle large amount of invites.

Never think to small

If you want to invite by facebook, myspace, twitter or whatever you might think of, there are API's for that that are not to hard to manage.

Upvotes: 0

nathanchere
nathanchere

Reputation: 8098

I don't know of any 'plug-and-play' systems like that for ASP.NET MVC but you can really easily implement a basic one yourself anyway.

Create an InviteController like so:

using System.Net.Mail;

namespace InTouch.Controllers
{

public class YourApp.Controllers
{
    public ActionResult Index()
    {
        return View();
    }

    [AcceptVerbs("POST")]
    public ActionResult Index(string fromname, string fromemail, string toname, string toemail)
  {
  const string emailregex = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
  var result = false;
  ViewData["fromname"] = fromname;
  ViewData["fromemail"] = fromemail;
  ViewData["toname"] = toname;
  ViewData["toemail"] = toemail;

  if (string.IsNullOrEmpty(fromname)) ViewData.ModelState.AddModelError("name", "Please enter your name!");
  if (string.IsNullOrEmpty(fromemail)) ViewData.ModelState.AddModelError("email", "Please enter your e-mail!");
  if (!string.IsNullOrEmpty(fromemail) && !Regex.IsMatch(fromemail, emailregex)) ViewData.ModelState.AddModelError("email", "Please enter your e-mail!");
  if (string.IsNullOrEmpty(toname)) ViewData.ModelState.AddModelError("comments", "Please enter a message!");
  if (!string.IsNullOrEmpty(toemail) && !Regex.IsMatch(toemail, emailregex)) ViewData.ModelState.AddModelError("email", "Please enter a valid recipient e-mail!");
  if (!ViewData.ModelState.IsValid) return View();

  var message = new MailMessage(fromemail, toemail)
        {
            Subject = "You have been invited to MyNewApp by " + fromname + "!",
            Body = fromname + " wants to invite you. Click my link httpwwwblahblah to join them!"
        };

        SmtpClient smtp = new SmtpClient();
        try
        {
            smtp.Send(message);
            result = true;
        }
        catch { }           

  return View("Thankyou");
    }


}
}

Then you just need a view for the form. Something like this, styled to your taste:

<form id="invite" method="post">
<fieldset><legend>Invite a friend!</legend>
<%=Html.ValidationMessage("fromname")%>
<%=Html.ValidationMessage("fromemail")%>
<%=Html.ValidationMessage("toname")%>
<%=Html.ValidationMessage("toemail")%>
Your Name: <input type="text" id="fromname" name="fromname" class="required" value="<%= ViewData["fromname"] ?? "" %>" /><br />
Your Email: <input type="text" id="fromemail" name="fromemail" class="required" value="<%= ViewData["fromemail"] ?? "" %>" /><br />
Friend's Name: <input type="text" id="toname" name="toname" class="required" value="<%= ViewData["toname"] ?? "" %>" /><br />
Friend's Email: <input type="text" id="toemail" name="toemail" class="required" value="<%= ViewData["toemail"] ?? "" %>" /><br />
<input type="submit" id="action" name="action" value="Submit" />
</fieldset></form>

Should do the trick without complicating the rest of your app!

Upvotes: 3

Related Questions