Monday, June 28, 2010

Send Email from Silverlight application using WCF service

Send email from Silverlight application using WCF service which is hosted on a server.

So, we have 4 main task:
1. Add Silverlight enabled WCF service into your web application project
2. Configure SMTP server
3. Write operation contract in your WCF service which will send email
4. Consume the WCF service in your Silverlight application and invoke to send email as required

Considering you have created a Silverlight application project with a web application project using Visual Studio 2010

Add Silverlight enabled WCF service:
Now create a folder called Service in your WEB application project and add new item


















Now create a class called SLEmailMessage and make it serializable so that this type can be transfer through WCF service:























Now add mail settings in your web.config's configuration section:
















Now write the email sending code in your service class (.svc.cs):
























Now in your Silverlight project add the service reference:


























In your XAML code behind invoke the added service:

























You can download the source.

13 comments:

  1. I have used all the code and at the end it just says unable to send email, as set out in the void, but I am trying to use my gmail account to send the email, which i suppose is the problem. Do you know the exact settings in web.config I could use in order to make it work?

    Thanks a lot

    ReplyDelete
  2. Hi thanks for this post.

    However while following your tutorial I was stuck at the point where you are adding a service reference. When I try to add the service reference I get an error saying

    The HTML document does not contain Web service discovery information.
    Metadata contains a reference that cannot be resolved: 'http://localhost:52878/ServiceEmail.svc'.
    Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:52878/ServiceEmail.svc. The client and service bindings may be mismatched.
    The remote server returned an error: (415) Unsupported Media Type.
    If the service is defined in the current solution, try building the solution and adding the service reference again.

    And i am not able to go forward from there..
    I have tried searching a lot but to no help.

    Any help from you is appreciated.

    Thanks & Regards
    Supreet

    ReplyDelete
  3. Its working on My machine only.. If i am hosting in IIS, mail could not sent.. What may be the problem...?

    ReplyDelete
  4. Yeah,same error, i'm working on it :(

    Mike24productions@wanadoo.fr

    ReplyDelete
  5. Thank you very much for this post and to sharing the source with us
    But unfortunately in my project does work, I tried everything ...
    In the end I Download your Source example .... but Nothing
    I cant send email ..

    Apart the Web.Config settings

    host = "smtp.gmail.com"
    port = "587"
    userName ="*********gmail.com "
    password ="********"/>

    What else should I change;
    Can you help me;

    Best Regards

    ReplyDelete
  6. Hi - Thanks for the code. Could you also show us how to add CC and BCC addresses. Also what if the TO includes multiple recipients?

    ReplyDelete
  7. @tSupreet

    rebuild you solution, then try adding the service reference again.

    ReplyDelete
  8. Hello your tuto is great but how can i catch my class mail serializable on the client ? i have no reference

    ReplyDelete
  9. for you folks using Gmail--the problem may be with Google--they don't like it when people send email automtically or via a program. This is related to their anti-SPAM defenses. I am not a Gmail user but that's what I recall reading. So try switching to another service and see if that makes your program work.

    ReplyDelete
    Replies
    1. Add to Web.Config

      enableSsl="true"

      Delete
  10. Thanks for this article. I will try this out. I wish it were easier to send emails from the client side in SL4...

    ReplyDelete
  11. This is great as far as it goes. Could you expand a bit and show how to add CC, Bcc, and Attachments? Assume the attachment is a file already saved on the server.
    Thanks.

    ReplyDelete
  12. Tahnk you.... Amyo Kabir
    It's Work!!!!!!!!!!!!!!!!!!!!!!!!!!

    ReplyDelete