0

有没有办法通过只允许基于 NTLM 身份验证的交换服务器使用 Java log4j 发送电子邮件?

4

1 回答 1

0

默认附加程序仅依赖于 SMTP 身份验证,因此它不会自行生成。您可以按照此处所述创建自定义电子邮件附加程序(链接只是为了说明谁首先分享了这个想法,这不是我的)。它基本上创建了完成身份验证的附加程序

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;

import org.apache.log4j.net.SMTPAppender;

/**
* @author DEV
*
*/
public class GmailAppender extends SMTPAppender {
private boolean startTLS = false;

@Override
protected Session createSession() {
Properties props = null;
try {
props = new Properties(System.getProperties());
} catch (SecurityException ex) {
props = new Properties();
}
String prefix = "mail.smtp";
if (this.getSMTPProtocol() != null) {
props.put("mail.transport.protocol", this.getSMTPProtocol());
prefix = "mail." + this.getSMTPProtocol();
}

if (this.getSMTPHost() != null)
props.put(prefix + ".host", this.getSMTPHost());
if (this.getSMTPPort() > 0)
props.put(prefix + ".port", String.valueOf(this.getSMTPPort()));
if (this.startTLS)
props.put("mail.smtp.starttls.enable", "true");

Authenticator auth = null;
if (this.getSMTPPassword() != null && this.getSMTPUsername() != null) {
props.put(prefix + ".auth", "true");
auth = new Authenticator() {
...

然后,在配置文件中,

#email appender
log4j.appender.mail=com.horri.GmailAppender
#defines how othen emails are send
log4j.appender.mail.BufferSize=1
log4j.appender.mail.SMTPHost=smtp.gmail.com
log4j.appender.mail.From=yourmail@servermail.com
log4j.appender.mail.To=yourmail@gmail.com
log4j.appender.mail.startTLS = true
log4j.appender.mail.SMTPUsername=yourgmailusername
log4j.appender.mail.SMTPPassword=yourgmailpasswd
log4j.appender.mail.Subject=Log ...
log4j.appender.mail.threshold=error
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

**编辑:有时有用会与“支持懒惰”混为一谈。因此,这是一个 apache 许可的 NTLM auth 实现。http://code.google.com/p/ntlm-java/希望它能正常工作。

于 2012-09-07T21:34:41.190 回答