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
Before 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
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.











[...] 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!