Skip to content

Commit

Permalink
Merge pull request nant#19 from rmboggs/mailtaskupdate
Browse files Browse the repository at this point in the history
Mailtaskupdate - Minor adjustments and unit tests
  • Loading branch information
dguder committed Jan 19, 2012
2 parents e5d09d4 + 8bdd188 commit 2f1be53
Show file tree
Hide file tree
Showing 5 changed files with 1,692 additions and 318 deletions.
Binary file added lib/common/neutral/netDumbster.dll
Binary file not shown.
93 changes: 69 additions & 24 deletions src/NAnt.Core/Tasks/MailTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,19 @@ protected override void ExecuteTask() {
}
else
{
smtp.UseDefaultCredentials = true;
// Mono does not implement the UseDefaultCredentials
// property in the SmtpClient class. So only set the
// property when NAnt is run on .NET. Otherwise,
// use an emtpy NetworkCredential object as the
// SmtpClient credentials.
if (PlatformHelper.IsMono)
{
smtp.Credentials = new NetworkCredential();
}
else
{
smtp.UseDefaultCredentials = true;
}
}

// Set the ssl and the port information.
Expand Down Expand Up @@ -520,8 +532,11 @@ protected override void ExecuteTask() {
/// The content of the specified file.
/// </returns>
private string ReadFile(string filename) {
using (StreamReader reader = new StreamReader(File.OpenRead(filename))) {
return reader.ReadToEnd();
using (StreamReader reader = new StreamReader(File.OpenRead(filename)))
{
string result = reader.ReadToEnd();
reader.Close();
return result;
}
}

Expand Down Expand Up @@ -615,6 +630,10 @@ private MailAddress ConvertStringToMailAddress(string address)
// normal string. Makes validation easier.
string plainAddress = UnescapeXmlCodes(address);

// Local vars to temporarily hold names and email addresses
string resultingName = null;
string resultingEmail = null;

// String array containing all of the regex strings used to
// locate the email address in the parameter string.
string[] validators = new string[]
Expand All @@ -629,10 +648,7 @@ private MailAddress ConvertStringToMailAddress(string address)
@"^\((?<fullname>.+)\)\s(?<email>[^<>\(\)\s]+@[^<>\(\)\s]+\.[^<>\(\)\s]+)$",

// Format: address@abcxyz.com (Full Name)
@"^(?<email>[^<>\(\)\s]+@[^<>\(\)\s]+\.[^\s]+)\s\((?<fullname>.+)\)$",

// Format: address@abcxyz.com
@"(?<email>[^<>\(\)\s]+@[^<>\(\)\s]+\.[^<>\(\)\s]+)"
@"^(?<email>[^<>\(\)\s]+@[^<>\(\)\s]+\.[^\s]+)\s\((?<fullname>.+)\)$"
};

// Loop through each regex string to find the one that the
Expand All @@ -645,29 +661,58 @@ private MailAddress ConvertStringToMailAddress(string address)
Regex currentRegex = new Regex(reg);
Match email = currentRegex.Match(plainAddress);

// If the match is considered successful, return
// a new MailAddress object. If a name was
// paired with an email address in the parameter,
// add it to the MailAddress object that is returned.
// If the match is considered successful, load
// the temp string vars with the found email/fullname
// information to use when creating a new MailAddress
// object. Then break from the loop.
if (email.Success)
{
if (email.Groups["fullname"].Success)
{
return new MailAddress(
email.Groups["email"].Value.Trim(),
email.Groups["fullname"].Value.Trim());
}

return new MailAddress(email.Groups["email"].Value.Trim());
resultingEmail = email.Groups["email"].Value.Trim();
resultingName = email.Groups["fullname"].Value.Trim();
break;
}
}

// If none of the regex strings matches the address parameter,
try
{
// Setup a new MailAddress to return.
MailAddress result;

// If both the temp name and email string vars contain values, initialize
// the result MailAddress var with both values.
if (!String.IsNullOrEmpty(resultingName) && !String.IsNullOrEmpty(resultingEmail))
{
result = new MailAddress(resultingEmail, resultingName);
}
// If only the temp email string var contains a value, initialize
// the result MailAddress var with just that value
else if (!String.IsNullOrEmpty(resultingEmail))
{
result = new MailAddress(resultingEmail);
}
// Otherwise, try initializing the result MailAddress var with the original
// address that was passed to the method.
else
{
result = new MailAddress(plainAddress);
}
// Return the result.
return result;
}
// If the MailAddress var throws a Format exception because of a bad email address,
// throw a build exception.
throw new BuildException(
String.Format(CultureInfo.InvariantCulture,
"{0} is not a recognized email address",
address));
catch (FormatException)
{
throw new BuildException(
String.Format(CultureInfo.InvariantCulture,
"{0} is not a recognized email address",
plainAddress));
}
// Rethrow any other exceptions.
catch (Exception)
{
throw;
}
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions tests/NAnt.Core/NAnt.Core.build
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<references>
<include name="${build.dir}/bin/NAnt.Core.dll" />
<include name="${nant::scan-probing-paths('nunit.framework.dll')}" />
<include name="${nant::scan-probing-paths('netDumbster.dll')}" />
</references>
<resources failonempty="true" basedir="Resources" dynamicprefix="true" prefix="XML:">
<include name="**/*.xml"/>
Expand Down
Loading

0 comments on commit 2f1be53

Please sign in to comment.