787
788
/// <description>The subject of build success messages. [default: "Build Success"]</description>
791
/// <term>MailLogger.success.attachments</term>
792
/// <description>The ID of a fileset holdng set of files to attach when the build is successful.</description>
795
/// <term>MailLogger.failure.attachments</term>
796
/// <description>The ID of a fileset holdng set of files to attach when the build fails.</description>
790
799
/// <term>MailLogger.body.encoding</term>
791
800
/// <description>The encoding type of the body of the e-mail message. [default: system's ANSI code page]</description>
803
/// <term>MailLogger.smtp.username</term>
804
/// <description>The name of the user to login to the SMTP server.</description>
807
/// <term>MailLogger.smtp.password</term>
808
/// <description>The password of the specified user.</description>
811
/// <term>MailLogger.smtp.enablessl</term>
812
/// <description>Specifies whether to use SSL to encrypt the connection. [default: false]</description>
815
/// <term>MailLogger.smtp.port</term>
816
/// <description>The SMTP server port to connect to. [default: 25]</description>
906
// create message to send
877
907
MailMessage mailMessage = new MailMessage();
878
mailMessage.From = GetPropertyValue(properties, "from", null);
879
mailMessage.To = GetPropertyValue(properties, prefix + ".to", null);
908
mailMessage.From = GetPropertyValue(properties, "from", null, true);
909
mailMessage.To = GetPropertyValue(properties, prefix + ".to", null, true);
880
910
mailMessage.Subject = GetPropertyValue(properties, prefix + ".subject",
881
(success) ? "Build Success" : "Build Failure");
911
(success) ? "Build Success" : "Build Failure", false);
882
912
mailMessage.Body = _buffer.ToString();
914
string smtpUsername = GetPropertyValue(properties, "smtp.username", null, false);
915
if (smtpUsername != null) {
916
#if (NET_1_1 || MONO_1_0)
917
mailMessage.Fields[cdoNamespaceURI + "smtpauthenticate"] = 1;
918
mailMessage.Fields[cdoNamespaceURI + "sendusername"] = smtpUsername;
920
Console.Error.WriteLine("[MailLogger] MailLogger.smtp.username"
921
+ " is not supported if NAnt is built targeting .NET"
922
+ " Framework 1.0.");
926
string smtpPassword = GetPropertyValue(properties, "smtp.password", null, false);
927
if (smtpPassword == null) {
928
#if (NET_1_1 || MONO_1_0)
929
mailMessage.Fields[cdoNamespaceURI + "sendpassword"] = smtpPassword;
931
Console.Error.WriteLine("[MailLogger] MailLogger.smtp.password"
932
+ " is not supported if NAnt is built targeting .NET"
933
+ " Framework 1.0.");
937
string smtpPort = GetPropertyValue(properties, "smtp.port", null, false);
938
if (smtpPort != null) {
939
#if (NET_1_1 || MONO_1_0)
940
mailMessage.Fields[cdoNamespaceURI + "smtpserverport"] = smtpPort;
942
Console.Error.WriteLine("[MailLogger] MailLogger.smtp.port"
943
+ " is not supported if NAnt is built targeting .NET"
944
+ " Framework 1.0.");
948
string enableSSL = GetPropertyValue(properties, "smtp.enablessl", null, false);
949
if (enableSSL != null) {
950
#if (NET_1_1 || MONO_1_0)
951
mailMessage.Fields[cdoNamespaceURI + "smtpusessl"] = enableSSL;
953
Console.Error.WriteLine("[MailLogger] MailLogger.smtp.enablessl"
954
+ " is not supported if NAnt is built targeting .NET"
955
+ " Framework 1.0.");
959
// attach files in fileset to message
960
AttachFiles(mailMessage, project, GetPropertyValue(properties,
961
prefix + ".attachments", null, false));
963
// set encoding of body
884
964
if (bodyEncoding != null) {
885
965
mailMessage.BodyEncoding = bodyEncoding;
888
968
// send the message
889
SmtpMail.SmtpServer = GetPropertyValue(properties, "mailhost", "localhost");
969
SmtpMail.SmtpServer = GetPropertyValue(properties, "mailhost", "localhost", false);
890
970
SmtpMail.Send(mailMessage);
891
971
} catch (Exception ex) {
892
Console.WriteLine("MailLogger failed to send e-mail!");
893
Console.WriteLine(ex.ToString());
972
Console.Error.WriteLine("[MailLogger] E-mail could not be sent!");
973
Console.Error.WriteLine(ex.ToString());
912
992
/// <param name="properties">Properties to obtain value from.</param>
913
993
/// <param name="name">Suffix of property name. "MailLogger" will be prepended internally.</param>
914
994
/// <param name="defaultValue">Value returned if property is not present in <paramref name="properties" />.</param>
995
/// <param name="required">Value indicating whether the property should exist, or have a default value set.</param>
916
997
/// The value of the specified property; or the default value if the
917
998
/// property is not present in <paramref name="properties" />.
919
/// <exception cref="ArgumentNullException">The specified property is not present and no default value has been given.</exception>
920
private string GetPropertyValue(PropertyDictionary properties, string name, string defaultValue) {
1000
/// <exception cref="ArgumentNullException"><paramref name="required" /> is <see langword="true" />, and the specified property is not present and no default value has been given.</exception>
1001
private string GetPropertyValue(PropertyDictionary properties, string name, string defaultValue, bool required) {
921
1002
string propertyName = "MailLogger." + name;
922
1003
string value = (string) properties[propertyName];
925
1006
value = defaultValue;
1009
if (required && value == null) {
929
1010
throw new ArgumentNullException(string.Format(CultureInfo.InvariantCulture, "Missing required parameter {0}.", propertyName));
1016
private void AttachFiles(MailMessage mail, Project project, string filesetID) {
1017
if (StringUtils.IsNullOrEmpty(filesetID)) {
1022
FileSet fileset = project.DataTypeReferences[filesetID] as FileSet;
1023
if (fileset == null) {
1024
Console.Error.WriteLine("[MailLogger] Fileset \"{0}\" is not"
1025
+ " defined. No files have been attached.", filesetID);
1029
foreach (string fileName in fileset.FileNames) {
1030
if (!File.Exists(fileName)) {
1031
Console.Error.WriteLine("[MailLogger] Attachment \"{0}\""
1032
+ " does not exist. Skipping.", filesetID);
1036
// create attachment
1037
MailAttachment attachment = new MailAttachment(fileName,
1038
MailEncoding.UUEncode);
1039
// add attachment to mail
1040
mail.Attachments.Add(attachment);
935
1044
#endregion Private Instance Methods
937
1046
#region Private Instance Fields
1199
/// Writes a character array to the text stream.
1308
/// Writes a character array to the buffer.
1201
1310
/// <param name="chars">The character array to write to the text stream.</param>
1202
1311
public override void Write(char[] chars) {
1203
_message += new string(chars, 0, chars.Length -1);
1312
Write(new string(chars, 0, chars.Length -1));
1316
/// Writes a string to the buffer.
1318
/// <param name="value"></param>
1206
1319
public override void Write(string value) {
1207
1320
_message += value;
1324
/// Writes an empty string to the logging infrastructure.
1210
1326
public override void WriteLine() {
1211
1327
WriteLine(string.Empty);
1216
/// Writes a string followed by a line terminator to the text stream.
1332
/// Writes a string to the logging infrastructure.
1218
1334
/// <param name="value">The string to write. If <paramref name="value" /> is a null reference, only the line termination characters are written.</param>
1219
1335
public override void WriteLine(string value) {
1220
_task.Log(OutputLevel, _message + value);
1221
_message = string.Empty;
1228
1344
/// <param name="line">The formatting string.</param>
1229
1345
/// <param name="args">The object array to write into format string.</param>
1230
1346
public override void WriteLine(string line, params object[] args) {
1231
_task.Log(OutputLevel, _message + string.Format(
1232
CultureInfo.InvariantCulture, line, args));
1233
_message = string.Empty;
1347
_message += string.Format(CultureInfo.InvariantCulture, line, args);
1236
public override void Close() {
1352
/// Causes any buffered data to be written to the logging infrastructure.
1354
public override void Flush() {
1237
1355
if (_message.Length != 0) {
1238
1356
_task.Log(OutputLevel, _message);
1239
1357
_message = string.Empty;
1362
/// Closes the current writer and releases any system resources
1363
/// associated with the writer.
1365
public override void Close() {