Results 1 to 3 of 3

Thread: Mail sender not working!

  1. #1
    Join Date
    May 2011
    Location
    Chennai,India
    Posts
    38

    Unhappy Mail sender not working!

    Hi all,
    I have a requirement to implement a mail sender in our application. Being a fan of Spring framework, i wanted to use the mail sender provided by Spring. Well actually, i started with the small example provided in the documents and i was able to configure and test for its working in my personal system with my gmail account.

    But, i'm not able to do the same in my office network and my official mail id's. I could only say that we have a firewall at my office. I couldn't find any other difference
    so do we need to add any other configuration to by-pass the firewall? any help would be great. I appreciate the same. anyways, here is the configuration that i'm trying,
    Code:
    <!-- Configuration for mail Manager - START -->
    	<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    		<property name="host" value="myhost" />
    		<property name="port" value="25" />
    		<property name="protocol" value="smtp" />
    		<property name="username" value="username" />
    		<property name="password" value="password" />
    		<property name="javaMailProperties">
    			<props>
    				<prop key="mail.smtp.auth">true</prop>
    				<prop key="mail.smtp.starttls.enable">true</prop>
    				<prop key="mail.smtp.ssl.enable">true</prop>
    			</props>
    		</property>
    	</bean>
    	<!--
    		this is a template message that we can pre-load with default state
    	-->
    	<bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
    		<property name="from" value="Paary" />
    		<property name="subject" value="Test Email" />
    		<property name="to" value="paary@abc.com" />
    		<property name="cc" value="manoj@abc.com" />
    	</bean>
    
    	<bean id="mailManager" class="com.mail.test.MailManagerTest">
    		<property name="mailSender" ref="mailSender" />
    		<property name="templateMessage" ref="templateMessage" />
    	</bean>
    
    	
    	<!-- Configuration for mail Manager - END -->
    and here is my implementation,
    Code:
    public class MailManagerTest {
    	private MailSender mailSender;
    	private SimpleMailMessage templateMessage;
    
    	
    	public void setMailSender(MailSender mailSender) {
    		this.mailSender = mailSender;
    	}
    
    	public void setTemplateMessage(SimpleMailMessage templateMessage) {
    		this.templateMessage = templateMessage;
    	}
    	public SimpleMailMessage getTemplateMessage() {
    		return templateMessage;
    	}
    	
    	public static void main(String args[]){
    		System.out.println("MailManager Start");
    		MailManagerTest mailTest=new MailManagerTest();
    		System.out.println("Calling the sendEmail()");
    		mailTest.SendEmail();
    		System.out.println("MailManager End");
    	}
    	
    	public void SendEmail(){		
    		ApplicationContext context=new ClassPathXmlApplicationContext("springconfig.xml");
    		MailManagerTest mailManager=(MailManagerTest) context.getBean("mailManager");
    		SimpleMailMessage mailMessage=new SimpleMailMessage(mailManager.getTemplateMessage());	
    		mailMessage.setText("This is a test email.Please igmore.");
    		System.out.println(mailMessage);
    		try{
    
    			mailSender.send(mailMessage);
    		}
    		catch(MailException me){
    			System.out.println("Cannot send email");			
    			System.out.println(me);
    		}	
    		catch (Exception e) {
    			System.out.println("could not send email");
    			System.out.println(e.getMessage());
    			e.printStackTrace();
    		}
    	}
    }
    I'm getting NPE
    This is the stack trace that i got in console,
    java.lang.NullPointerException
    at com.mail.test.MailManagerTest.SendEmail(MailManage rTest.java:42)
    at com.mail.test.MailManagerTest.main(MailManagerTest .java:29)

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Your code is wrong...

    Code:
    public void SendEmail(){		
    		ApplicationContext context=new ClassPathXmlApplicationContext("springconfig.xml");
    		MailManagerTest mailManager=(MailManagerTest) context.getBean("mailManager");
    		SimpleMailMessage mailMessage=new SimpleMailMessage(mailManager.getTemplateMessage());	
    		mailMessage.setText("This is a test email.Please igmore.");
    		System.out.println(mailMessage);
    		try{
    
    			mailSender.send(mailMessage);
    		}
    		catch(MailException me){
    			System.out.println("Cannot send email");			
    			System.out.println(me);
    		}	
    		catch (Exception e) {
    			System.out.println("could not send email");
    			System.out.println(e.getMessage());
    			e.printStackTrace();
    		}
    	}
    This isn't going to work as it will always be null in your current instance... You should use the mailManager class configured in the application context to send the email.

    Code:
    public class MailManagerTest {
    	private MailSender mailSender;
    	private SimpleMailMessage templateMessage;
    	
    	public void setMailSender(MailSender mailSender) {
    		this.mailSender = mailSender;
    	}
    
    	public void setTemplateMessage(SimpleMailMessage templateMessage) {
    		this.templateMessage = templateMessage;
    	}	
    	
    	public static void main(String args[]){
    		System.out.println("MailManager Start");
    		ApplicationContext context=new ClassPathXmlApplicationContext("springconfig.xml");
    		MailManagerTest mailManager=(MailManagerTest) context.getBean("mailManager");
    		System.out.println("Calling the sendEmail()");
    		mailManager.SendEmail();
    		System.out.println("MailManager End");
    	}
    	
    	public void SendEmail(){		
    		SimpleMailMessage mailMessage=new SimpleMailMessage(templateMessage);	
    		mailMessage.setText("This is a test email.Please igmore.");
    		System.out.println(mailMessage);
    		try{
    			mailSender.send(mailMessage);
    		} catch(MailException me){
    			System.out.println("Cannot send email");			
    			System.out.println(me);
    		} catch (Exception e) {
    			System.out.println("could not send email");
    			System.out.println(e.getMessage());
    			e.printStackTrace();
    		}
    	}
    }
    Something along these lines should work.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    May 2011
    Location
    Chennai,India
    Posts
    38

    Default

    Marten,
    Thanks a ton i don't know how i overlooked it! the NPE exception is gone and I'm getting some other exception (something not related to Spring) "Initial Access check failure". I will work on it.

    Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •