SmtpMail Task Impl

In case someone needs an SmtpMail task. It was pretty easy to implement:



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

namespace Acme.Tools.MSBuildTasks
{
publicclass SmtpMail : Task
{
privatestring _smtpHost;
privatestring _from;
privatestring _to;
privatestring _cc = "";
privatestring _subject = "";
privatestring _body;
privatestring _attachments = "";
privateint _timeout = 30 * 1000;
private Nullable<int> _portNumber;

[Required]
publicstring SmtpHost
{
get {return _smtpHost; }
set { _smtpHost = value; }
}

publicstring SmtpPort
{
get
{
string s = _portNumber.HasValue ? _portNumber.ToString() : "";
return s;
}
set
{
int port;
if (Int32.TryParse(value,out port))
{
_portNumber = port;
}
}
}

publicint Timeout
{
get {return _timeout; }
set { _timeout = value; }
}

[Required]
publicstring From
{
get {return _from; }
set { _from = value; }
}

[Required]
publicstring To
{
get {return _to; }
set { _to = value; }
}

publicstring CC
{
get {return _cc; }
set { _cc = ((value ==null) ? "" : value); }
}

publicstring Subject
{
get {return _subject; }
set { _subject = ((value ==null) ? "" : value); }
}

[Required]
publicstring Body
{
get {return _body; }
set { _body = value; }
}

publicstring Attachments
{
get {return _attachments; }
set { _attachments = value; }
}

publicoverridebool Execute()
{
MailMessage message =new MailMessage();

message.From =new MailAddress(_from);

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

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

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

message.Subject = _subject;
message.Body = _body;

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

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

smtpClient.Send(message);

returntrue;
}
}
}

[5309 byte] By [KeithHill] at [2008-2-6]
# 1
Thanks Keith.

Hopefully someone will set up a site on the web to share msbuild tasks and loggers...

DanMoseley at 2007-9-8 > top of Msdn Tech,Visual Studio,Visual Studio MSBuild...

Visual Studio

Site Classified