Welcome to AspAdvice Sign in | Join | Help

Generate Email Notifications With MSBuild and Web Deployment Projects

If you are not familiar with MSBuild I suggest you take the time to read over the reference and overview. I use Web Deployment Projects to automate my build process to include the build release type as well as deployments to the testing and staging servers, this in itself is an entire new topic.

Once I completed my build and deployment I previously turned to Outlook and sent an email to the development team notifing them the build has been completed and to be honest this was a process that was easily overlooked and redundant. To overcome this manual process I turned to using a custom MSBuild task. The purpose of this task was to provide an automated mechanism of generating the email to the development team.

First thing is to create a Class Library. This is not any different than any class library with the exception that you reference the Microsoft.Build.Utilities and Microsoft.Build.Framework and your class must inherit Task Class.

Example BuildNotificationByEmail Class:

using System;
using System.Collections.Generic;
using System.Net.Mail;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace RadDev.MSBuildClassLibrary
{
    public class BuildNotificationByEmail : Task
    {
        private string _smtpHost = string.Empty;
        private string _from = string.Empty;
        private string _to = string.Empty;
        private string _cc = string.Empty;
        private string _subject = string.Empty;
        private string _body = string.Empty;
        private string _attachments = string.Empty;
        private string _userName = string.Empty;
        private string _password = string.Empty;
        private int _timeout = 30 * 1000;
        private Nullable<int> _portNumber;

        /// <summary>
        /// Gets or sets the SMTP host.
        /// </summary>
        /// <value>The SMTP host.</value>
        [Required]
        public string SmtpHost
        {
            get { return _smtpHost; }
            set { _smtpHost = value; }
        }

        /// <summary>
        /// Gets or sets the SMTP port.
        /// </summary>
        /// <value>The SMTP port.</value>
        public string SmtpPort
        {
            get
            {
                string s;
                if (_portNumber.HasValue)
                    s= _portNumber.ToString();
                else
                    s = "";
                return s;
            }
            set
            {
                int port;
                if (Int32.TryParse(value, out port))
                {
                    _portNumber = port;
                }
            }
        }

        /// <summary>
        /// Gets or sets the timeout.
        /// </summary>
        /// <value>The timeout.</value>
        public int Timeout
        {
            get { return _timeout; }
            set { _timeout = value; }
        }

        /// <summary>
        /// Gets or sets from.
        /// </summary>
        /// <value>From.</value>
        [Required]
        public string From
        {
            get { return _from; }
            set { _from = value; }
        }

        /// <summary>
        /// Gets or sets to.
        /// </summary>
        /// <value>To.</value>
        [Required]
        public string To
        {
            get { return _to; }
            set { _to = value; }
        }

        /// <summary>
        /// Gets or sets the CC.
        /// </summary>
        /// <value>The CC.</value>
        public string CC
        {
            get { return _cc; }
            set { _cc = ((value == null) ? "" : value); }
        }

        /// <summary>
        /// Gets or sets the subject.
        /// </summary>
        /// <value>The subject.</value>
        public string Subject
        {
            get { return _subject; }
            set { _subject = ((value == null) ? "" : value); }
        }

        /// <summary>
        /// Gets or sets the body.
        /// </summary>
        /// <value>The body.</value>
        [Required]
        public string Body
        {
            get { return _body; }
            set { _body = value; }
        }

        /// <summary>
        /// Gets or sets the attachments.
        /// </summary>
        /// <value>The attachments.</value>
        public string Attachments
        {
            get { return _attachments; }
            set { _attachments = value; }
        }

        /// <summary>
        /// Gets or sets the name of the user.
        /// </summary>
        /// <value>The name of the user.</value>
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        /// <summary>
        /// Gets or sets the password.
        /// </summary>
        /// <value>The password.</value>
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        /// <summary>
        /// When overridden in a derived class, executes the task.
        /// </summary>
        /// <returns>
        /// true if the task successfully executed; otherwise, false.
        /// </returns>
        public override bool Execute()
        {
            MailMessage message = new MailMessage();

            message.From = new MailAddress(_from);

            string[] toAddresses =
                _to.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            foreach (string address in toAddresses)
            {
                message.To.Add(address);
            }

            string[] ccAddresses =
                _cc.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            foreach (string address in ccAddresses)
            {
                message.CC.Add(address);
            }

            string[] attachments =
                _attachments.Split(";".ToCharArray(),
                                          StringSplitOptions.RemoveEmptyEntries);
            foreach (string attachment in attachments)
            {
                message.Attachments.Add(new Attachment(attachment.Trim()));
            }

            message.Subject = _subject;
            string _dateTime = DateTime.Now.ToUniversalTime().ToString() + "\r\n";

            message.Body = "Build completed at " + _dateTime + _body;

            SmtpClient smtpClient = new SmtpClient(_smtpHost);
            smtpClient.Timeout = _timeout;

            if (_portNumber.HasValue)
            {
                smtpClient.Port = _portNumber.Value;
            }

            smtpClient.Send(message);

            return true;
        }
    }
}

Once you have compiled you assembly simply drop it directly into the folder that contains you web deployment project.

Next open the Web Deployment Project file for editing with Visual Studio 2005 and at the following:

<UsingTask TaskName="RadDev.MSBuildClassLibrary.SmtpMail" AssemblyFile="RadDev.MSBuildClassLibrary.dll"/>
<SmtpMail SmtpHost="radicaldev"
                    To="someone@somewhere.com"
                    From="someone@somewhere.com"
                    Subject="Build Complete and Deployed to Server One"
                    Body="Version 1.0 is ready for testing." />

Here is a short snippet of my project file:

<!-- 
  Microsoft Visual Studio 2005 Web Deployment Project 
  http://go.microsoft.com/fwlink/?LinkId=55111
-->
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <UsingTask TaskName="RadDev.MSBuildClassLibrary.SmtpMail" AssemblyFile="RadDev.MSBuildClassLibrary.dll"/>
    .....
    <Target Name="AfterBuild">
        <SmtpMail SmtpHost="radicaldev"
                    To="someone@somewhere.com"
                    From="someone@somewhere.com"
                    Subject="Build Complete and Deployed to Server One"
                    Body="Version 1.0 is ready for testing." />
    </Target>
</Project>

Now all that remains is to build your web deployment project. Once the build completes suucessfully the email address you defined will recieve an automated email.

Published Monday, May 28, 2007 6:48 PM by sswafford

Comments

New Comments to this post are disabled