Saturday 7 December 2013

Resource Injection (Using javax.annotation.Resource)


Resource Injection Using javax.annotation.Resource  



Basically we can inject
(1)Mail Session,
(2)Data Source,
(3)Jndi Resource it can be simple text or it can be beans etc.

Here i am going to Demonstrate MailSession Injection using glashfish server:


Hello friends.hope you all are fine.in this post i am going to demonstrate you how to inject MailSession that has been configured in glashfish server.i hope you are aware of how to define Java MailSession using glashfish server.

if you are not familiar with how to configure java mail session into glashfish than i should advice you to go through http://javaeenotes.blogspot.in/2010/04/using-javamail-api-with-glassfish-and.html.


i am posting sample code you can modify the below code as per the requirement:




package org.demo.Mail;

import javax.annotation.Resource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.*;


public class MailSender
{

    /**
     * @param args
     * @throws NamingException
     * @throws MessagingException
     */

//refers to myMailSession that has been configured in glashfish server.
    @Resource(name="mail/myMailSession")
    Session mailSession;
   
    public static void main(String[] args) throws NamingException, MessagingException
    {
       
       
       
               
       
        InitialContext ic = new InitialContext();//create the initcontext object
       

MyMailSession=Java Mail Session that has been configured into  Glashfish server.
        String snName = "java:comp/env/mail/MyMailSession";

       
       
        Session session = (Session)ic.lookup(snName);//get the resource from global JNDI
       
       
        Properties  props=session.getProperties();//get the Properties that you have defined in glashfish
       
       
        props.put("mail.from","********@gmail.com");//append extra property
        //[Note: when you are configuring properties you are configuring it as a mail-session which is interally converted into mail.session by server.so whatever property you want to define you can define it as a property-name pair.
       
        Message msg =new MimeMessage(session);//create message with Session


        msg.setSubject("Reminder");
           
        msg.setSentDate(new Date());
       
        msg.setRecipients(Message.RecipientType.TO,
                   InternetAddress.parse("****@gmail.com", false));
       
   
        msg.setText("hey guyzz");
       
        Transport.send(msg);
       
       
       
       
       
    }

}



Thanks a lot
for any query ping me on pathak.nisarg@yahoo.com

No comments:

Post a Comment

Spring Boot SSL configuration -Tomcat Server

Hi Friends hope you all are doing well. Today I am going to demonstrate about how to configure SSL in Spring boot web Application. Need o...