我正在尝试通过log4j发送错误电子邮件。通过使用以下appender:
<appender name="ERROR_MAIL" class="org.apache.log4j.net.SMTPAppender">
<param name="SMTPUsername" value="xxxxxx@gmail.com" />
<param name="SMTPPassword" value="**********" />
<param name="To" value="test@gmail.com"/>
<param name="From" value="xxxxxx@gmail.com"/>
<param name="Subject" value="Newyse Error "/>
<param name="SMTPHost" value="smtp.gmail.com"/>
<param name="SMTPPort" value="25" />
<param name="BufferSize" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601} %t %5p %c:$L]"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR"/>
<param name="LevelMax" value="FATAL"/>
</filter>
</appender>
但我收到以下异常
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. wr9sm43519864pbc.7 - gsmtp
从其他几个问题我明白我需要在smtpAppender
props.put("mail.smtp.starttls.enable", "true");
那么我们如何将它添加到现有的SMTPAppender?
答案 0 :(得分:13)
第一种方式:您可以扩展SMTP appender并覆盖createSession()方法,您可以在其中添加任何其他属性到Java Mail会话,如前面提到的“mail.smtp.starttls.enable”
public class SecureSMTPAppender extends SMTPAppender {
private boolean useStartTLS;
public void setUseStartTLS(boolean useStartTLS) {
this.useStartTLS = useStartTLS;
}
@Override
protected Session createSession() {
Properties props = null;
try {
props = new Properties(System.getProperties());
} catch (SecurityException ex) {
props = new Properties();
}
if (getSMTPHost() != null) {
props.put("mail.smtp.host", getSMTPHost());
}
if (useStartTLS) {
props.put("mail.smtp.starttls.enable", "true");
}
Authenticator auth = null;
if (getSMTPPassword() != null && getSMTPUsername() != null) {
props.put("mail.smtp.auth", "true");
auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(getSMTPUsername(), getSMTPPassword());
}
};
}
Session session = Session.getInstance(props, auth);
if (getSMTPDebug()) {
session.setDebug(true);
}
return session;
}
}
第二种方式:您可以使用-Dmail.smtp.starttls.enable = true选项启动Java进程。这种方法看起来更容易,但需要控制JVM选项;它也可能被过于严格的SecurityManager打破。
答案 1 :(得分:0)
非常感谢@ Jk1给我答案,让我得到问题中提到的配置的工作代码。
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import org.apache.log4j.Layout;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.net.SMTPAppender;
import org.apache.log4j.spi.LoggingEvent;
import com.sun.mail.smtp.SMTPTransport;
/**
* Extension of Log4j {@link SMTPAppender} for Gmail support
*
*/
public class SecureSMTPAppender extends SMTPAppender
{
/**
* Cached session for later use i.e. while sending emails
*/
protected Session session;
public SecureSMTPAppender()
{
super();
}
/**
* Create mail session.
*
* @return mail session, may not be null.
*/
protected Session createSession()
{
Properties props = new Properties();
props.put("mail.smtps.host", getSMTPHost());
props.put("mail.smtps.auth", "true");
Authenticator auth = null;
if (getSMTPPassword() != null && getSMTPUsername() != null)
{
auth = new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(getSMTPUsername(), getSMTPPassword());
}
};
}
session = Session.getInstance(props, auth);
if (getSMTPDebug())
{
session.setDebug(getSMTPDebug());
}
return session;
}
/**
* Send the contents of the cyclic buffer as an e-mail message.
*/
protected void sendBuffer()
{
try
{
MimeBodyPart part = new MimeBodyPart();
StringBuffer sbuf = new StringBuffer();
String t = layout.getHeader();
if (t != null)
sbuf.append(t);
int len = cb.length();
for (int i = 0; i < len; i++)
{
LoggingEvent event = cb.get();
sbuf.append(layout.format(event));
if (layout.ignoresThrowable())
{
String[] s = event.getThrowableStrRep();
if (s != null)
{
for (int j = 0; j < s.length; j++)
{
sbuf.append(s[j]);
sbuf.append(Layout.LINE_SEP);
}
}
}
}
t = layout.getFooter();
if (t != null)
sbuf.append(t);
part.setContent(sbuf.toString(), layout.getContentType());
Multipart mp = new MimeMultipart();
mp.addBodyPart(part);
msg.setContent(mp);
msg.setSentDate(new Date());
send(msg);
} catch (Exception e)
{
LogLog.error("Error occured while sending e-mail notification.", e);
}
}
/**
* Pulled email send stuff i.e. Transport.send()/Transport.sendMessage(). So
* that on required this logic can be enhanced.
*
* @param msg
* Email Message
* @throws MessagingException
*/
protected void send(Message msg) throws MessagingException
{
SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
try
{
t.connect(getSMTPHost(), getSMTPUsername(), getSMTPPassword());
t.sendMessage(msg, msg.getAllRecipients());
} finally
{
System.out.println("Response: " + t.getLastServerResponse());
t.close();
}
}
}