locked
send a message from a list of emails in lightswitch 2013.4 HTML screen using ajax RRS feed

  • Question

  • Hi There,

    any idea how to make a button to send a message from a list of emails in lightswitch 2013.4 HTML.

    my idea is to send message to all emails in the details picker (or the drop down menu or a choice list) tat is filled by SQL query (as a data source in lightswitch)

    i have read many articles,  most of them were written in PHP or VB !

    for example:

    • http://www.devarticles.com/c/a/HTML/Sending-email-with-AJAX-building-a-small-application/1/
    • http://stackoverflow.com/questions/22442074/how-to-send-email-to-a-list-of-addresses-from-lightswitch
    • http://lightswitchhelpwebsite.com/Forum/tabid/63/aft/75/Default.aspx
    • http://maxslabyak.com/c-sharp/using-jquery-to-send-email-with-web-services/
    • http://www.paulspatterson.com/microsoft-lightswitch-sending-emails-from-the-client/
    • http://www.quercussolutions.com/blog/index.php/microsoft-lightswitch-sending-emails/
    • http://www.lightswitchspecial.com/2013/01/7-most-easiest-way-to-send-email-from.html

    as far as  i know, the HTML screens in lightswitch 2013 can be encoded with ajax only, not vb or C# any more!

    my project contains only HTML screens, no desktop screens. i know that i can do the VB programming in the tables actions, but i am looking to have this code in the Browse screen to give me more flexibility depending on my decision to send or not..

    help please :)  


    Monday, February 16, 2015 1:08 PM

Answers

  • Hello Hasan,

    I do this using a generic handler(e.g. sendmail.ashx). In the ashx file i use this code:

    Imports System.Net.Mail
    Imports System.Web
    Imports System.Web.Services
        Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    
            Dim str As String = "there was no error "
    
    
            Dim Recipient As String = Convert.ToString(context.Request.Params("pRecipient"))
            Dim strsub As String = Convert.ToString(context.Request.Params("pSubject"))
            Dim strmsg As String = Convert.ToString(context.Request.Params("pMessage"))
            Dim msg As New MailMessage
    
            Try
                ' Your mail address and display name.
                ' This what will appear on the From field.
                ' If you used another credentials to access
                ' the SMTP server, the mail message would be
                ' sent from the mail specified in the From
                ' field on behalf of the real sender.
                msg.From = New MailAddress("whererv@gmx.de", "Displayname")
                ' To addresses
                msg.To.Add("towhomitamyconcern@whereever.com")
                'msg.To.Add(New MailAddress("friend_b@example.com", "Friend B"))
                ' You can specify CC and BCC addresses also
                ' Set to high priority
                msg.Priority = MailPriority.High
                msg.Subject = strsub
                ' You can specify a plain text or HTML contents
                msg.IsBodyHtml = True
                msg.Body = strmsg
                'msg.Body = _
                '    "Hello everybody,<br /><br />" & _
                '    "I found an interesting site called <a href=""http:'JustLikeAMagic.WordPress.com"">" & _
                '    "Just Like a Magic</a>. Be sure to visit it soon."
                ' In order for the mail client to interpret message
                ' body correctly, we mark the body as HTML
                ' because we set the body to HTML contents.
                ' Attaching some data
                '        msg.Attachments.Add(New Attachment("D:\Site.lnk"))
                ' Connecting to the server and configuring it
                Dim client As New SmtpClient()
                client.Host = "mail.gmx.com"
                client.Port = 25
                client.EnableSsl = True
                ' The server requires user's credentials
                ' not the default credentials
                client.UseDefaultCredentials = False
                ' Provide your credentials
                client.Credentials = New System.Net.NetworkCredential("yourcredentias", "f2f081639ad")
                client.DeliveryMethod = SmtpDeliveryMethod.Network
                ' Use SendAsync to send the message asynchronously
                client.Send(msg)
    
    
            Catch ex As Exception
                str = ex.Message
            Finally
    
                msg.Dispose()
            End Try
    
            context.Response.ContentType = "text/plain"
            context.Response.Write(str)
    
        End Sub
    

    When you create the ASHX then replace the ProcessRequest with the above function.

    In LS HTML Screen create the function for pressing a button an appen the following text:

    $.ajax({
                type: 'post',
                data: {
                    pRecipient: Screen.yourdata,
                    pSubject: Screen.yourdata,
                    pMessage: Screen.yourdata,
    
                },
                url: '../sendmail.ashx',
                success: function success(result) {
                    alert(result);
                }
            });

    This is a way you can do that. In VB and out of your browse Screen.

    Hope this helps

    Thomas

    • Marked as answer by Angie Xu Tuesday, February 24, 2015 11:21 AM
    Monday, February 16, 2015 2:22 PM

All replies

  • Hello Hasan,

    I do this using a generic handler(e.g. sendmail.ashx). In the ashx file i use this code:

    Imports System.Net.Mail
    Imports System.Web
    Imports System.Web.Services
        Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    
            Dim str As String = "there was no error "
    
    
            Dim Recipient As String = Convert.ToString(context.Request.Params("pRecipient"))
            Dim strsub As String = Convert.ToString(context.Request.Params("pSubject"))
            Dim strmsg As String = Convert.ToString(context.Request.Params("pMessage"))
            Dim msg As New MailMessage
    
            Try
                ' Your mail address and display name.
                ' This what will appear on the From field.
                ' If you used another credentials to access
                ' the SMTP server, the mail message would be
                ' sent from the mail specified in the From
                ' field on behalf of the real sender.
                msg.From = New MailAddress("whererv@gmx.de", "Displayname")
                ' To addresses
                msg.To.Add("towhomitamyconcern@whereever.com")
                'msg.To.Add(New MailAddress("friend_b@example.com", "Friend B"))
                ' You can specify CC and BCC addresses also
                ' Set to high priority
                msg.Priority = MailPriority.High
                msg.Subject = strsub
                ' You can specify a plain text or HTML contents
                msg.IsBodyHtml = True
                msg.Body = strmsg
                'msg.Body = _
                '    "Hello everybody,<br /><br />" & _
                '    "I found an interesting site called <a href=""http:'JustLikeAMagic.WordPress.com"">" & _
                '    "Just Like a Magic</a>. Be sure to visit it soon."
                ' In order for the mail client to interpret message
                ' body correctly, we mark the body as HTML
                ' because we set the body to HTML contents.
                ' Attaching some data
                '        msg.Attachments.Add(New Attachment("D:\Site.lnk"))
                ' Connecting to the server and configuring it
                Dim client As New SmtpClient()
                client.Host = "mail.gmx.com"
                client.Port = 25
                client.EnableSsl = True
                ' The server requires user's credentials
                ' not the default credentials
                client.UseDefaultCredentials = False
                ' Provide your credentials
                client.Credentials = New System.Net.NetworkCredential("yourcredentias", "f2f081639ad")
                client.DeliveryMethod = SmtpDeliveryMethod.Network
                ' Use SendAsync to send the message asynchronously
                client.Send(msg)
    
    
            Catch ex As Exception
                str = ex.Message
            Finally
    
                msg.Dispose()
            End Try
    
            context.Response.ContentType = "text/plain"
            context.Response.Write(str)
    
        End Sub
    

    When you create the ASHX then replace the ProcessRequest with the above function.

    In LS HTML Screen create the function for pressing a button an appen the following text:

    $.ajax({
                type: 'post',
                data: {
                    pRecipient: Screen.yourdata,
                    pSubject: Screen.yourdata,
                    pMessage: Screen.yourdata,
    
                },
                url: '../sendmail.ashx',
                success: function success(result) {
                    alert(result);
                }
            });

    This is a way you can do that. In VB and out of your browse Screen.

    Hope this helps

    Thomas

    • Marked as answer by Angie Xu Tuesday, February 24, 2015 11:21 AM
    Monday, February 16, 2015 2:22 PM
  • Thomas and Michael , i appreciate your help.

    will try that :) 


    Tuesday, February 17, 2015 11:48 AM