Unable to send email on error from logback

2019-08-30 01:18发布

问题:

I am doing a POC to send email using logback and Java 8. On error we want to send an email to the user so i referred here and my full configuration :

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class App {

    private static final Logger logger = LoggerFactory.getLogger(App.class);

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("java.net.preferIPv4Stack", "true");
        logger.trace("trace message");
        logger.warn("warn message");
        logger.error("error message, a very long error message!");
    }
}

My logback.xml looks like below :

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="5 seconds">
 <appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
        <smtpHost>smtp.gmail.com</smtpHost>
        <username>xxx@gmail.com</username>
        <password>password</password>
        <smtpPort>25</smtpPort>
        <to>xxx@gmail.com</to>
        <from>xxx@gmail.com</from>
        <STARTTLS>true</STARTTLS>
        <asynchronousSending>false</asynchronousSending>
        <subject>Testing %logger
        {20}

        - %m</subject>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d
                {HH:mm:ss.SSS}

                [%thread] %-5level %logger
                {1}

                -%msg%n
            </pattern>
        </layout>
    </appender>
    <root level="ERROR">
        <appender-ref ref="EMAIL" />
    </root>
</configuration>

I see when I run main class the configuration is hanging. I do not see any error messages also. Kindly suggest.

UPDATE: OK ... I see that the port 25 or 587 throws the following error I have also referred this :

20:07:02,050 |-ERROR in ch.qos.logback.classic.net.SMTPAppender[EMAIL] - Error occurred while sending e-mail notification. javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
  nested exception is:
    java.net.SocketException: Permission denied: connect
    at javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587
    at  at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
    at  at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at  at javax.mail.Service.connect(Service.java:317)
    at  at javax.mail.Service.connect(Service.java:176)
    at  at javax.mail.Service.connect(Service.java:125)
    at  at javax.mail.Transport.send0(Transport.java:194)
    at  at javax.mail.Transport.send(Transport.java:124)
    at  at ch.qos.logback.core.net.SMTPAppenderBase.sendBuffer(SMTPAppenderBase.java:388)
    at  at ch.qos.logback.core.net.SMTPAppenderBase.append(SMTPAppenderBase.java:212)
    at  at ch.qos.logback.core.AppenderBase.doAppend(AppenderBase.java:82)
    at  at ch.qos.logback.core.spi.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:51)
    at  at ch.qos.logback.classic.Logger.appendLoopOnAppenders(Logger.java:270)
    at  at ch.qos.logback.classic.Logger.callAppenders(Logger.java:257)
    at  at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:421)
    at  at ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus(Logger.java:383)
    at  at ch.qos.logback.classic.Logger.error(Logger.java:538)
    at  at com.email.example.App.main(App.java:18)
Caused by: java.net.SocketException: Permission denied: connect
    at  at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at  at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at  at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at  at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at  at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at  at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at  at java.net.Socket.connect(Socket.java:589)
    at  at java.net.Socket.connect(Socket.java:538)
    at  at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321)
    at  at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
    at  at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
    at  ... 16 common frames omitted

Am I missing something here? one point I want to mention is the from and to address are same, not sure if that matters.

标签: java logback