0

I have the following requirement.

I have validate given application paths more than 40 and send the email to distribution groups with the path and issue details every 60 min.

I am thinking which email framework to use in my application.

I am currently using spring and jdk1.6 in my maven project for validating paths.

I may need to attach the error log(Exception details) if there are any exception accessing path or folder to the email being sent to groups.

Please advise appropriate email mechanism. Links to the exists code on the web or sample code is really helpful.

My team was saying they were using log4j framework to send email. But I am thinking why not use Spring. I don't know how to select best mechanism to do this supporting my requirement.

mahesh
  • 1,523
  • 4
  • 23
  • 39
  • 4
    Log4j to send emails with java? Sort of frightens me. – Dirk Lachowski Feb 05 '14 at 15:35
  • You may want to read this > http://stackoverflow.com/questions/46663/how-to-send-an-email-by-java-application-using-gmail-yahoo-hotmail?rq=1 – z atef Feb 05 '14 at 15:35
  • There is a log4j SMTP appender, but I don't know if you can have it send logs or whatever on a schedule. – Andrew Feb 05 '14 at 15:38
  • @Andrew afaik not, it's more of an option to send alerts for fatal errors to for example sys admins. You could create a custom appender that collates log messages and send them when certain triggers are met (like on timers, or when the number of messages reaches a set number), but that you'd have to code yourself. – jwenting Feb 05 '14 at 15:40

2 Answers2

1

well, many people use javaMail to send email in java

https://java.net/projects/javamail/pages/Home

of course, this is just the client side.

if you need also a mail server (maybe for testing purposes), I think apache james (https://james.apache.org/) is not that hard to install and use

here is some example of how to do it

http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

Leo
  • 6,480
  • 4
  • 37
  • 52
0

You can use Spring Mail for sending emails, it’s very easy and straightforward.

  1. Add these dependencies to your POM:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>...</version>
    </dependency>
    
    <dependency>
       <groupId>com.sun.mail</groupId>
       <artifactId>mailapi</artifactId>
       <version>1.5.0</version>
    </dependency>
    
    <dependency>
       <groupId>com.sun.mail</groupId>
       <artifactId>smtp</artifactId>
       <version>1.5.0</version>
    </dependency>
    
  2. Configure the Spring Mail beans:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    
        <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"
              p:defaultEncoding="utf-8"
              p:host="smtp.example.org"
              p:port="25" />
    
        <!-- This is a template message that we can pre-load with default state. -->
        <bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage"
              p:from="no-reply@example.org"
              p:subject="Testing message FTW!" />
    </beans>
    

    or if you prefer Java-based configuration (there’s written in Groovy):

    @Configuration
    class MailConfig {
    
        @Bean mailSender() {
            new JavaMailSenderImpl (
                defaultEncoding: 'utf-8',
                host: 'smtp.example.org',
                port: 25
            )
        }
    
         @Bean templateMessage() {
             new SimpleMailMessage (
                 from:    'no-reply@example.org',
                 subject: 'Testing message FTW!'
             )
         }
    }
    
  3. And finally send some email!

    @Slf4j
    public class EmailSenderExample {
    
       private MailSender mailSender;
       private SimpleMailMessage templateMessage;
    
       public void sendMail() {
           log.info("Sending email...");
    
           // Create a thread-safe "copy" of the template message and customize it.
           SimpleMailMessage msg = new SimpleMailMessage(templateMessage);
           msg.setTo("flynn@encom.com");
           msg.setText("Greetings, programs!");
    
           try {
                mailSender.send(msg);
           } catch (MailException ex) {
                log.warn("Email sending failed", ex);
           }
       }
    }
    
Jakub Jirutka
  • 10,269
  • 4
  • 42
  • 35