Announcement Announcement Module
Collapse
No announcement yet.
Mail sender not working! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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="[email protected]" />
    		<property name="cc" value="[email protected]" />
    	</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
    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.

    Comment


    • #3
      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

      Comment


      • #4
        Hi Paary,
        I know this may not be the correct forum,but i am getting same expection Intial Access check failure with simple java code, no link in google provided me exact solution.
        May i know how you could solved this. I am trying in my local machine with DEV db in company network ,
        It would very much greatful if you give correct solution, i know its been long back you got above error.

        Comment

        Working...
        X