Skip to main content

Microsoft Silverlight

Answered Question How to email a form's inputs to email out to an email addressRSS Feed

(0)

donmarais
donmarais

Member

Member

37 points

82 Posts

How to email a form's inputs to email out to an email address

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.

Donavan Marais
www.2browndogs.com
donavan@2browndogs.com

sladapter
sladapter

All-Star

All-Star

17441 points

3,172 Posts

Re: How to email a form's inputs to email out to an email address

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.

 

 



sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Answered Question

Re: Re: How to email a form's inputs to email out to an email address

 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 Service
SteveWebService.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 clicked
private 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,);
            }
        }

 

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev

SteveWong
SteveWong

Contributor

Contributor

6343 points

1,281 Posts

Re: Re: Re: How to email a form's inputs to email out to an email address

 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>

 

Regards,
SteveWong (HongKong)
Please mark post as answer if they help you

Client App Dev

donmarais
donmarais

Member

Member

37 points

82 Posts

Re: Re: How to email a form's inputs to email out to an email address

Thanks to both of you for your responses.

 Steve, your code worked just fine!

Donavan Marais
www.2browndogs.com
donavan@2browndogs.com

hari1dream
hari1dream

Member

Member

2 points

2 Posts

Re: Re: Re: How to email a form's inputs to email out to an email address

its very good

thank you

harikrishna

sandeepsaxena
sandeeps...

Member

Member

5 points

3 Posts

Re: Re: How to email a form's inputs to email out to an email address

Can you please send its equivalent VB Syntax ?

 

Thanks

 

Sandeep

 

sandeepsaxena77@yahoo.com

almiro.alves
almiro.a...

Member

Member

2 points

1 Posts

Re: Re: Re: How to email a form's inputs to email out to an email address

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. 

 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities