NAnt.Core.Tasks.MailTask.ExecuteTask C# (CSharp) Method

ExecuteTask() protected method

This is where the work is done.
protected ExecuteTask ( ) : void
return void
        protected override void ExecuteTask()
        {
            MailMessage mailMessage = new MailMessage();

            mailMessage.From = this.From;
            mailMessage.To = this.ToList;
            mailMessage.Bcc = this.BccList;
            mailMessage.Cc = this.CcList;
            mailMessage.Subject = this.Subject;
            mailMessage.BodyFormat = this.Format;

            // ensure base directory is set, even if fileset was not initialized
            // from XML
            if (Files.BaseDirectory == null) {
                Files.BaseDirectory = new DirectoryInfo(Project.BaseDirectory);
            }
            if (Attachments.BaseDirectory == null) {
                Attachments.BaseDirectory = new DirectoryInfo(Project.BaseDirectory);
            }

            // begin build message body
            StringWriter bodyWriter = new StringWriter(CultureInfo.InvariantCulture);

            if (!StringUtils.IsNullOrEmpty(Message)) {
                bodyWriter.WriteLine(Message);
                bodyWriter.WriteLine();
            }

            // append file(s) to message body
            foreach (string fileName in Files.FileNames) {
                try {
                    string content = ReadFile(fileName);
                    if (!StringUtils.IsNullOrEmpty(content)) {
                        bodyWriter.Write(content);
                        bodyWriter.WriteLine(string.Empty);
                    }
                } catch (Exception ex) {
                    Log(Level.Warning, string.Format(CultureInfo.InvariantCulture,
                        ResourceUtils.GetString("NA1135"), fileName,
                        ex.Message));
                }
            }

            // add message body to mailMessage
            string bodyText = bodyWriter.ToString();
            if (bodyText.Length != 0) {
                mailMessage.Body = bodyText;
            }

            // add attachments to message
            foreach (string fileName in Attachments.FileNames) {
                try {
                    MailAttachment attachment = new MailAttachment(fileName);
                    mailMessage.Attachments.Add(attachment);
                } catch (Exception ex) {
                    Log(Level.Warning, string.Format(CultureInfo.InvariantCulture,
                        ResourceUtils.GetString("NA1136"), fileName,
                        ex.Message));
                }
            }

            // send message
            try {
                Log(Level.Info, "Sending mail to {0}.", mailMessage.To);
                SmtpMail.SmtpServer = this.Mailhost;
                SmtpMail.Send(mailMessage);
            } catch (Exception ex) {
                StringBuilder msg = new StringBuilder();
                msg.Append("Error enountered while sending mail message."
                    + Environment.NewLine);
                msg.Append("Make sure that mailhost=" + this.Mailhost
                    + " is valid" + Environment.NewLine);
                throw new BuildException("Error sending mail:" + Environment.NewLine
                    + msg.ToString(), Location, ex);
            }
        }