Announcement Announcement Module
Collapse
No announcement yet.
Spring Mail (gmail) Annotation Configuration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Mail (gmail) Annotation Configuration

    I'm Getting this error because for some reason my mail configuration won't wire up to my java class. I want to do it the annotation way.

    Code:
    java.lang.IllegalArgumentException: The 'original' message argument cannot be null
    applicationContext.xml

    Code:
    <!-- ========================= GMAIL DEFINITIONS ========================= -->
    
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    	<property name="host" value="smtp.gmail.com" />
    	<property name="port" value="465" />
    	<property name="protocol" value="smtps" />
    	<property name="username" value="[email protected]" />
    	<property name="password" value="xxxxxxx" />
    	<property name="javaMailProperties">
    		<props>
    			<prop key="mail.smtps.auth">true</prop>
    			<prop key="mail.smtps.starttls.enable">true</prop>
    			<prop key="mail.smtps.debug">true</prop>
    		</props>
    	</property>
    </bean>
    
    <bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
    	<property name="from" value="xxxxxx@gm[email protected]" />
    	<property name="subject" value="Register" />
    </bean>
    springmvc-servlet.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
    	  default-autowire="byName">
    
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 
    
    <context:spring-configured/>
    <context:annotation-config/>
    <context:component-scan base-package="com.xxxxxx" />
    
    <!--  View Resolver -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    Java Class

    Code:
    package com.xxxxxxx.email;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Configurable;
    import org.springframework.mail.MailException;
    import org.springframework.mail.MailSender;
    import org.springframework.mail.SimpleMailMessage;
    
    import com.xxxxxxx.domain.SignUpAccount;
    
    @Configurable
    public class RegisterEmail{
    
        private transient MailSender mailSender;
        private transient SimpleMailMessage templateMessage;
        
        @Autowired
        public void setMailSender(MailSender mailSender) {
            this.mailSender = mailSender;
        }
        @Autowired
        public void setTemplateMessage(SimpleMailMessage templateMessage) {
            this.templateMessage = templateMessage;
        }
    
        public void sendRegisterEmail(SignUpAccount signUpAccount) {
    
            SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
            msg.setTo(signUpAccount.getEmail());
            msg.setText("Wow u registered");
            try{
                this.mailSender.send(msg);
            }
            catch(MailException ex) {
                // simply log it and go on...
                System.err.println(ex.getMessage());            
            }
        }
    }
    This example is taken from Springs official blog explaining how to do it.
    http://blog.springsource.com/2008/01...ction-feature/

    Any feedback?
    Thanks

  • #2
    Again trying to get this to work I follow Springs online documentation
    and it still won't work?

    http://static.springframework.org/sp...ence/mail.html

    In debugging I get
    Code:
    RegisterEmailImpl.sendRegisterEmail(SignUpAccount) line: 30 (out of synch)	
    
    mailSender and templateMessage are null
    
    and again
    
    java.lang.IllegalArgumentException: The 'original' message argument cannot be null
    org.springframework.util.Assert.notNull(Assert.java:112)
    org.springframework.mail.SimpleMailMessage.<init>(SimpleMailMessage.java:73)
    	com.xxxxx.email.RegisterEmailImpl.sendRegisterEmail(RegisterEmailImpl.java:30)

    Now the code has full XML configuration exactly like the Spring documentation

    Code:
    <!-- ========================= GMAIL DEFINITIONS ========================= -->
    
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    	<property name="host" value="smtp.gmail.com" />
    	<property name="port" value="465" />
    	<property name="protocol" value="smtps" />
    	<property name="username" value="[email protected]" />
    	<property name="password" value="xxxxxx" />
    	<property name="javaMailProperties">
    		<props>
    			<prop key="mail.smtps.auth">true</prop>
    			<prop key="mail.smtps.starttls.enable">true</prop>
    			<prop key="mail.smtps.debug">true</prop>
    		</props>
    	</property>
    </bean>
    
    <bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
    	<property name="from" value="[email protected]" />
    	<property name="subject" value="Register" />
    </bean>
    
    <bean id="registerEmail" class="com.xxxxxx.email.RegisterEmailImpl">
            <property name="mailSender"><ref bean="mailSender" /></property>
            <property name="templateMessage"><ref bean="templateMessage" /></property>
    </bean>

    The interface (RegisterEmail.java)
    Code:
    package com.xxxxx.email;
    
    import com.xxxxxx.domain.SignUpAccount;
    
    public interface RegisterEmail {
    
    	void sendRegisterEmail(SignUpAccount signUpAccount);
    }
    The implementation (RegisterEmailImpl.java)
    Code:
    package com.xxxxxx.email;
    
    import org.springframework.mail.MailException;
    import org.springframework.mail.MailSender;
    import org.springframework.mail.SimpleMailMessage;
    
    import com.xxxxxx.domain.SignUpAccount;
    
    public class RegisterEmailImpl implements RegisterEmail{
    
        private MailSender mailSender;
        private SimpleMailMessage templateMessage;
        
    
        public void setMailSender(MailSender mailSender) {
            this.mailSender = mailSender;
        }
    
        public void setTemplateMessage(SimpleMailMessage templateMessage) {
            this.templateMessage = templateMessage;
        }
    
        public void sendRegisterEmail(SignUpAccount signUpAccount) {
    
            // Do the business calculations...
    
            // Call the collaborators to persist the order...
    
            // Create a thread safe "copy" of the template message and customize it
            SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
            msg.setTo(signUpAccount.getEmail());
            msg.setText("Wow u registered");
            try{
                this.mailSender.send(msg);
            }
            catch(MailException ex) {
                // simply log it and go on...
                System.err.println(ex.getMessage());            
            }
        }
    }

    Any idea? I just can't figure out why the xml isn't being injected into the RegisterEmailImpl?

    Thanks

    Comment


    • #3
      How are you weaving in the aspects?

      -Ramnivas

      Comment


      • #4
        Well...

        I wasn't as I don't really know AOP, and didn't know its was required from
        only finding your mail implementation.

        But now having read up on it. From Apress's Pro Spring 2.5 and the spring
        @Configurable documentation it seems like I have to weave in the configuration statically or dynamically.

        Yet i don't know how I can go about doing this.

        Do I have to make on the bean depend on aspect? (If so where should this go in?)
        Code:
        depends-on="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect"
        Also they talk about ProxyFactory. But this is within the classes

        AOP is quite new to me, but I'm try to learn

        Any advice?
        Thanks

        Comment

        Working...
        X