Skip to main content
Home Forums Silverlight Programming Programming with .NET - General How to email a form's inputs to email out to an email address
7 replies. Latest Post by almiro.alves on July 3, 2009.
(0)
donmarais
Member
37 points
82 Posts
09-23-2008 4:43 PM |
Being a nubie to SL, and to .net in general, while I am mostly excited about how SL will impact the web, one thing that I'm confused about is the ability to create a form in my app, such as a "Contact Us Form", with simple textblocks for user's to add their info, such as email address, first name, last name, etc. Finally upon completion, the user clicks "send" and the strings are validated, concatenated and emailed to my email address. Voila! While not extremely complex, and from the various blogs I have read, not the most practical way to maniplulate data, it is the most simplest method. While simple enough to do in asp.net using System.Net.Mail, I have had no luck in finding a way to do this in SL. I found a blog that went into a quick and dirty trick to add a web service, but without validation and little regard for security.
So after many hours of research through the forums of SL and the great wide web, I have come to the conclusion that the only way around is to create a seperate aspx page, which by the way will not reflect the RICH layout and use of SL controls, and use standard ASP.NET methods. Please could someone tell me if I am right, since I think it defeats the goal of creating RIA's and not being able to at least add a button event to email data to an email address.
But, after all, I am just a newbie and still pretty clueless, albeit excited about learning about SL. At the moment I am putting all my eggs in one basket to learn this new development platform, and I am hoping that SL will be my saving grace.
sladapter
All-Star
17441 points
3,172 Posts
09-23-2008 10:36 PM |
For this simple email application in Silverlight, you have to use WebService. Silverlight dose not support System.Mail object. Because that System.Mail object need either a SMTP server running on the same machine or you need send the message to a network Email server directly. Because Silverlgiht is running on user's browser, it won't have access to those resources. Just like you write a traditional email page, the email sending code has to be running on Server, not on client machine.
For sending data back to server, You need to use WebService. If you learned how to write a WCF service and using WebService to do communication to your server, you will find it's very easy to do what you want.
SteveWong
Contributor
6343 points
1,281 Posts
09-23-2008 11:08 PM |
As sladapter said you requried SMTP Service from the Mail Service.
Here I have an example for you that I have created.
In your Service, you should include this
public bool SendMail(string fromAddress, string toAddress, string subject, string body) { try { MailMessage msg = new MailMessage(); msg.From = new MailAddress(fromAddress); msg.To.Add(new MailAddress(toAddress)); msg.Subject = subject; msg.Body = body; msg.IsBodyHtml = false; msg.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.EnableSsl = true; smtp.Send(msg); return true; } catch (Exception ex) { ///Log.Write(ex) return false; } }
Remember to add these two lines:
using System.Net;using System.Net.Mail;
Then, in your sillverlight you should have to have this:
//Add the ServiceSteveWebService.ServiceClient mydata = new SteveSLWeb.SteveWebService.ServiceClient();void Page_Loaded(object sender, RoutedEventArgs e) { mydata.SendMailCompleted += new EventHandler<SteveSLWeb.SteveWebService.SendMailCompletedEventArgs>(mydata_SendMailCompleted); }//Send the Msg when Send Button is clickedprivate void Btn_Send_Click(object sender, RoutedEventArgs e) { if (!DataValid()) { //prompt msg to ask user to fill in all boxes } else { mydata.SendMailAsync(Tb_SenderAdd.Text, Tb_ReceiverAdd.Text, Tb_Subject.Text, MailContent.Text,); } }
09-23-2008 11:13 PM |
Opps! Sorry one thing to add
In order to send the mail successfully, you should pre-set a mail address(sender)
Add the following lines between </system.serviceModel> and </configuration>
<system.net> <mailSettings> <smtp> <network host="smtp.gmail.com" port="587" userName="youremail@gmail.com" password="yourpassword"/> </smtp> </mailSettings> </system.net>
09-23-2008 11:15 PM |
Thanks to both of you for your responses.
Steve, your code worked just fine!
hari1dream
2 points
2 Posts
01-05-2009 2:49 AM |
its very good
thank you
sandeeps...
5 points
3 Posts
02-26-2009 7:46 AM |
Can you please send its equivalent VB Syntax ?
Thanks
Sandeep
sandeepsaxena77@yahoo.com
almiro.a...
1 Posts
07-03-2009 10:49 PM |
Hi Steve,
I tried make this, so ocorrurs the following errors:
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Configuration.ConfigurationErrorsException: Insufficient permissions for setting the configuration property 'port'. (D:\Hosting\3457677\html\web.config line 76) at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult) at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSection(String configKey, Boolean getLkg, Boolean checkPermission) at System.Configuration.BaseConfigurationRecord.GetSection(String configKey) at System.Web.HttpContext.GetSection(String sectionName) at System.Web.Configuration.HttpConfigurationSystem.GetSection(String sectionName) at System.Web.Configuration.HttpConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String configKey) at System.Configuration.ConfigurationManager.GetSection(String sectionName) at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName) at System.Net.Configuration.SmtpSectionInternal.GetSection() at System.Net.Mail.SmtpClient.get_MailConfiguration() at System.Net.Mail.MailMessage..ctor() at ServiceEmail.Email.Enviar() in D:\Documents\Visual Studio 2008\WebSites\rodrigomattosepraiano\ServiceEmail\Email.asmx.cs:line 18 --- End of inner exception stack trace ---
Any ideas above this problem?
Tks for help.
Note: Excuse me for my english.