Sending mail using flash and asp.net
Tuesday, November 24, 2009
This article decribes how to send mail from a Flash UI using asp.net. Create a UI like this in flash.

Assign an instance name of the 'Send' movie clip as'myButton'. Also name the text box as 'email'. On the first frame action, write the action script code.

var loadvar=new LoadVars(); //to send variables
var receiveLoad = new LoadVars();//to receive the response


receiveLoad.onLoad=function(success)
{
if(success)
{
trace(unescape(receiveLoad.toString()));//if received succesfully
}
else
{
trace("error");
}
}

this.myButton.onRelease=function(){ //myButton click event


if (email.text.indexOf(" ") != -1 || email.text.indexOf("@") == -1 || email.text.indexOf(".") == -1 ||email.text.length<6||email.text.lastIndexOf(".") {
res="validateion error";
return false;
} //email validation



if(email.text!=""){

loadvar.customp=escape(email.text);//send a variable customp

var url="http://YOUR SERVER NAME/SendMail.ashx?cb="+(Math.round(Math.random()*96584674));//cb-cache burst used to explicitly send new urls so that the request is not cached

loadvar.sendAndLoad(url,receiveLoad,"POST");
}
}

The LoadVars object is an alternative to the loadVariables action for transferring variables between a Flash movie and a server. It uses the methods load , send , and sendAndLoad to communicate with a server.


In Asp.NET create a generic handler with name 'SendMail.ashx' . Write the code to send email in the handler file.

<%@ WebHandler Language="C#" Class="SendMail" %>

using System;
using System.Web;
using System.Net.Mail;
using System.Text.RegularExpressions;

public class SendMail : IHttpHandler {

public void ProcessRequest (HttpContext context) {

string emailId = string.Empty;

if (context.Request["customp"] != null)
{
emailId = HttpUtility.UrlDecode(context.Request["customp"].ToString());
}

if ((emailId != "") && Regex.IsMatch(emailId, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")) //check for a valid emailid
{
SmtpClient smtpClient = new SmtpClient();
MailAddress mailFrom = new MailAddress("YOUR EMAIL ID");
MailAddress mailTo = new MailAddress(emailId);
MailMessage mailMessage = new MailMessage(mailFrom, mailTo);
mailMessage.Subject = "Test Mail";
mailMessage.Body = "This is a test mail sent using Flash 8";
mailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.Never;
smtpClient.Host = "YOUR MAIL SERVER HOST NAME";
smtpClient.Port = YOUR PORT NO;
smtpClient.Credentials = new System.Net.NetworkCredential("YOUR USERNAME", "YOUR PASSWORD");
try
{
smtpClient.Send(mailMessage);
context.Response.Write("&res=1");
}
catch (Exception exException)
{
context.Response.Write("&res="+exException.ToString());
}
}
else
{
context.Response.Write("&res=2");
}

}

public bool IsReusable {
get {
return false;
}
}

}


When executed the loadvar variable posts a request to the sendmal.ashx file with the emailid which inturn sends the mail.

Labels: , , , , , ,

 
posted by Abhijeet at 8:39 PM | Permalink | 0 comments