V
V
Vyacheslav2018-05-17 21:18:11
Java
Vyacheslav, 2018-05-17 21:18:11

How to send log to mail using log4j 2?

Logging to the console and the file works correctly, but it does not send letters.
there are these settings:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
    <Appenders>

        <!-- SMTP appender -->
        <SMTP name="Mail" subject="Logging"
              to="[email protected]" from= "[email protected]"
              smtpHost="smtp.gmail.com" smtpPort="465" bufferSize="100"
              smtpProtocol="smtps"

        >
            <PatternLayout>
                <Pattern>%d{dd MM yyyy HH:mm:ss,SSS}  - %msg%n</Pattern>
            </PatternLayout>
        </SMTP>

        <!-- File appender -->
        <File name="File" fileName="c:/logs/app.log">
            <PatternLayout>
                <Pattern>%d{dd MM yyyy HH:mm:ss,SSS}  - %msg%n</Pattern>
            </PatternLayout>
        </File>

        <!-- Console appender -->
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="%m%n"/>
        </Console>

    </Appenders>

    <Loggers>
        <Root level="error">
            <AppenderRef ref="Mail"/>
            <AppenderRef ref="File" />
            <AppenderRef ref="STDOUT"/>
        </Root>

    </Loggers>
</Configuration>

writes:
JavaFX Application Thread ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.SmtpAppender for element SMTP: java.lang.NoClassDefFoundError: javax/mail/MessagingException java.lang.reflect.InvocationTargetException
at sun 0
_
_
_ (Method.java:498)

JavaFX Application Thread ERROR Null object returned for SMTP in Appenders.
documentation if anything here: https://logging.apache.org/log4j/2.x/manual/append...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2018-05-18
@ShamanSBubnom

Obviously, there are not enough classes to work with SMTP.
It worked for me with adding dependencies in Maven:

<dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>smtp</artifactId>
            <version>1.6.1</version>
        </dependency>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question