Showing posts with label .net programming. Show all posts
Showing posts with label .net programming. Show all posts

Sunday, April 17, 2011

Send Email in SharePoint via .NET SmtpClient Class and SharePoint SPUtility Class

In SharePoint, you could send email programmatically by using either .NET Class Library or SharePoint Object Model. I always prefer the second method since using SharePoint ensures that the required settings are maintained by Central Administration.

The following show the two code examples on how you could do this:

Sending Email via .NET SmtpClient Class

using System.Net.Mail;
using Microsoft.SharePoint;

/// <summary>
/// Sends the mail via NET SmtpClient.
/// </summary>
/// <param name="Subject">The subject.</param>
/// <param name="Body">The body.</param>
/// <param name="IsBodyHtml">if set to <c>true</c> [is body HTML].</param>
/// <param name="From">From.</param>
/// <param name="To">To.</param>
/// <param name="Cc">The cc.</param>
/// <param name="Bcc">The BCC.</param>
/// <returns></returns>
public static bool SendMailviaNET(string Subject, string Body, bool IsBodyHtml, string From, string To, string Cc, string Bcc)
{
    bool mailSent = false;
    try
    {
        SmtpClient smtpClient = new SmtpClient();
        smtpClient.Host = SPContext.Current.Site.WebApplication.OutboundMailServiceInstance.Server.Address;
        MailMessage mailMessage = new MailMessage(From, To, Subject, Body);

        if (!String.IsNullOrEmpty(Cc))
        {
            MailAddress CCAddress = new MailAddress(Cc);
            mailMessage.CC.Add(CCAddress);
        }
        if (!String.IsNullOrEmpty(Bcc))
        {
            MailAddress BCCAddress = new MailAddress(Bcc);
            mailMessage.Bcc.Add(BCCAddress);
        }

        mailMessage.IsBodyHtml = IsBodyHtml;
        smtpClient.Send(mailMessage);
        mailSent = true;
    }
    catch (Exception)
    {
        return mailSent;
    }
    return mailSent;
}

Sending Email via SharePoint SPUtility Class

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Collections.Specialized;

/// <summary>
/// Sends the mail via SharePoint SPUtility.
/// </summary>
/// <param name="Subject">The subject.</param>
/// <param name="Body">The body.</param>
/// <param name="IsBodyHtml">if set to <c>true</c> [is body HTML].</param>
/// <param name="From">From.</param>
/// <param name="To">To.</param>
/// <param name="Cc">The cc.</param>
/// <param name="Bcc">The BCC.</param>
/// <returns></returns>
public static bool SendMailviaSharePoint(string Subject, string Body, bool IsBodyHtml, string From, string To, string Cc, string Bcc)
{
    bool mailSent = false;
    try
    {
        SPWeb thisWeb = SPContext.Current.Web;
               
        StringDictionary headers = new StringDictionary();
        headers.Add("to", To);
        headers.Add("cc", Cc);
        headers.Add("bcc", Bcc);
        headers.Add("from", From);
        headers.Add("subject", Subject);
        if (IsBodyHtml) headers.Add("content-type", "text/html");

        mailSent = SPUtility.SendEmail(thisWeb, headers, Body);
    }
    catch (Exception)
    {
        return mailSent;
    }
    return mailSent;
}

Thursday, February 10, 2011

MSDN Virtual Lab: Introduction to Microsoft ASP.NET Web Forms 4.0

Today MSDN has released a newest Virtual Lab: Introduction to Microsoft ASP.NET Web Forms 4.0. Enjoy it!

Objectives

After completing this lab, you will be better able to:

  • Take advantage of the new ASP.NET Project Templates
  • Control server control ClientIds
  • Enable bi-directional routing support
  • Control the View State at application and page level

Scenario

ASP.NET 4 Web Forms provides enhancements in targeted areas and includes some new features. This Lab is also available online at http://channel9.msdn.com/learn and covers the following features:

New ASP.NET Project Templates

ASP.NET 4 Web Forms includes updated templates for creating new web applications and web sites. These templates include common functionality already implemented, which helps reducing the development time and provides guidance on best practices for building ASP.NET 4 Web applications.

Client IDs

Developers can now manage control IDs that affect rendered client IDs. The Control class now provides a new property called ClientIDMode that allows you to specify what behavior you want the runtime to take when determining whether or not to refactor the client ID upon rendering. This removes the previous bloat in the client ID.

URL Routing

ASP.NET 4 Web Forms introduces the new PageRouteHandler class that integrates URL Routing with Web Form Pages. URL routing in ASP.NET enables you to use URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs in a Web application that are descriptive of the user's action and are more easily understood by users and search engines. In URL routing, you define URL patterns that contain placeholders for values that are used when you handle URL requests. At run time, the pieces of the URL that follow the application name are parsed into discrete values, based on a URL pattern that you have defined.

View State

ASP.NET 4 Web Forms provides a more granular control over the View State. Developers can now disable the View State on a Page and enable it on specific server controls, and also disable it on a control an enable it on its childs.

Estimated time to complete

90 min

Computers used in this Lab

  • VS2010Labs-PC
  • The password for the Administrator account on all computers in this lab is: pass@word1

Thursday, February 3, 2011

Random Number Generation - Not Random At All

using System;

namespace MyRandom
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {
                // Generate random number
                Random random = new Random();
                int myNum = random.Next(0, 10);
                // Write generated random number to console window
                Console.WriteLine("My random number: " + myNum.ToString());
            }
            // Pause running console window
            Console.ReadLine();
        }
    }
}

Did you know that if you declared Random Class within the loop as shown in the code sample above, it will not generate random number at all – in fact it will generate same number as shown in the screenshot below:

RandomNumber

To fix this problem, you need to declare Random Class outside of for loop as follows:

using System;

namespace MyRandom
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();
            for (int i = 0; i < 10; i++)
            {
                // Generate random number
                int myNum = random.Next(0, 10);
                // Write generated random number to console window
                Console.WriteLine("My random number: " + myNum.ToString());
            }
            // Pause running console window
            Console.ReadLine();
        }
    }
}