title: How to send email asynchronously using spring 4 @Async
body: <p>I know this question has been asked but I am not able to send email using configuration. I don't know what I am doing wrong and why I am not getting the email. Here is my Spring configuration.</p>
  
  <pre><code>@Configuration
  @PropertySource(value = { 
      "classpath:autoalert.properties"
  })
  @EnableAsync
  @Import({PersistenceConfig.class, EmailConfig.class, VelocityConfig.class})
  @ComponentScan(basePackageClasses = { 
      ServiceMarker.class,
      RepositoryMarker.class }
  )
  public class AutoAlertAppConfig {
  
      @Bean
      public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
          return new PropertySourcesPlaceholderConfigurer();
      }
  }
  </code></pre>
  
  <p>Here is my email config</p>
  
  <pre><code>@Configuration
  @PropertySources({
  @PropertySource("classpath:email/email.properties")
  })
  public class EmailConfig {
  
      @Autowired
      private Environment env;
  
      @Bean
      public JavaMailSender mailSender() {
           JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
           mailSender.setSession(getEmailSession());
           return mailSender;
       }
  
       @Bean
       public MailMessage mailSettings() {
           SimpleMailMessage mailMessage = new SimpleMailMessage();
           mailMessage.setFrom(env.getProperty("mail.from"));
           ...
           mailMessage.setText(env.getProperty("mail.body"));
           return mailMessage;
       }
  
      private Session getEmailSession() {
  
           Properties emailProperties = SpringUtil.loadPropertiesFileFromClassPath("email" + File.separator + "general-mail-settings.properties");
  
           final String userName = emailProperties.getProperty("user");
           final String password = emailProperties.getProperty("password");
  
          Session session = null;
          try {
  
              session = Session.getInstance(emailProperties, new javax.mail.Authenticator() {
              protected PasswordAuthentication getPasswordAuthentication() {
                      return new PasswordAuthentication(userName, password);      
                  }       
              });  //end of anonymous class       
          } catch (Exception e) {
              e.printStackTrace();
          }
          return session;
  
      } //end of getEmailSession()
  
  }
  </code></pre>
  
  <p>Here is my class</p>
  
  <pre><code>public static void main(String[] args) { 
  
      try (GenericApplicationContext springContext = new AnnotationConfigApplicationContext(AutoAlertAppConfig.class)) {
          AutoAlertService autoAlertService = springContext.getBean(AutoAlertServiceImpl.class);
          try {
              autoAlertService.handleProcess(fromDate, toDate);
          } catch (Exception e) {
              logger.error("Exception occurs", e);
              autoAlertService.handleException(fromDate, toDate, e);
          }
      } catch (Exception e) {
          logger.error("Exception occurs in loading Spring context: ", e);
      }
  
  @Service
  public class AutoAlertServiceImpl implements AutoAlertService {
  
      @Inject
      private AsyncEmailService asyncEmailService;
  
      @Override
      public void handleProcess(String fromDate, String toDate) {
          logger.info("Start process");
          try {
              ..
              //Sending email 
              asyncEmailService.sendMailWithFileAttachment(fromDate, toDate, file);
          } catch (Exception e) {
              handleException(fromDate, toDate, e);
          }
          logger.info("Finish process");
      }
  }
  </code></pre>
  
  <p>here is my email service</p>
  
  <pre><code>@Component
  public class AsyncEmailServiceImpl implements AsyncEmailService {
  
      @Resource(name="mailSender")
      private JavaMailSender mailSender;
  
      @Resource(name="mailSettings")
      private SimpleMailMessage simpleMailMessage;
  
      @Async
      @Override
      public void sendMailWithFileAttachment(String from, String to, String attachFile) {
          logger.info("Start execution of async. Sending email with file attachment");
          MimeMessage message = mailSender.createMimeMessage();
          try{
              MimeMessageHelper helper = new MimeMessageHelper(message, true);
              ....
              helper.setText(String.format(simpleMailMessage.getText(), from, to));
              FileSystemResource file = new FileSystemResource(attachFile);
              helper.addAttachment(file.getFilename(), file);
              mailSender.send(message);
          } catch (MessagingException e) {
              logger.error("Exception occurs in sending email with file attachment: " + attachFile, e);
              throw new MailParseException(e);
          }
          logger.info("Complete execution of async. Email with file attachment " + attachFile + " send successfully.");
  
      }
  }
  </code></pre>
  
  <p>When I run the code it comes to method. This is printed on the console</p>
  
  <pre><code>13:59:43.004 [main] INFO  com.softech.vu360.autoalert.service.impl.AutoAlertServiceImpl - Finish process
  13:59:43.005 [SimpleAsyncTaskExecutor-1] INFO  com.softech.vu360.autoalert.service.impl.AsyncEmailServiceImpl - Start execution of async. Sending email with file attachment
  13:59:43.007 [main] INFO  com.softech.vu360.autoalert.AutoAlert - Exiting application.
  </code></pre>
  
  <p>But I get no email. In case of Synchronous calling I get the email. Why I am not getting email ? Am I doing something wrong ?</p>
  
  <p>Thanks</p>