Thursday 10 January 2013

Send an Email in Lightswitch


How to send HTML mail in Visual Studio Lighswitch?

 For this you have to import two class in your application

using System.Net;
using System.Net.mail;

 public static void CreateOutlookEmail(string toAddress, string subject, string body)
       {
           dynamic olMailItem = 0;
           dynamic olFormatPlain = 1;
           dynamic olFormatHTML = 2;
           try
           {
               dynamic outlook;
               if (AutomationFactory.IsAvailable)
               {
                   try
                   {
                       // Get the reference to the open Outlook App
                       outlook = AutomationFactory.GetObject("Outlook.Application");
                   }
                   catch
                   {
                       // If Outlook isn't open, then an error will be thrown.
                       //  Try to open the application
                       outlook = AutomationFactory.CreateObject("Outlook.Application");
                   }
                   if (outlook != null)
                   {
                       // Create the email
                       //  Outlook object model (OM) reference:
                       //  msdn.microsoft.com/.../ff870566.aspx
                       dynamic mail = outlook.CreateItem(olMailItem);
                       // With...
                       if (body.ToLower().Contains("<html>"))
                       {
                           mail.BodyFormat = olFormatHTML;
                           mail.HTMLBody = body;
                       }
                       else
                       {
                           mail.BodyFormat = olFormatPlain;
                           mail.Body = body;
                       }
                       mail.Recipients.Add(toAddress);
                       mail.Subject = subject;
                       mail.Save();
                       mail.Display();
                       // mail.Send()
                   }
               }
           }
           catch (Exception ex)
           {
               throw new InvalidOperationException("Failed to create email.", ex);
           }
       }

  for more detail please Click Here.

No comments: