如何返回"成功保存"按下"保存"消息框和/或发送收据电子邮件在Lightswitch HTML屏幕中?

时间:2017-03-01 00:12:02

标签: html visual-studio-lightswitch

我正在使用Lightswitch HTML"创建新数据"面向互联网的屏幕,供人们将工作申请数据提交到数据库中。当他们按下" Save"按钮数据保存,但申请人无法判断他们的数据是否已保存。数据输入屏幕就消失了。有没有办法通过弹出消息框,发送电子邮件或两者同时通知申请人他们的申请被收到?

1 个答案:

答案 0 :(得分:2)

是的,一旦将数据插入系统,就可以发送电子邮件。要做的第一件事就是创建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文件

在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

并添加此代码(确保添加引用文件并设置&#34;复制本地&#34;为True:

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 < / p>

现在添加以下代码:

 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