Click here to Skip to main content
14,357,858 members

Send E-mail from Flash using .NET (in 5 Easy Steps)

Rate this:
5.00 (1 vote)
Please Sign up or sign in to vote.
5.00 (1 vote)
13 Apr 2010CPOL
How to send e-mail from Flash using .NET in 5 easy steps

Sending emails from Flash using .NET is really simple - all you need is a Flash Interface that sends the request to an ASPX page. Here are the things you need.

  1. And ASPX Page that accepts the request (in this sample, let's call it EmailSender.aspx)
  2. A Flash frontend for users to post their messages (in this sample, let's call it EmailForm.fla)
  3. And Action Script class to process the message (in this sample, let's call it EmailForm.as)

Step 1

Create EmailForm.fla and add the following objects with the following properties:

Name: Type = Input Text, Name = txtName
Email Address: Type = Input Text, Name =txtEmailAddress
Subject: Type = Input Text, Name =txtSubject
Message: Type = Input Text, Name =txtMessage
Error: Type = Dynamic Text, Name =txtError
Send Email Button: Type = Button, Name =btnSendEmail

Image 1

Email Form Properties

Step 2

Create an AS file, called EmailForm.as and save it in the same directory create and add these codes.

Image 2

New AS
package 
{
 import flash.display.MovieClip;
 import flash.display.SimpleButton;
 import flash.events.MouseEvent;
 import flash.utils.Timer;
 import flash.events.*;
 import flash.net.URLLoader;
 import flash.net.URLVariables;
 import flash.net.URLRequest;
 import flash.net.URLRequestMethod;
 import flash.net.URLLoaderDataFormat;
 import flash.ui.ContextMenu;
 import flash.ui.ContextMenuItem;
 import flash.net.navigateToURL;

 public class EmailForm extends MovieClip
 {
 private var oTimer:Timer;
 private var bCheckStatus:Boolean=false;

 public function EmailForm()
 {
 init();
 initListener();
 }

 private function init():void
 {

 }

 private function initListener():void
 {
 btnSendEmail.addEventListener(MouseEvent.MOUSE_DOWN,controlFields);
 }

 private function controlFields(m:MouseEvent):void
 {
 if(txtName.text!=''&&txtEmailAddress.text!=''&&txtSubject.text!=''&&txtMessage.text!='')
 {
 if(CheckEmail(txtEmailAddress.text))
 {
 bCheckStatus=true;
 sendEmail();
 }
 else
 {
 bCheckStatus=false;
 txtError.text='Email Address is Invalid';
 toggleAlert();
 }
 }
 else
 {
 bCheckStatus=false;
 txtError.text='You need to fill in required fields';
 toggleAlert();
 }
 }

 private function toggleAlert():void
 {
 oTimer=new Timer(2000,1);
 oTimer.addEventListener('timer',cancel);
 oTimer.start();
 }

 private function cancel(t:TimerEvent):void
 {
 txtError.text='';
 if(bCheckStatus)
 {
 txtName.text='';
 txtEmailAddress.text='';
 txtSubject.text='';
 txtMessage.text='';
 }
 bCheckStatus=false;
 }

 private function sendEmail():void
 {
 var oEmailFields:URLVariables=new URLVariables();
 oEmailFields.sEmail=txtEmailAddress.text;
 oEmailFields.sMessage=txtMessage.text;
 oEmailFields.sSubject=txtSubject.text;
 oEmailFields.sName=txtName.text;

 var oRequest:URLRequest=new URLRequest();

 oRequest.url='EmailSender.aspx';
 oRequest.method=URLRequestMethod.POST;
 oRequest.data=oEmailFields;
 var oLoader:URLLoader=new URLLoader();
 oLoader.dataFormat=URLLoaderDataFormat.VARIABLES;
 addListeners(oLoader);
 try
 {
 loader.load(oRequest);
 }
 catch (error:Error)
 {
 trace('Page Not Found');
 }
 }

 private function addListeners(d:IEventDispatcher):void
 {
 d.addEventListener(Event.OPEN,Initialize);
 d.addEventListener(ProgressEvent.PROGRESS,inProgress);
 d.addEventListener(Event.COMPLETE,Completed);

 d.addEventListener(HTTPStatusEvent.HTTP_STATUS,HTTPStatus);
 d.addEventListener(SecurityErrorEvent.SECURITY_ERROR,SecurityError);
 d.addEventListener(IOErrorEvent.IO_ERROR,IOError);
 }

 private function Initialize(e:Event):void
 {
 txtError.text='Sending ...';
 }

 private function inProgress(e:ProgressEvent):void
 {
 txtError.text='Sending ...';
 }

 private function Completed(e:Event):void
 {
 var loader:URLLoader=URLLoader(e.target);
 var vars:URLVariables=new URLVariables(loader.data);
 if(vars.answer=='ok')
 txtError.text='Message Sent';
 else
 txtError.text='Error (Message not Sent)!!!';
 toggleAlert();
 }

 private function SecurityError(e:SecurityErrorEvent):void
 {
 txtError.text='Error (Security)!!!';
 }

 private function HTTPStatus(e:HTTPStatusEvent):void {}

 private function IOError(e:IOErrorEvent):void
 {
 txtError.text='Error (IO)!!!';
 }
 public function CheckEmail(s:String):Boolean
 {
 var x:Number;
 var y:Number=0;
 var boolTest:Boolean=new Boolean();
 for(var i:Number=0;i<s.length;i++)
 {
 if(s.charAt(i)==' ')
 {
 boolTest=false;
 break;
 }
 if(s.charAt(i)=='@')
 {
 x=i;
 y++;
 }
 else if(i>x&&s.charAt(i)=='.'&&s.charAt(s.length-1)!='.')
 {
 y++
 boolTest=true
 break;
 }
 else
 {
 if(i==s.length-1)
 {
 boolTest=false;
 break;
 }
 }
 }
 return boolTest;
 }

 }
}

Step 3

Use that class on your EmailForm.fla:

Image 3

Declare Class

Step 4

Create the EmailSender.aspx file and copy these codes:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
 if (Request.Form["sEmail"] != null)
 {
 string sPOSTEmail = Request.Form["sEmail"].ToString();
 string sPOSTMessage = Request.Form["sMessage"].ToString();
 string sPOSTSubject = Request.Form["sSubject"].ToString();
 string sPOSTName = Request.Form["sName"].ToString();

 MailMessage oMailMessage = new MailMessage();
 SmtpClient oSMTPClient = new SmtpClient();

 MailAddress oFromAddress = new MailAddress(sPOSTEmail, sPOSTName);

 string sEmailUser = "XXXXXXXXXXXXXX";
 string sEmailPassword = "XXXXXXXXXXXXXXXXX";
 string sEmailServer = "XXXXXXXXXXXXXXXXX";
 int iEmailServerSMTPPort = 25;

 oSMTPClient.Host = sEmailServer;
 oSMTPClient.Port = iEmailServerSMTPPort;

 System.Net.NetworkCredential oCredentials = 
                  new System.Net.NetworkCredential(sEmailUser, sEmailPassword);
 oSMTPClient.Credentials = oCredentials;

 oMailMessage.From = oFromAddress;
 oMailMessage.To.Add("XXXXXXXXXXXXXXXXXXXXXXX");
 oMailMessage.Subject = sPOSTSubject;

 oMailMessage.IsBodyHtml = true;
 oMailMessage.Body = sPOSTMessage;

 oSMTPClient.Send(oMailMessage);
 }
 }
}

Step 5

You're ready to rock and roll, you can publish your site now.

Image 4 Image 5 Image 6 Image 7 Image 8 Image 9

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Raymund Macaalay
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/

Comments and Discussions

 
GeneralLooks good... Pin
Sandeep Mewara13-Apr-10 9:40
mentorSandeep Mewara13-Apr-10 9:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Technical Blog
Posted 13 Apr 2010

Tagged as

Stats

11.5K views