Click here to Skip to main content
15,886,648 members
Articles / Programming Languages / C#

NAnt : Little Pretty Automatic

Rate me:
Please Sign up or sign in to vote.
4.61/5 (16 votes)
17 Oct 20042 min read 135.4K   524   57  
Some little utility tasks for NAnt
using System;
using System.IO;
using System.Web.Mail;
using NAnt.Core;
using NAnt.Core.Attributes;
using NAnt.Core.Util;

namespace NAnt.Utility.Tasks
{
	/// <summary>
	/// Sends e-mail from NAnt.
	/// </summary>
	[TaskName("email")]
	public class EmailTask : Task
	{

        private string _smtpServer = null;
        private string _fromEmail = null;
        private string _toEmail = null;
        private string _subject = null;
        private string _fileToSend = null;

        /// <summary>
        /// Sender's e-mail.
        /// </summary>
        [TaskAttribute("from")]
        public virtual string FromEmail
        {
            get
            {
                if (null == _fromEmail)
                    return string.Format("nant@{0}", System.Net.Dns.GetHostName().ToLower());
                else
                    return _fromEmail;
            }
            set { _fromEmail = StringUtils.ConvertEmptyToNull(value); }
        }

        /// <summary>
        /// Recipient's e-mail.
        /// </summary>
        [TaskAttribute("to", Required = true)]
        [StringValidator(AllowEmpty = false)]
        public virtual string ToEmail
        {
            get { return StringUtils.ConvertNullToEmpty(_toEmail); }
            set { _toEmail = StringUtils.ConvertEmptyToNull(value); }
        }

        /// <summary>
        /// E-mail subject.
        /// </summary>
        [TaskAttribute("subject")]
        public virtual string Subject
        {
            get
            {
                if (null == _subject)
                    return "NANT e-mail report";
                else
                    return _subject;
            }
            set { _subject = StringUtils.ConvertEmptyToNull(value); }
        }

        /// <summary>
        /// SMTP server to send e-mail through.
        /// </summary>
        [TaskAttribute("smtp")]
        public virtual string SMTPServer
        {
            get
            {
                if (null == _smtpServer)
                    return System.Net.Dns.GetHostName().ToLower();
                else
                    return _smtpServer;
            }
            set { _smtpServer = StringUtils.ConvertEmptyToNull(value); }
        }

        /// <summary>
        /// Files to send to recipient.
        /// </summary>
        [TaskAttribute("files")]
        public virtual string FilesToSend
        {
            get { return StringUtils.ConvertNullToEmpty(_fileToSend); }
            set { _fileToSend = StringUtils.ConvertEmptyToNull(value); }
        }

        /// <summary>
        /// Executes the task.
        /// </summary>
        protected override void ExecuteTask()
        {
            System.Web.Mail.SmtpMail.SmtpServer = SMTPServer;

            //  Construct mail message
            MailMessage Message = new MailMessage();
            Message.From = FromEmail;
            Message.Subject = Subject;
            Message.Body = XmlNode.InnerText.Trim();
            Message.BodyFormat = MailFormat.Text;

            if (0 != FilesToSend.Length)
            {
                //  Attach files to message
                foreach (string File in FilesToSend.Split(';', ','))
                {
                    string FilePath = System.IO.Path.Combine(this.Project.BaseDirectory, File);
                    if (System.IO.File.Exists(FilePath))
                        Message.Attachments.Add(new MailAttachment(FilePath, MailEncoding.Base64));
                }
            }
            
            foreach (string Recipient in ToEmail.Split(';', ','))
            {
                Message.To = Recipient;
                //  Send e-mail
                Log(Level.Info, string.Format("Sending e-mail from {0} to {1} using {2}", 
                    FromEmail, ToEmail, SMTPServer));
                System.Web.Mail.SmtpMail.Send(Message);
            }
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
Started professional career in software development back in 2000, in Ukraine. Founder and owner of a boutique software company called ByteGems.com Software. Worked for 6 years at w2bi, Inc in New Jersey USA, currently work in a large multinational company based in Redmond, WA.

My buzzwords at the moment: .NET, C#, ASP.NET, MVC, LINQ, TypeScript, JavaScript, AngularJS, HTML, JSON, services.

Still buzzing: C++, Win32, ATL, MFC, SQL, WinForms, WebForms, EF, Sockets, TCP/IP, Remoting.

Comments and Discussions