8

In RegistrationService, @Autowired registrationDao and registerCustomer work ok. But it cannot auto-wire ActivationMailService.

I didn't find the problem. It works when I remove ActivationMailService in RegistrationService. Could anybody tell me please what is the problem in my code?

I've uploaded the project to http://www.mediafire.com/?baum85us0rmmue3

I get this error below when I run the application:

Hibernate: drop table if exists Customer
ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'activationMailServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.game.portal.utils.SendMail com.game.portal.services.impl.ActivationMailServiceImpl.sendMail; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendMail': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl com.game.portal.utils.SendMail.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.game.portal.utils.SendMail com.game.portal.services.impl.ActivationMailServiceImpl.sendMail; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendMail': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl com.game.portal.utils.SendMail.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
    ... 22 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendMail': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl com.game.portal.utils.SendMail.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:912)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
    ... 24 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl com.game.portal.utils.SendMail.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
    ... 35 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:988)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
    ... 37 more
Jan 14, 2014 11:45:14 AM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'activationMailServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.game.portal.utils.SendMail com.game.portal.services.impl.ActivationMailServiceImpl.sendMail; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendMail': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl com.game.portal.utils.SendMail.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.game.portal.utils.SendMail com.game.portal.services.impl.ActivationMailServiceImpl.sendMail; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendMail': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl com.game.portal.utils.SendMail.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
    ... 22 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendMail': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl com.game.portal.utils.SendMail.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:912)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
    ... 24 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl com.game.portal.utils.SendMail.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
    ... 35 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:988)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
    ... 37 more

Manage bean:

package com.game.portal.managedbeans;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import com.game.portal.entities.Customer;
import com.game.portal.services.RegistrationService;

@ViewScoped
@ManagedBean
public class CustomerBean implements Serializable {

    private static final long serialVersionUID = 7291708796066664438L;

    @ManagedProperty(value="#{registrationService}")
    RegistrationService registrationService;

    Customer customer = new Customer();

    public void registerCustomer(){
        customer.setName("xxx");
        customer.setSurname("xxx");
        customer.setUsername("xxx");
        customer.setEmail("xxx@yahoo.com");
        customer.setPassword("xxxx");

        registrationService.registerCustomer(customer);

    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public RegistrationService getRegistrationService() {
        return registrationService;
    }

    public void setRegistrationService(RegistrationService registrationService) {
        this.registrationService = registrationService;
    }

}

Here is RegistrationService

package com.game.portal.services;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.game.portal.dao.RegistrationDao;
import com.game.portal.entities.Company;
import com.game.portal.entities.Customer;

@Service
public class RegistrationService {

    @Autowired
    RegistrationDao registrationDao;

    @Autowired
    ActivationMailService activationMailService;

    public void registerCustomer(Customer customer) {

        System.out.println("RegistrationUserBean:: Registering user " + customer.getName() + " " + customer.getUsername() + ", with email " + customer.getEmail());
        registrationDao.registerCustomer(customer);
        activationMailService.sendActivationMail(customer);

        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Registration success", "success");  
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void registerCompany(Company company) {

        System.out.println("RegistrationUserBean:: Registering user " + company.getName() + " " + company.getUsername() + ", with email " + company.getEmail());
        registrationDao.registerCompany(company);

        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Registration success", "success");  
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

}

ActivationMailService

Note: I tested removing the @Autowired on SendMail, HashMap and EmailTemplateService. It works. There is no problem with @Autowired on ActivationCodeService. It is really strange.

package com.game.portal.services;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.game.portal.entities.Customer;
import com.game.portal.utils.SendMail;

@Service
public class ActivationMailService {

    @Autowired
    protected ActivationCodeService activationCodeService;  
    @Autowired
    protected SendMail sendMail;
    @Autowired
    protected HashMap<String, String> emailMap;
    @Autowired
    private EmailTemplateService mailTemplateService;

    protected static final Logger logger = Logger.getLogger("ActivationMailService");   

    public void sendActivationMail(Customer customer) {
        try {
            sendMail.send("test", "test", customer.getEmail(), createActivationMailMessage(customer));
        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage(), e);
        }
    }

    private String createActivationMailMessage(Customer customer) throws UnsupportedEncodingException {
        String emailTemplate = mailTemplateService.generateTemplate("activation", emailMap.get("domain_name"));

        return emailTemplate;
    }
}

SendMail.class

import java.util.ArrayList;
import java.util.HashMap;

import javax.activation.FileDataSource;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

@Lazy(true)
@Scope("singleton")
@Service
public class SendMail {

    private static Logger logger = Logger.getLogger(SendMail.class);

    @Autowired
    JavaMailSenderImpl mailSender;

    @Autowired
    private HashMap<String, String> emailMap;

    public boolean send(String subject, String from, String[] to, String message) {
        boolean sendSuccessfull = true;

        MimeMessage mailMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mailMessage, "UTF-8");
        try {
            helper.setFrom(from, emailMap.get("fromName"));
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(message, true);
            mailSender.send(mailMessage);
            logger.warn("SEND MAIL TO : " + to + " WITH SUBJECT " + subject
                + " OK ");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            sendSuccessfull = false;
        }

        return sendSuccessfull;
    }

    public boolean send(String subject, String from, String to, String message) {   
        boolean sendSuccessfull = true;

        MimeMessage mailMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mailMessage, "UTF-8");
        try {
            helper.setFrom(from, emailMap.get("fromName"));
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(message, true);
            mailSender.send(mailMessage);
            logger.warn("SEND MAIL TO : " + to + " WITH SUBJECT " + subject
                + " OK ");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            sendSuccessfull = false;
        }

        return sendSuccessfull;
    }

    public boolean send(String subject, String from, String[] to, String[] cc, String message) {
        boolean sendSuccessfull = true;

        MimeMessage mailMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mailMessage, "UTF-8");
        try {
            helper.setFrom(from, emailMap.get("fromName"));
            helper.setTo(to);
            helper.setCc(cc);
            helper.setSubject(subject);
            helper.setText(message, true);
            mailSender.send(mailMessage);
            logger.warn("SEND MAIL TO : " + to + " WITH SUBJECT " + subject
                + " OK ");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            sendSuccessfull = false;
        }

        return sendSuccessfull;
    }

    public boolean send(String subject, String from, String to, String cc, String message) {
        boolean sendSuccessfull = true;

        MimeMessage mailMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mailMessage, "UTF-8");
        try {
            helper.setFrom(from, emailMap.get("fromName"));
            helper.setTo(to);
            helper.setCc(cc);
            helper.setSubject(subject);
            helper.setText(message, true);
            mailSender.send(mailMessage);
            logger.warn("SEND MAIL TO : " + to + " WITH SUBJECT " + subject
                + " OK ");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            sendSuccessfull = false;
        }

        return sendSuccessfull;
    }

    public boolean send(String subject, String from, String[] to, String[] cc,
        String message, ArrayList<String> attachmentfilenamelist,
        ArrayList<String> attachmentfilewithpathlist) {
        boolean sendSuccessfull = true;

        MimeMessage mailMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper;
        try {
            helper = new MimeMessageHelper(mailMessage, true, "UTF-8");
            helper.setFrom(from, emailMap.get("fromName"));
            helper.setTo(to);
            helper.setCc(cc);
            helper.setSubject(subject);
            helper.setText(message, true);
            for (int i = 0; i < attachmentfilenamelist.size(); i++) {
            helper.addAttachment(attachmentfilenamelist.get(i),
                new FileDataSource(attachmentfilewithpathlist.get(i)));
            }
            mailSender.send(mailMessage);
            logger.warn("SEND MAIL TO : " + to + " WITH SUBJECT " + subject
                + " OK ");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            sendSuccessfull = false;
        }

        return sendSuccessfull;
    }

    public boolean sendFromUser(String subject, String from, String fromName, String []to, String message) {
        boolean sendSuccessfull = true;

        MimeMessage mailMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mailMessage, "UTF-8");
        String toAdresses = "";
        for (int i = 0; i < to.length; i++) {
            if(i == 0)
                toAdresses = to[i];
            else
                toAdresses = toAdresses + ", " + to[i];
        }
        try {
            helper.setFrom(from, fromName);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(message, true);
            mailSender.send(mailMessage);
            logger.warn("SEND MAIL TO : " + toAdresses + " WITH SUBJECT " + subject
                + " OK ");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            sendSuccessfull = false;
        }

        return sendSuccessfull;
    }
}
abhi
  • 1,760
  • 1
  • 24
  • 40
node
  • 121
  • 1
  • 1
  • 8
  • 1
    You might want to set the log level to DEBUG or lower to figure out why Spring is not creating your bean. Also I don't see the `import org.springframework.stereotype.Service;` in `ActivationMailService` but you do use that annotation. Did you add that to this post only? If so, that's why it doesn't work. – Giovanni Botta Jan 13 '14 at 17:24
  • 1
    I think there's more to that NoSuchBeanException. It's impossible to tell without the full stacktrace. However, you have a suspicious @Autowired annotation on a HashMap inside your ActivationService. That autowired annotation will most likely behave differently than you expect. Where is the definition for that? – hyness Jan 13 '14 at 17:28
  • @Giovanni, It is copy paste mistake. I added import org.springframework.stereotype.Service. It is not the cause of error. – node Jan 13 '14 at 18:44
  • @Hyness, I will post full stactrace tomorrow. – node Jan 13 '14 at 18:46
  • I'd also like to see your web.xml to know what's loading what. – Sotirios Delimanolis Jan 13 '14 at 19:17
  • @Sotirios, I added web.xml – node Jan 13 '14 at 19:24
  • Why you inject an HashMap? And where you define it for autowire? – gipinani Jan 13 '14 at 20:39
  • @mserioli , Actually I dont need HashMap, I used just to test it. I got same error when I remove autowire HashMap aswell.. I add SendMail class. – node Jan 13 '14 at 20:51
  • Please double check that your `ActivationMailService` class is as you've posted it. Also, add CGLIB to your Maven dependencies. – Sotirios Delimanolis Jan 13 '14 at 21:00
  • @Sotirious , I added CGLIB in pom file but nothing happened. I've uploaded the project at https://www.mediafire.com/?baum85us0rmmue3 – node Jan 14 '14 at 09:54
  • You should be programming and instead of `JavaMailSenderImpl` and `HashMap` use `JavaMailSender` and `Map` . – M. Deinum Jan 14 '14 at 11:43
  • @M.Deinum , Thanks, I tried but nothing happened. – node Jan 14 '14 at 12:50
  • 1
    I don't see any `JavaMailSenderImpl` bean definitions in your context files. – Sotirios Delimanolis Jan 14 '14 at 13:37

3 Answers3

8

Based on exception:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl]

it seems that Spring can't find bean to be injected into

@Autowired
JavaMailSenderImpl mailSender;

in SendMail class.

Are you sure there is a bean of class JavaMailSenderImpl defined?

BTW It's a little bit unusual that an implementation class is injected directly (and not its interface).

Adam Dyga
  • 8,666
  • 4
  • 27
  • 35
  • I changed it but still same error; @Autowired JavaMailSender mailSender; – node Jan 14 '14 at 13:04
  • @user1662738 but did you check if there is any bean defined which implements `JavaMailSender` ? I can't see any in the code you posted. – Adam Dyga Jan 14 '14 at 13:57
  • 2
    Just had a similar issue - it was because I was trying to autowire the implementation and not the interface. I think that using the @Transactional annotation Spring creates proxies so that you don't always have what you think you have. Switching to autowiring the interface fixed it for me. – pillingworth May 09 '14 at 09:21
  • 1
    I just had a similar issue (injecting the implementation and not the interface) and you finally opened my eyes to the error in my ways. THANK YOU. No idea how I could ever not see that. – deucalion Jul 13 '14 at 20:25
1

The methods in the Service class should be annotated @Transactional

user2791841
  • 71
  • 1
  • 9
0

write @Service("sendMail") at your autowired class

Guruputra
  • 3
  • 2