Announcement Announcement Module
Collapse
No announcement yet.
spring mail application Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring mail application

    Hi all,
    I am new to spring.I am doing mail application in spring and i want to do console application only.
    here is the code of applicationContext.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC
    "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
      <property name="host"><value>mail.2xprime.com</value></property>
    </bean>
    <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
      <property name="from"><value>[email protected]</value></property>
      <property name="subject"><value>TEst Mail</value></property>
    </bean>
    <bean id="orderSend" class="com.prime.maileg.OrderSend">
      <property name="mailSender"><ref bean="mailSender"/></property>
      <property name="message"><ref bean="mailMessage"/></property>
    </bean>
    </beans>
    And this is the application OrderSend.java
    Code:
    public class OrderSend {
    	static private MailSender mailSender;
        static private SimpleMailMessage message;
    
        public void setMailSender(MailSender mailSender) {
            OrderSend.mailSender = mailSender;
        }
    
        public void setMessage(SimpleMailMessage message) {
            OrderSend.message = message;
        }
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		//Resource     res = new  ClassPathResource("application-context.xml");
            //BeanFactory  factory = new  XmlBeanFactory(res);
    		 SimpleMailMessage msg = new SimpleMailMessage(OrderSend.message);
    	        msg.setTo("[email protected]");
    	        msg.setText("test mail");
    	        try{
    	            mailSender.send(msg);
    	        }
    	        catch(MailException ex) {
    	            //log it and go on
    	            System.err.println(ex.getMessage());
    	        }
    	}
    }
    and my folder structure of the application is:
    <dir>com.prime.maileg(Package)
    OrderSend.java
    lib
    spring.jar
    applicationContext.xml
    My application is compiling fine.but while running it is giving the following error:
    Code:
    Exception in thread "main" java.lang.IllegalArgumentException: The 'original' message argument cannot be null
    	at org.springframework.util.Assert.notNull(Assert.java:112)
    	at org.springframework.mail.SimpleMailMessage.<init>(SimpleMailMessage.java:73)
    	at com.prime.maileg.OrderSend.main(OrderSend.java:29)
    Pleaese help me ...
    Thanks in advance
    Suresh

  • #2
    (Note: This probably shouldn't be in the Spring WebFlow forum, but...)

    Hi Suresh,

    Here is the flow of logic that you should be using:
    1. Load your applicationContext.xml with a BeanFactory (looks like you were heading in this direction, but commented it out?).
    2. Retrieve the bean you're interested in ("orderSend") from the BeanFactory. This instance will be fully configured as per the applicationContext.xml.
    3. Create your SimpleMailMessage as you've done, but instead of accessing a static property, you should be calling orderSend.getMessage(), since this will give you access to the SimpleMailMessage injected into the orderSend bean by Spring.

    Here's the sort of thing (untested):
    Code:
    public class OrderSend {
    
        private MailSender mailSender;
        private SimpleMailMessage message;
    
        public void setMailSender(MailSender mailSender) {
            this.mailSender = mailSender;
        }
    
        public void setMessage(SimpleMailMessage message) {
            this.message = message;
        }
    
        public SimpleMailMessage getMessage() {
            return this.message;
        }
    
        public static void main(String[] args) {
            // Use Spring to load the beans.
            BeanFactory beanFactory = new ClassPathXmlApplicationContext("application-context.xml");
    
            // The orderSend bean is the one we're interested in...
            OrderSend orderSend = (OrderSend)beanFactory.getBean("orderSend");
    
            // ...so that we can build this SimpleMailMessage.
            SimpleMailMessage msg = new SimpleMailMessage(orderSend.getMessage());
    
            // Configure the message.
            msg.setTo("[email protected]");
            msg.setText("test mail");
    
            // And fire it off!
            try{
                mailSender.send(msg);
            }
            catch(MailException ex) {
                //log it and go on
                System.err.println(ex.getMessage());
            }
        }
    }
    Hope this helps you out!

    Comment


    • #3
      spring mail application

      Thank you very much Chris for your quick response,
      After giving your valuable suggestion i changed my code as below
      OrderSend.java
      Code:
      package com.prime.maileg;
      import org.springframework.mail.MailSender;
      import org.springframework.mail.SimpleMailMessage;
      import org.springframework.mail.MailException;
      import org.springframework.beans.factory.*;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      public class OrderSend {
      	private MailSender mailSender;
          private SimpleMailMessage message;
      
          public void setMailSender(MailSender mailSender) {
              this.mailSender = mailSender;
          }
          public void setMessage(SimpleMailMessage message) {
              this.message = message;
          }
          public SimpleMailMessage getMessage(){
          	return this.message;
          }
          public MailSender getMailSender(){
          	return this.mailSender;
          }
      	public static void main(String[] args) {
      		// TODO Auto-generated method stub
      		BeanFactory beanfactory=new ClassPathXmlApplicationContext("application-context.xml");
      		OrderSend osend=(OrderSend)beanfactory.getBean("orderSend");
      		SimpleMailMessage msg = new SimpleMailMessage(osend.getMessage());
      	    msg.setTo("[email protected]");
      	    msg.setText("test mail");
      	    try{
      	        osend.getMailSender().send(msg);
      	        }
      	    catch(MailException ex) {
      	       System.err.println(ex.getMessage());
      	        }
      	}
      }
      It was compiled well but while running it is giving the follwing Error:
      Code:
      Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
      	at org.springframework.util.ClassUtils.<clinit>(ClassUtils.java:73)
      	at org.springframework.core.io.DefaultResourceLoader.<init>(DefaultResourceLoader.java:52)
      	at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:199)
      	at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:84)
      	at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.<init>(AbstractRefreshableConfigApplicationContext.java:59)
      	at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:58)
      	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:136)
      	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
      	at com.prime.maileg.OrderSend.main(OrderSend.java:32)
      My folder structure was
      <rootDir>com.prime.maileg(Package)\OrderSend.jav a
      <rootDir>lib\spring.jar
      <rootDir>application-context.xml
      I know that its a silly error, please help me in this issue.....

      --With Thanks Suresh--

      Comment


      • #4
        Hey,

        This is an error you'll come up with a lot on your travels with Java development. Since Spring makes use of a number of other libraries itself, it needs access to them at runtime to function properly. You provide access to these libraries by including them on your classpath. If you download the '-with-dependencies' distribution of Spring (from here: http://sourceforge.net/project/showf...ease_id=578115), you'll find all of the libraries you need in the 'lib' subfolder. You can put these in the same folder as you've put spring.jar and it should be sorted.

        You'll soon find that having to do this sort of thing is irritating (it doesn't just apply to Spring, but pretty much -any- other library you use). The task of making sure the correct JARs are on your classpath (and the correct versions of those JARs) is called "dependency management." Doing it by hand is something that you definitely need to do for a while, but sooner or later you'll get really sick with it, understand it completely and be ready to start looking to other tools.

        It's really worth reading up on using Maven 2 for project and dependency management - it's used for most available libraries in Java and will handle all of this dependency juggling for you (amongst a dozen other things). Maven 2 has a lot of criticism - and some will recommend you use Apache Ant instead (in conjunction with Ivy, most likely) - but it really is the best we have right now and you'll do well to learn how it works and how to use it.

        Cheers.

        Comment


        • #5
          spring mail application

          hi chris,
          I downloaded all dependencies also i downloaded mail.jar and actiavation.jar
          now its working..
          Thanks for your cooperation.

          cheers
          --Suresh

          Comment

          Working...
          X