^ Click Here

Sunday, August 26, 2012

Sending Simple Mail through Java (Spring)

Seriously, there are plenty of examples for this available on net and mine isn't any different in that respect (well not this one). So here we go...

I am using a maven project but you can do it without that also though it would be bit pain to download the jar by yourself and putting them in buildpath.....still the concept that matters.

so first of all the dependency needed to be added is (I assume there is already a spring core, context and junit test dependency available).

<!-- Java Mail API -->
    <dependency>
     <groupId>javax.mail</groupId>
     <artifactId>mail</artifactId>
     <version>1.4.5</version>
    </dependency>

Secondly, since I am using maven and spring, I would define a bean in the Spring configuration xml file.

<bean class="org.springframework.mail.javamail.JavaMailSenderImpl" id="mailSender">
    <property name="host" value="smtp.gmail.com">
    <property name="port" value="587">
    <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>
       </props>
    </property>
</bean>

Now our java mail sender bean is ready (I don't need to tell that put your username and password value in the properties) we have define a gmail host smtp.gmail.com and port is 587 because that's the port gmail uses for smtp (though 25 is a default port for smtp). also we need to turn on few properties like auth and tls. but that's about configuration.

Now we will prepare for using this mailSender bean to send a simple message.

So here is my class for that -

package simpleSendMail;
 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;
 
@Component
public class SendMail
{
    @Autowired
    private MailSender mailSender;
 
 public void sendMail(String from, String[] to, String subject, String msg) {
  SimpleMailMessage message = new SimpleMailMessage();
  message.setFrom(from);
  message.setTo(to);
  message.setSubject(subject);
  message.setText(msg);
  mailSender.send(message); 
 }
}

As you see I have autowired the mailSender bean to be used here, instead of it you very well can define this class as a bean and in xml configuration file link mailSender bean to the property of this one....if that is the case don't forget to put a setter......maybe this will help.

So as you can see there is a method which takes parameter for from, to addresses and subject and text to be sent then initiates the object of SimpleMessage class , sets the value and with our mailSender it sends that SimpleMessage

below is the Test class for that.

package simpleSendMail;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SendMailTest {

  @Test 
 public void mailTest(){
    ApplicationContext context = new ClassPathXmlApplicationContext("simpleSendMail/SpringMail.xml");

       SendMail mm = (SendMail) context.getBean("sendMail");
           mm.sendMail("sender@gmail.com",
        new String[]{"recepient1@gmail.com","recepient2@rediffmail.com"},
        "Hello Friend", 
        "This is a test mail");
 } 
}

Again in test method you will provide valid mail addresses for to and from option

This might( and most surely) would fail if you are behind some proxy or firewall which doesn't let you use the specified port directly and blocks it considering it a threat. (Solution? I am still trying to find out.)

Thursday, August 9, 2012

[fixed] jQuery UI datepicker showing calendar when setting date.

For showing, choosing date jQuery UI datepicker is a great plugin, it provides a lot of functionality. However I feel it's little short on events, yet enough to fulfill almost all need of a basic application.

I was using that however I found an issue......with a few logic I was updating a date field which have a date picker when another field with datepicker was updated.

Problem : the primary datepicker was is visible and secondary is well say gets visible only when user wants. Since I was updating the secondary even when it was hidden.....it popped a calendar at the bottom of the page which of course was an annoying issue since it didn't use to disappear until and unless user clicks on some other datepicker enabled field.

I figured it out that the problem was with datepicker setDate function...I was using

$('.date-pick').datepicker('setDate', newDate)  //where date is a js date object.

I found that there is a hide function in datepicker, I tried that but it couldn't solve the problem

$('.date-pick').datepicker('setDate', newDate).datepicker('hide')   //failed miserably.

Hence I started to google it out......but didn't really got a satisfactory solution, but found out that few other people were facing the same problem. I got a hint from some site then I started looking into the datepicker js (I certainly am not a js expert).

there in the _setDateDatepicker function after setting the date value in the target it was calling _updateDatepicker which I assumed was popping the datepicker calendar but since the target was hidden it couldn't get the exact position hence it showed the calendar on the bottom left of the page.

So I commented that _updateDatepicker function out and it stopped popping the calendar while setting the date.