Application Server Connectivity > Sample 2: Sending Mail

 

Sample 2: Sending Mail

This sample illustrates how to send mail by means of the versatile CFMAIL tag. It shows how valuable this tag is for the distribution of Flash Communication Server data. Before re-creating this sample, make sure that you have configured your mail server in ColdFusion. To do so, select Server Settings > Mail Server, and specify the IP address of the SMTP server you want to use.

 
About the sample

The user fills out four fields: To, From, Subject, and a message body field. When the user clicks the Send button, the Flash Player sends a sendMail command to the Flash Communication Server along with the user's input in the four fields as parameters. The Flash Communication Server then calls on the Flash Remoting service to send the mail.

 
Re-creating the sample

The doc_sendmail.fla file provides a simple user interface for sending a mail message. The client-side ActionScript requests the message be sent, but no result is returned to the client.

Before you start to re-create the sample, see Creating your working environment.

 
To create the user interface for this sample:

1

In the Flash MX authoring environment, select File > New to open a new file.

2

From the toolbox, select the Text tool and draw four text boxes.

3

For each text box, in the Property inspector (Window > Properties), select Input Text for the kind of text box, and type one of the following instance names: FromText, ToText, SubjectText, and BodyText.

4

To add the button for sending the mail, open the Components panel (Window > Components) and drag a push button onto the Stage. In the Property inspector, give it the instance name Send_btn, the label Send, and the click handler sendMail.

5

Save the file as doc_mailto.fla.

6

Create a directory named doc_mailto in your Flash Communication Server applications directory.

 
To write the client-side ActionScript for this sample:

1

Add the following debug code to trace all the status information coming back from the Flash Communication Server.

// Trace all the status information.
function traceStatus(info) {
	trace("Level: " + info.level + "   Code: " + info.code);
}

// Create onStatus prototypes for the objects that
// will return status.
NetConnection.prototype.onStatus = traceStatus;
NetStream.prototype.onStatus = traceStatus;
SharedObject.prototype.onStatus = traceStatus;

2

Create the event handler for the send_btn button. Notice the call to sendMail. This invokes the function you'll define later in the server-side code. The second parameter, null, tells the Flash Communication Server that the client doesn't expect a result to be returned.

// Send the mail. The second parameter, null, tells the
// Flash Communication Server not to return a value. The remaining
// parameters provide the data to send.
function sendMail() {
	main_nc.call("sendMail", null, ToText.text, FromText.text,
											SubjectText.text, BodyText.text);
}

3

Create a new network connection and connect to the application.

// Create a new connection and connect to the application
// instance on the Flash Communication Server.
main_nc = new NetConnection();
main_nc.connect("rtmp:/doc_mailto/room_01");

 
To write the server-side ActionScript for this sample:

1

Create a new file using your server-side ActionScript editor, and load the netservices.asc file to enable Flash Remoting through the Flash Communication Server. Remember to add a copy of this file in the doc_remoting directory.

load("netservices.asc");

2

In the onAppStart function, define the location of the Flash Remoting service, and connect to it.

application.onAppStart = function() {
	trace("***** on app start");

	// Set the default gateway URL.
	NetServices.setDefaultGatewayUrl("http://localhost:8500/flashservices/gateway");
	
	// Connect to the gateway.
	var gatewayConnection = NetServices.createGatewayConnection();
	
	// Get a reference to the service. "mail.sendMail" refers
	// to the ColdFusion file, sendMail.cfc, found in the mail
	// application directory under the deployed ColdFusion directory.
	this.mailSendService = gatewayConnection.getService("mail.sendMail",
																									this);
}

3

In the onConnect function, accept the client's connection.

application.onConnect = function (clientObj) {

	trace("***** on connect");

	// Accept the connection for this client.
	this.acceptConnection(clientObj);

	return true;
}

4

Provide a prototype function that the client can call to send the mail.

// Handle the client's call to sendMail.
Client.prototype.sendMail = function(to, from, subject, body) {
	trace("***** sending mail");
	// Call the sendMail function of the Flash Remoting instance
	// created in application.onAppStart, above.
	application.mailSendService.sendMail(to, from, subject, body);
}

5

Save the file as main.asc in the /applications/doc_mailto directory.

 
To write the ColdFusion component for this sample:

1

Using your standard editor, create a new file and add code to name the application and indicate how to access it.

<cfcomponent name="sendMail" access="remote">

2

Create the sendMail function and define each parameter.

	<cffunction name="sendMail" output="false"
						description="send mail to a client" access="remote">
		<cfargument name="to" required="true" type="string"
						description="recipient of the email">
		<cfargument name="from" required="true" type="string"
						description="sender of the email">
		<cfargument name="subject" required="true" type="string"
						description="subject heading">
		<cfargument name="body" required="true" type="string"
						description="body text of the email">

3

Call the ColdFusion method cfmail to send the message.

		<cfmail to = "#to#" from = "#from#"subject = "#subject#">#body#</cfmail> 
		<cftrace category="UDF End" inline = "True"
						text = "Email was sent" var = "MyStatus">
	</cffunction>
</cfcomponent>

4

Save the file as sendmail.cfc in the mail application directory under the directory where you publish all of your ColdFusion MX files.