locked
Is this possible? Send email from LS based on query RRS feed

  • Question

  • I'v waded though Paul Pattersons (and Beths) tutorial on sending email from LS, and I "think" it's working (our service SMTP server is down at the moment).

    Anyway, is it possible to extend those examples to fire the EmailHelper while looping through the results of a screen query which contain an email address column? E.g. I query all employees that are in a certain division, review the results returned to the list, then "Push a button" and an email is sent to all email addresses from the list. Using VB with no RIA service in this app. 

    I don't know if this makes this easier, but our service SMTP server is port 25, anon authentication. 

    Saturday, March 8, 2014 8:20 PM

Answers

  • Hi,

    I'm using System.Net.mail and not a helper but I think this will work though.

    Dim sendarray = from detail in Yourquery
    where detail.something = something
    Select detail
    
    For Each d In sendarray
    Dim mailHelper As New EMailHelper(entity.SenderEmailAddress, _
    entity.RecipientName, _                                           
    entity.RecipientEmailAddress, _
    entity.RecipientName, _
    sSubject, _
    sMessage)
                                                
    next
    
    mailHelper.SendMail()

    Good luck

    Sven


    Sven Elm

    • Marked as answer by tpcolson Sunday, March 16, 2014 11:19 PM
    Monday, March 10, 2014 3:47 PM
  • Maby you cannot use the emailhelper inside an array. Ignore the emailhelper and use system.net.mail instead. I can post the code i'm using later today.

    Sven Elm

    • Marked as answer by tpcolson Sunday, July 13, 2014 4:14 PM
    Sunday, March 16, 2014 6:42 AM

All replies

  • Yes. I use a related sentemails table. And fire the send function on inserting or inserted. Then use a query with foreach. Good luck

    Sven Elm

    Sunday, March 9, 2014 9:39 PM
  • Sven, sounds great. Any chance you have an example one can follow? Thanks!
    Monday, March 10, 2014 10:48 AM
  • Hi,

    Post your setup with query and I post an example.

    Sven


    Sven Elm

    Monday, March 10, 2014 11:54 AM
  • Here is the code that I have working so far. As you can see, I've gotten as far as getting the example email working, which when the SMTP server came up today, worked great. 

            Private Sub ProxyEmails_Inserted(entity As ProxyEmail)
                Dim sSubject = "Test Email."
                Dim carRtn = Environment.NewLine & Environment.NewLine
    
                Dim sMessage = "The following email has come from a button on LightSwitch..." & carRtn
                sMessage += "Testing 1, 2, 3!!"
                Dim mailHelper As New EMailHelper(entity.SenderEmailAddress, _
                                                 entity.RecipientName, _
                                                 entity.RecipientEmailAddress, _
                                                 entity.RecipientName, _
                                                 sSubject, _
                                                 sMessage)
                mailHelper.SendMail()
            End Sub       
    
     Private Sub QRYParameters_PreprocessQuery(Year As System.Nullable(Of Integer), MaxRank As System.Nullable(Of Integer), ByRef query As System.Linq.IQueryable(Of LightSwitchApplication.Employee))
                If MaxRank.HasValue Then
                    query = From q In query
                            Where q.SARCertifications.Rank <= MaxRank
                                                Select q
                Else
                    query = From q In query
                            Select q
                End If
                If Year.HasValue Then
                    Dim lastYear = DateAndTime.Now.AddYears(-Year.ToString)
                    query = From q In query
                            Where q.DateFit > lastYear
                     Select q
                Else
                    Dim lastYear = DateAndTime.Now.AddYears(-1)
                    query = From q In query
                            Where q.DateFit > lastYear
                     Select q
                End If
            End Sub


    The rest of the code deals with some custom query parameters on the employee table, as depicted here

    The query is working great (Finally), we're able to filter a list of employees based on any permutation of criteria... we'd just like to be able to send an email to ALL employees in the filtered list based on one of the email columns (all email addresses that an employee has is icing on the cake). Screenshot below shows a sample query, and the output list showing employee records with email addresses. Entire (messy) project is on Git.



    • Edited by tpcolson Monday, March 10, 2014 1:54 PM
    Monday, March 10, 2014 1:46 PM
  • Hi,

    I'm using System.Net.mail and not a helper but I think this will work though.

    Dim sendarray = from detail in Yourquery
    where detail.something = something
    Select detail
    
    For Each d In sendarray
    Dim mailHelper As New EMailHelper(entity.SenderEmailAddress, _
    entity.RecipientName, _                                           
    entity.RecipientEmailAddress, _
    entity.RecipientName, _
    sSubject, _
    sMessage)
                                                
    next
    
    mailHelper.SendMail()

    Good luck

    Sven


    Sven Elm

    • Marked as answer by tpcolson Sunday, March 16, 2014 11:19 PM
    Monday, March 10, 2014 3:47 PM
  • I'm sort-of getting it. The part about using an array, based on the query, make sense, but I'm having a hard time putting the pieces together. "IRT" is the grid? detail? which is the list of employees which is displayed based on a run-time query (not a query attached to a table, as the employees search is based on picklists sourced from several related tables). 

           Private Sub EmailBlast_Execute()
                Dim sendarray = From detail In IRT
                                            Where detail.PersonalSMS = detail.PersonalSMS
                                            Select detail
                For Each d In sendarray
                    Dim newEmail As New EmailHelper(
                        entity.SenderEmailAddress,
                        entity.RecipientEmailAddress,
                        entity.RecipientName,
                        entity.SenderEmailAddress,
                        entity.SenderName,
                        sMessage)
                Next
    
                EmailHelper.SendMail()
            End Sub
    I'm getting email helper not declared? 

    Sunday, March 16, 2014 12:40 AM
  • If this makes this any simpler, we're not including a subject, or file attachments. The ultimate goal here is to be able to send 164 character emergency messages to a select list of email addresses in text format. 
    Sunday, March 16, 2014 12:44 AM
  • Maby you cannot use the emailhelper inside an array. Ignore the emailhelper and use system.net.mail instead. I can post the code i'm using later today.

    Sven Elm

    • Marked as answer by tpcolson Sunday, July 13, 2014 4:14 PM
    Sunday, March 16, 2014 6:42 AM
  • Here's what I have working so far: 

    Server-side: EmailHelper.vb

    Imports System.Net
    Imports System.Net.Mail
    
    Namespace LightSwitchApplication
        Public Class EMailHelper
    
            Private Property SMTPServer As String = My.Settings.SMTPServer
            Private Property SMTPPort As Integer = My.Settings.SMTPPort
    
            Private Property MailFrom As String
            Private Property MailFromName As String
            Private Property MailTo As String
            Private Property MailToName As String
            Private Property MailSubject As String
            Private Property MailBody As String
    
            Sub New(ByVal SendFrom As String,
                    ByVal SendFromName As String, _
                    ByVal SendTo As String, _
                    ByVal SendToName As String, _
                    ByVal Subject As String, _
                    ByVal Body As String)
                _MailFrom = SendFrom
                _MailFromName = SendFromName
                _MailTo = SendTo
                _MailToName = SendToName
                _MailSubject = Subject
                _MailBody = Body
            End Sub
    
            Public Sub SendMail()
    
    
                Dim mail As New MailMessage
                Dim mailFrom As New Mail.MailAddress(_MailFrom, _MailFromName)
                Dim mailTo As New Mail.MailAddress(_MailTo, _MailToName)
    
                With mail
                    .From = mailFrom
                    .To.Add(mailTo)
                    .Subject = _MailSubject
                    .Body = _MailBody
                End With
    
                Dim smtp As New SmtpClient(_SMTPServer, _SMTPPort)
                smtp.EnableSsl = False
    
    
                smtp.Send(mail)
            End Sub
    
        End Class
    End Namespace

    ApplicationDataServices.vb:

         Private Sub ProxyEmails_Inserted(entity As ProxyEmail)
                Dim sSubject = "Test Email."
                Dim carRtn = Environment.NewLine & Environment.NewLine
    
                Dim sMessage = "The following email has come from a button on LightSwitch..." & carRtn
                sMessage += "Testing 1, 2, 3!!"
                Dim mailHelper As New EMailHelper(entity.SenderEmailAddress, _
                                                 entity.RecipientName, _
                                                 entity.RecipientEmailAddress, _
                                                 entity.RecipientName, _
                                                 sSubject, _
                                                 sMessage)
                mailHelper.SendMail()
            End Sub

    And client-side, this code is attached to a button in a screen: 

        Public Class SendAnEmail
    
            Private Sub SendMyEmailOnCommand_Execute()
                Dim newEmail = DataWorkspace.ApplicationData.ProxyEmails.AddNew()
                With newEmail
                    .RecipientEmailAddress = "bigfoot@sasquatch.com"
                    .RecipientName = "Big Foot"
                    .SenderEmailAddress = "bigfoot@sasquatch.com"
                    .SenderName = "Big Foot"
                End With
    
                DataWorkspace.ApplicationData.SaveChanges()
                newEmail.Delete()
                DataWorkspace.ApplicationData.SaveChanges()
            End Sub
        End Class

    And finally, the email server settings stored as strings: 

    I'm wondering if this is a simple as replacing one constant with a pointer to the array from "IRT"? But forgive me, I'm not enough of a coder to figure out where. 

    I'm trying to follow one of your previous forum replies, which I'm sure comes close to what I'm trying to do, but I'm lacking context of where to put the various pieces together. Thanks!

    Sunday, March 16, 2014 4:29 PM
  • Sven your answer here, and from  your previous forum replies eventually got me straightened out. Here's what I finally cobbled together:

    In the employee search screen, added a button and hung the following code on it: 

            Private Sub EmailBlast_Execute()
                Dim sendarray = From detail In IRT
                                Where detail.Id = detail.Id
                                Select detail
    
                For Each d In sendarray
                    Dim newEmail = DataWorkspace.ApplicationData.ProxyEmails.AddNew()
    
                    With newEmail
                        .RecipientEmailAddress = d.WorksSMS
                        .RecipientName = d.LastName
                        .SenderEmailAddress = "big.foot@sasquatch.com"
                        .SenderName = "Big Foot"
                        .Message = Type + " " + Location + " " + "Priority: " + Priority + " Staging at " + StagingArea + ". Call (123)456 7890."
                    End With
    
    
                    DataWorkspace.ApplicationData.SaveChanges()
                    newEmail.Delete()
                    DataWorkspace.ApplicationData.SaveChanges()
                Next
    
    
    
    
            End Sub

    In the above,  "message" is sourced from several local(string) properties that are added to the screen, some have a choice list, some are free text, that allow a user to customize the message. The following screen shot shows the logic: A user has created a very specific list of requirements then made some entries in the top of the screen (local properties). (See  big picture!)

    Pressing the button results in 

    Needs some more clean up but I'm glad, with the help of the MSDN Forums, that I was able to figure out the nuts and bolts of sending an email to an array!

    Thanks all!

    Sunday, March 16, 2014 11:30 PM
  • Happy to help you

    Sven Elm

    Monday, March 17, 2014 3:59 AM