'how to send email with log4j SMTP appender

I want send email to email group only once after I finish the java task.

in log4j properties, I am able to send email,but is triggering email for every log statement.

But my requirement is that to send email after all tasks are completed, meaning start java program, do some processing which will have multiple log statements, then send email.

Below is my log4j smtp properties list.

#log4j.rootLogger=DEBUG, sendMail  
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
#log4j.appender.stdout.layout.ConversionPattern=[%5p] %d{mm:ss} (%F:%M:%L)%n%m%n%n  
#log4j.appender.sendMail=org.apache.log4j.net.SMTPAppender  
##log4j.appender.sendMail.Threshold=WARN  
#[email protected]   
#[email protected]  
#log4j.appender.sendMail.SMTPHost=10.13.16.57
#log4j.appender.sendMail.Subject=Log4J Message  
#log4j.appender.sendMail.layout=org.apache.log4j.PatternLayout  
#log4j.appender.sendMail.layout.ConversionPattern=%p %t %c - %m%n  
#log4j.appender.sendMail.BufferSize=1  
#log4j.appender.sendMail.SMTPDebug=true 


Solution 1:[1]

Since you want to send one email at the end of the batch run, you may be better to aggregate all of the messages into some sort of large string, and then use Spring's JavaMailSender to send a single email.

You can find various articles on sending email using JavaMailSender but essentially you'd need to first configure an instance of it in your Spring config:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="mail_host"/>
    <property name="port" value="25"/>
    <property name="username" value="mail_username"/>
    <property name="password" value="mail_password"/>
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
        </props>
    </property>
</bean>

Then in your client (batch job or whatever it happens to be), you can specify an autowired field for the JavaMailSender:

@Autowired
private JavaMailSender mailSender;

And then it would just be a case of creating and sending the message:

SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(body); // Your aggregated messages
mailSender.send(message);

That's the basics - but you'll find plenty of other articles and documentation discussing it in more detail around the web.

Solution 2:[2]

Define your log4j configuration in XML and add a logger element to use as logger name.

see log4j XML format and Log4j config

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
   <to value="[email protected]" />
   <from value="[email protected]" />
   <subject value="test logging message" />
   <smtpHost value="SMTPServer.domain.com" />
   <layout type="log4net.Layout.PatternLayout">
       <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
   </layout>
</appender>   
<logger name="sendMail" additivity="false">
   <level value="info" />
   <appender-ref ref="mailAppender" />
</logger>

In your code:

private static Logger LOG_MAIL = Logger.getLogger("sendMail");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Will Keeling
Solution 2 Nick