This Forum is ARCHIVE only
For LightSwitch questions,
please use the official forums at:

http://social.msdn.microsoft.com/forums/en-US/lightswitch/threads


Sending Emails In A Web Application
Last Post 26 Apr 2013 01:41 AM by admin. 12 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
Michael WashingtonUser is Offline
Send Private Message
Posts:1313
Avatar

--
06 Jun 2011 10:28 PM


    Switch to File View




    Add references to System.configuration and System.Messaging.



    Add a class file to the Server Project with the following code:

    // Adapted from:
    // http://www.paulspatterson.com/technology/lightswitch/
    // microsoft-lightswitch-sending-emails-from-the-client/#more-2896
    // ** Also, add a reference to System.Messaging.dll
     
    using System.Net;
    using System.Net.Mail;
    using System.Configuration;
    using System;
     
    namespace LightSwitchApplication
    {
        public class MailHelper
        {
            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"]);
            }
     
            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;
     
                SmtpClient smtp = new SmtpClient(_SMTPServer, _SMTPPort);
                smtp.EnableSsl = _SMTPSSL;
     
                smtp.Credentials =
                    new NetworkCredential(_SMTPUserId, _SMTPPassword);
     
                smtp.Send(mail);
            }
        }
    }



    Open the Web.config file in the ServerGenerated project and add these keys:



    Then, in one of the "server level events" of any of the Entities (Tables), such as the "_Inserted" method, you can use code such as this to send an email:

    #region Messages_Inserted
    partial void Messages_Inserted(Message entity)
    {
        string strSubject = "Message From ThingsForSale";
     
        string strMessage = String.Format("{0}", "This is a message From the Things For Sale program.")
            + Environment.NewLine + Environment.NewLine;
     
        strMessage = strMessage + String.Format("Regarding the post '{0}'", entity.Post.Description)
            + Environment.NewLine;
     
        strMessage = strMessage + String.Format("This message was sent by {0}", entity.NameFrom)
            + Environment.NewLine + Environment.NewLine;
     
        strMessage = strMessage + String.Format("Message: {0}", entity.MessageText) + 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();
    }
    #endregion





    -----------
    Michael Washington
    http://LightSwitchHelpWebsite.com
    SupreetUser is Offline
    New Member
    New Member
    Send Private Message
    Posts:2
    Avatar

    --
    10 Aug 2011 01:24 AM
    Hi Michael,
    Thanks for the post.
    I am getting an assembly reference while using LightSwitchApplication.Properties
    and it is not showing option for "Properties".
    Please help...
    Thanks,
    Supreet
    Michael WashingtonUser is Offline
    Send Private Message
    Posts:1313
    Avatar

    --
    10 Aug 2011 01:26 AM
    Download the code for this article:
    http://www.codeproject.com/KB/silve...rSale.aspx

    That is a working example.
    -----------
    Michael Washington
    http://LightSwitchHelpWebsite.com
    yoav miloUser is Offline
    New Member
    New Member
    Send Private Message
    Posts:1
    Avatar

    --
    06 Mar 2012 11:33 PM
    Hi Michael,
    I am new to LS and wanted to know if i can use this code on a desktop deployment app...
    And if not where can i find C# code sample that works...
    Thanks,
    Yoav
    Michael WashingtonUser is Offline
    Send Private Message
    Posts:1313
    Avatar

    --
    07 Mar 2012 12:30 AM
    I plan to post a blog on this next week.
    -----------
    Michael Washington
    http://LightSwitchHelpWebsite.com
    wtwashburnUser is Offline
    New Member
    New Member
    Send Private Message
    Posts:1
    Avatar

    --
    18 Mar 2012 06:28 PM
    Hi Michael,
    Thanks for this post and many others. I used this as the model for my own app. I was wondering if there is any reason I could not or should not put the SMTP settings in a table rather than the web config file?
    Thanks,
    Bill
    Michael WashingtonUser is Offline
    Send Private Message
    Posts:1313
    Avatar

    --
    18 Mar 2012 10:22 PM
    Posted By wtwashburn on 19 Mar 2012 02:28 AM
    Hi Michael,
    Thanks for this post and many others. I used this as the model for my own app. I was wondering if there is any reason I could not or should not put the SMTP settings in a table rather than the web config file?
    Thanks,
    Bill

    There should be no problem.
    -----------
    Michael Washington
    http://LightSwitchHelpWebsite.com
    sridharUser is Offline
    New Member
    New Member
    Send Private Message
    Posts:2
    Avatar

    --
    12 Apr 2012 05:36 PM
    Hi Michael

    Can you tell me How to send mails with attachments in LS?
    Michael WashingtonUser is Offline
    Send Private Message
    Posts:1313
    Avatar

    --
    12 Apr 2012 09:22 PM
    Posted By sridhar on 13 Apr 2012 01:36 AM
    Hi Michael

    Can you tell me How to send mails with attachments in LS?


    I have no examples, sorry.
    -----------
    Michael Washington
    http://LightSwitchHelpWebsite.com
    NESTicleUser is Offline
    New Member
    New Member
    Send Private Message
    Posts:9
    Avatar

    --
    26 Apr 2013 01:08 AM
    Can u help me?.. I have a problem sending emails, look at my error http://puu.sh/2HuNi.png , http://puu.sh/2HuNH.png i dont know how to fixed it.. Thanks
    Michael WashingtonUser is Offline
    Send Private Message
    Posts:1313
    Avatar

    --
    26 Apr 2013 01:16 AM
    @NESTicle -The settings in the web.config file to contact the mail server are not correct.
    -----------
    Michael Washington
    http://LightSwitchHelpWebsite.com
    NESTicleUser is Offline
    New Member
    New Member
    Send Private Message
    Posts:9
    Avatar

    --
    26 Apr 2013 01:23 AM
    Thanks for the quick replay, but i have this in the web.config file http://puu.sh/2HviA.png
    Michael WashingtonUser is Offline
    Send Private Message
    Posts:1313
    Avatar

    --
    26 Apr 2013 01:41 AM
    Yeah but the port could be wrong or blocked by a firewall. I would google to find a SMTP diagnostic program that will help you track down the connection issues. The error you showed earlier is what I got when I had connection issues.
    -----------
    Michael Washington
    http://LightSwitchHelpWebsite.com
    You are not authorized to post a reply.


    Microsoft Visual Studio is a registered trademark of Microsoft Corporation / LightSwitch is a registered trademark of Microsoft Corporation