Featured Posts

Adobe Flex - Filter XML Data using E4X and RegExp Have you ever you wanted to filter your data like you would using the SQL LIKE condition? Well with E4X and RegExp you too can reap the benefits of pattern matching! Here...

Readmore

Flex Assets/File Manager – ASP VBScript - Version... I recently built a JavaScript file manager but I wanted more control and better usability instead of expanding on the JavaScript version I decided to build it using Flex....

Readmore

Getting at your SQL Database using Flex and .NET Web ServicesGetting at your SQL Database using Flex and .NET Web... So you gave up on trying to figure out how you can directly connect Flex to your database like with any server side language and decided to Google the answer. Well by now...

Readmore

Adobe Flex - Filter XML Data using E4X and RegExp Have you ever you wanted to filter your data like you would using the SQL LIKE condition? Well with E4X and RegExp you too can reap the benefits of pattern matching! Here...

Readmore

Fitchett Rss

Adobe Flex – ASP.NET Web Service Send E-mail

Posted on : 08-04-2009 | By : Michael Fitchett | In : ASP.NET, ActionScript, Development, Flex, Web Service

5

flex-email-applicationBefore we get started download “One Headlight” by the Wallflowers. Yea its a bit old but still rocks! ;-)

Now that have some music jamming in the background lets get started.

We need an ASP.NET web service if you don’t already have one create it; if you do then great. This example code is in C# it can easily be done in VB as well.

1. Let’s add an import to the top

using System.Web.Mail;

 
1a. Put “One Headlight” on repeat.

2. Now we need to create the web service function that sends the emails

[WebMethod]
public string sendEmail(string strMailTo, string strMailFrom, string strMailSubject, string strMailBody)
{
     //create the mail message
     MailMessage mail = new MailMessage();

     //set the addresses
     mail.From = new MailAddress(strMailFrom);
     mail.To.Add(strMailTo);

     //set the content
     mail.Subject = strMailSubject;
     mail.Body = strMailBody;
     mail.IsBodyHtml = true;

     //send the message
     SmtpClient smtp = new SmtpClient("127.0.0.1");
     smtp.Send(mail);

     //return message
     return "Message Sent!";
}

 

 

3. Test it by running the web service in debug mode.

Click on the sendMail link and you should see something like this

 

Screen Shot - Web Service - sendMail Function

Screen Shot - Web Service - sendMail Function

 

 

fill it out and click Invoke. You need to make sure you have a SMTP server running on your machine. If you don’t you will get an error. I doubt you will be running it locally so you can upload it to your server and carry on to step 4.

4. Now that we have our web service function built we need to link it up in Adobe Flex. Make sure to replace “yourPassedValue” with some string value that you are passing from somewhere like a text field. If you want it to be static then remove the open and close Flex code tags “{}” and just type the string.

<mx:WebService id="yourWebServiceID" wsdl="http://www.domain.com/<b style="color:black;background-color:#ff66ff">Service</b>.asmx?WSDL" showBusyCursor="true">
       <mx:operation name="sendEmail">
           <mx:request>
                  <strMailTo>{yourPassedValue}</strMailTo>
                  <strMailFrom>{yourPassedValue}</strMailFrom>
                  <strMailSubject>{yourPassedValue}</strMailSubject>
                  <strMailBody>{yourPassedValue}</strMailBody>
           </mx:request>
      </mx:operation>
</mx:WebService>

5.  Now all that’s left is to add this to an action like a button click event

yourWebServiceID.sendEmail.send();

That’s it. I added an example below if it errors out its because the max relays have been reached. I will update it soon with a notification. I will also provide the source.

The Flash plugin is required to view this object.

http://www.fitchett.me/wp-content/plugins/sociofluid/images/digg_32.png http://www.fitchett.me/wp-content/plugins/sociofluid/images/delicious_32.png http://www.fitchett.me/wp-content/plugins/sociofluid/images/technorati_32.png http://www.fitchett.me/wp-content/plugins/sociofluid/images/google_32.png http://www.fitchett.me/wp-content/plugins/sociofluid/images/myspace_32.png http://www.fitchett.me/wp-content/plugins/sociofluid/images/facebook_32.png http://www.fitchett.me/wp-content/plugins/sociofluid/images/twitter_32.png

Comments (5)

[...] bookmarks tagged the wallflowers Adobe Flex – ASP.NET Web Service Send E-mail | Fit… saved by 9 others     playarabbit bookmarked on 04/09/09 | [...]

Thank for such a great tutorial michael..
It is really helpful..god bless you..
Now I managed to send and retrieve data from MSSQL..
But I encounter a problem and Im not sure why it happen..
I created save button with click function that will send data to MSSQL database from my flex application.But when i look to my MSSQL database, there are duplicate data at the database..Is this because of the inconsistency of flex with MSSQL or I’ve miss something here?

ryco

ps:Error
There are redundant data..
im not sure which word can clearly describe it… :???:

Hi Ryco,

If it’s posting to your database twice my best guess would be that your calling the Web Service Insert function multiple times. If would you like e-mail me your Flex code and I will take a look at it and see what’s going on or post it as a comment (make sure to rename any personal information) this will likely help people experiencing the same issue.

This is the only article on the net I could find and it works! Thank you very much!

Write a comment

Anti-Spam Protection by WP-SpamFree

Advertise Here