1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
.Extensions
.ExceptionChaining
18 using System
.Configuration
;
19 using Castle
.Components
.Common
.EmailSender
;
20 using Castle
.Core
.Configuration
;
23 /// A handle that sends the exception by email
25 public class EmailHandler
: AbstractExceptionHandler
, IConfigurableHandler
27 private String mailTo
;
28 private String mailFrom
= "donotreply@castleproject.org";
31 /// Implementors should check for known attributes and child nodes
32 /// within the <c>exceptionHandlerNode</c>
34 /// <param name="exceptionHandlerNode">The Xml node
35 /// that represents this handler on the configuration file</param>
36 public void Configure(IConfiguration exceptionHandlerNode
)
38 string mailToAtt
= exceptionHandlerNode
.Attributes
["mailTo"];
40 if (mailToAtt
== null)
42 String message
= "'mailTo' is a required attribute " +
43 "for EmailHandler (part of ExceptionChaining extension)";
44 throw new ConfigurationErrorsException(message
);
49 string mailFromAtt
= exceptionHandlerNode
.Attributes
["mailFrom"];
51 if (mailFromAtt
!= null)
53 mailFrom
= mailFromAtt
;
58 /// Implementors should perform the action
59 /// on the exception. Note that the exception
60 /// is available in <see cref="IEngineContext.LastException"/>
62 /// <param name="context"></param>
63 public override void Process(IEngineContext context
)
65 IEmailSender emailSender
= (IEmailSender
) context
.GetService(typeof(IEmailSender
));
67 String message
= BuildStandardMessage(context
);
71 emailSender
.Send(mailFrom
, mailTo
,
72 "MonoRail Exception: " + context
.LastException
.GetType().Name
, message
);
76 // We ignore errors during send