-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtendedSMTPAppender.java
60 lines (53 loc) · 1.63 KB
/
ExtendedSMTPAppender.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package org.apache.log4j.extras;
import java.util.ArrayList;
import javax.mail.Header;
import javax.mail.MessagingException;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.net.SMTPAppender;
/**
* An SMTPAppender which has additional capabilities.
*
* <ul>
* <li>Arbitrary headers: <code>log4j.appender.A1.SMTPHeader=Field: value</code>
* <ul>
*
* @author Christopher Schultz
*/
public class ExtendedSMTPAppender
extends SMTPAppender
{
private ArrayList<Header> headers;
/**
* Add a header to SMTP messages sent by this appender.
* Multiple headers can be added by calling <code>setSMTPHeader</code>
* multiple times.
*
* @param header The header to add e.g. <code>Auto-Submitted: auto-generated</code>
*/
public synchronized void setSMTPHeader(String header)
{
if(null == headers)
headers = new ArrayList<Header>();
int split = header.indexOf(':');
if(split >= 0)
headers.add(new Header(header.substring(0, split).trim(),
header.substring(split+1).trim()));
else
headers.add(new Header(header.trim(), ""));
}
@Override
public synchronized void activateOptions()
{
super.activateOptions();
if(null != headers)
{
try {
for(Header h : headers)
msg.addHeader(h.getName(), h.getValue());
} catch(MessagingException me) {
LogLog.error("Could not set SMTP message headers.", me);
}
headers = null; // Don't need those any more
}
}
}