How do I return a message saying “saved successfully” and / or send an email when I receive it after clicking “Save” on the HTML Lightswitch screen?

I use the Lightswitch’s “Create New Data” HTML screen facing the Internet so that people can send application data to the database. When they click the "Save" button, the data is saved, but the applicant cannot tell if their data has been saved. The data entry screen just goes away. Is there a way to notify the applicant that their application has been received by opening a message box, sending an email, or both?

+4
source share
1 answer

, , . , , 2 ( SQL-, LightSwitch )

1 - ,

CREATE TABLE Setting (SettingID INT IDENTITY (1,1) NOT NULL,
SettingName VARCHAR(11),
SettingValue VARCHAR(5))

ALTER TABLE Setting ADD CONSTRAINT SettingID_PK PRIMARY KEY (SettingID)

CREATE TABLE Message (MessageID INT IDENTITY (1,1) NOT NULL,
NameFrom VARCHAR (100), --EMAIL SERVER NAME
EmailFrom VARCHAR (100), --EMAIL A
NameTo VARCHAR (100), --SUBJECT
EmailTo VARCHAR (100), -- EMAIL B
EmailMessage VARCHAR (1500), --MESSAGE
EmailCreated DATETIMEOFFSET)

ALTER TABLE Message ADD CONSTRAINT MessageID_PK PRIMARY KEY (MessageID)

--INSERT THIS INTO SETTINGS TO ALLOW THE SYSTEM TO SEND EMAILS
INSERT INTO SETTING VALUES ('SendEmails', 'true')

2 - Web.config node

Web.config ( 25-30) :

<add key="ApplicationCulture" value="en-US" />
<add key="Microsoft.LightSwitch.DefaultClientName" value="HTMLClient" />

:

<add key="SMTPSendingName" value="MAIL HEADER" />
<add key="SMTPSendingEmailAddress" value="EMAIL TO SEND CONFIRMATION FROM" />
<add key="SMTPServer" value="mail.YOURSERVER.com" />
<add key="SMTPUserID" value="" />
<add key="SMTPPassword" value="" />
<add key="SMTPPort" value="25" />
<add key="SMTPSSL" value="false" />

3 - MailHelper.cs ( ):  enter image description here

(, " " "":

using System.Net;
using System.Net.Mail;
using System.Configuration;
using System;

namespace LightSwitchApplication.DataSources.ProjectHandlerThreeData
{
    internal class MailHelper
    {

        public SmtpClient objSmtpClient { get; set; }
        private string _SMTPSendingEmailAddress { get; set; }
        private string _SMTPServer { get; set; }
        private string _SMTPUserId { get; set; }
        private string _SMTPPassword { get; set; }
        private int _SMTPPort { get; set; }
        private bool _SMTPSSL { get; set; }

        private string _MailFromName { get; set; }
        private string _MailToEmail { get; set; }
        private string _MailToName { get; set; }
        private string _MailSubject { get; set; }
        private string _MailBody { get; set; }

        public MailHelper(
            string SendFromName, string SendToEmail,
            string SendToName, string Subject,
            string Body) 
{
            _MailFromName = SendFromName;
            _MailToEmail = SendToEmail;
            _MailToName = SendToName;
            _MailSubject = Subject;
            _MailBody = Body;


            _SMTPSendingEmailAddress = Convert.ToString(ConfigurationManager.AppSettings["SMTPSendingEmailAddress"]);
            _SMTPServer = Convert.ToString(ConfigurationManager.AppSettings["SMTPServer"]);
            _SMTPUserId = Convert.ToString(ConfigurationManager.AppSettings["SMTPUserID"]);
            _SMTPPassword = Convert.ToString(ConfigurationManager.AppSettings["SMTPPassword"]);
            _SMTPPort = Convert.ToInt32(ConfigurationManager.AppSettings["SMTPPort"]);
            _SMTPSSL = Convert.ToBoolean(ConfigurationManager.AppSettings["SMTPSSL"]);

            objSmtpClient = new SmtpClient(_SMTPServer, _SMTPPort);
        }

        public void SendMail()
        {
            MailMessage mail = new MailMessage();

            System.Net.Mail.MailAddress mailFrom =
                new System.Net.Mail.MailAddress(_SMTPSendingEmailAddress, _MailFromName);

            System.Net.Mail.MailAddress mailTo =
                new System.Net.Mail.MailAddress(_MailToEmail, _MailToName);


            var _with1 = mail;
            _with1.From = mailFrom;
            _with1.To.Add(mailTo);
            _with1.Subject = _MailSubject;
            _with1.Body = _MailBody;


            objSmtpClient.EnableSsl = _SMTPSSL;

            objSmtpClient.Credentials =
                new NetworkCredential(_SMTPUserId, _SMTPPassword);

            objSmtpClient.SendAsync(mail, mail.To);
        }

     }
}

4 - ( "", " " "Messages_Inserted"

:

 try
            {
                var EmailSetting = Settings.Where(x => x.SettingName == "SendEmails").FirstOrDefault();
                if (EmailSetting != null)
                {
                    if (EmailSetting.SettingValue.ToLower() == "true")
                    {

                        string strSubject = "MAIL HEADER";
                        string strMessage = String.Format("{0}",
                            "TEXT AT TOP OF EMAIL IF REQUIRED")
                                            + Environment.NewLine + Environment.NewLine;
                        strMessage = strMessage + String.Format("{0}",
                            entity.EmailMessage) + Environment.NewLine;

                        // Create the MailHelper class created in the Server project.
                        MailHelper mailHelper =
                            new MailHelper(
                                entity.NameFrom,
                                entity.EmailTo,
                                entity.NameTo,
                                strSubject,
                                strMessage);

                        // Send Email
                        mailHelper.SendMail();

                    }
                    else
                    {
                    }
                }
                else
                {
                }
            }
            catch (Exception ex)
            {
            }

, :

using System.Configuration;
using System.Net.Mail;
using LightSwitchApplication.DataSources.YOURDATASOURCE;

, INSERTED ,

//email a save receipt to the person who added the data
            Message objMessage = new Message();
            objMessage.NameFrom = Convert.ToString(ConfigurationManager.AppSettings["SMTPSendingName"]);
            objMessage.EmailFrom = Convert.ToString(ConfigurationManager.AppSettings["SMTPSendingEmailAddress"]);
            objMessage.NameTo = "Details Successfully Saved"; //SUBJECT
            objMessage.EmailTo = "emailto@email.com"; 
            objMessage.EmailMessage =
                string.Format("The Following User has successfully been Added: " +
                              "\nErrorID: " + entity.ErrorID +
                              "\nBy User: " + entity.StaffTable.Staffname +
                              "\nDate Reported:" + DateTime.Now.ToString(" dd.MM.yy") +
                              "\n\nError Details: " + entity.Message);

, , . , :) - , , , .

, , JavaScript:

. CTRL + S, . ( ):

myapp.AddEditScreen.created = function (screen) {
  $(window).one("pagechange", function (e, data) {
        var $page = $("#" + screen.details._pageId);
        var $button = $page.find(".msls-save-button");
        $button.removeClass("msls-save-button");
    });
};

-, ( PostRender). , , , .

$("[data-ls-tap='tap:{data.shell.discardCommand.command}']").hide();
$("[data-ls-tap='tap:{data.shell.saveCommand.command}']").hide();
$("[data-ls-tap='tap:{data.shell.okCommand.command}']").hide();
$("[data-ls-tap='tap:{data.shell.cancelCommand.command}']").hide();

( ): http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/2224/Sending-Asynchronous-Emails-Using-LightSwitch-HTML-Client.aspx

+2

Source: https://habr.com/ru/post/1671088/


All Articles