Search This Blog

Wednesday, 3 September 2014

Sending Email using Java

Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com"); // for gmail use smtp.gmail.com
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");

        Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {

                @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(getUserName()/*gmail username in Strings */, getPassword()/*gmail password in Strings*/ );
                                               
            }
        });
             System.out.println("Pasword::"+getPassword());
        mailSession.setDebug(true); // Enable the debug mode

        Message msg = new MimeMessage( mailSession );

        //--[ Set the FROM, TO, DATE and SUBJECT fields
        msg.setFrom( new InternetAddress( getUserName()/*"your gmail user name"*/ ) );
        msg.setRecipients( Message.RecipientType.TO,InternetAddress.parse(getTo()/*any which person your sendibg that person mail id in Strings*/) );
        msg.setSentDate( new Date());
        msg.setSubject( getSubject()/*Subject of your mail*/ );

        //--[ Create the body of the mail
        msg.setText( getMessage()/*your message*/ );
//--[ Ask the Transport class to send our mail message
        Transport.send( msg );//sending the message

Tuesday, 2 September 2014

Reading Email and Search the mails based on subject

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sravan;

/**
 *
 * @author sravan.k
 */
import java.util.*;
import javax.mail.*;


public class ReadingMail {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "imaps");//setting the properties for imap .it is advanced server
        try {
            Session session = Session.getInstance(props, null);//getting the session
            Store store = session.getStore();//getting the store using session object
            store.connect("imap.gmail.com", "username@gmail.com", "your password");
            Folder inbox = store.getFolder("INBOX");//getting the inbox folder
            inbox.open(Folder.READ_ONLY);//open it in read only mode
          String sub = "123456789";//search subject. change accordingly
          int x =  inbox.getMessageCount();//total number mails in your acccount
          int i=0;
            System.out.println("inbox::"+x);
       
            Message messages[] = inbox.getMessages();
            for(i=x-1;i>x-50;i--){ ///reading last 50 messages and selecting subject 123456789
                Message msg = messages[i];
                if(sub.equalsIgnoreCase(msg.getSubject()))
                {
                    System.out.println("Recieved date::"+msg.getReceivedDate());//getting Received date\
                    System.out.println("Sent Date::"+msg.getSentDate());
                  //using getFrom() methos in message object we will get the address.and bodypart() method we will get the content
                   
                }
               
               
               
            }
           
           
        } catch (Exception mex) {
            mex.printStackTrace();
        }
    }
}

Saturday, 23 August 2014

Creating a Grid Using Extjs and Loading Data from Database

First Creating Java Bean to set and get the Details from  DataBase.

package com.first;

public class GridStore {

    public String getEMAILID() {
        return EMAILID;
    }

    public void setEMAILID(String EMAILID) {
        this.EMAILID = EMAILID;
    }

    public String getFIRSTNAME() {
        return FIRSTNAME;
    }

    public void setFIRSTNAME(String FIRSTNAME) {
        this.FIRSTNAME = FIRSTNAME;
    }

    public String getLASTNAME() {
        return LASTNAME;
    }

    public void setLASTNAME(String LASTNAME) {
        this.LASTNAME = LASTNAME;
    }

    public String getMOBILENO() {
        return MOBILENO;
    }

    public void setMOBILENO(String MOBILENO) {
        this.MOBILENO = MOBILENO;
    }

    private String FIRSTNAME;
    private String LASTNAME;
    private String EMAILID;
    private String MOBILENO;



}


Now Creating Grid and loading Data from Data Base....



<%@page import="com.first.GridStore"%>
<%@page import="com.google.gson.Gson"%>
<%@ page import="java.sql.*" %>
<%@page import="java.util.*" %>


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<script type="text/javascript" src="ext-all.js"></script>
<link href="resources/css/ext-all.css" rel="stylesheet"/>

<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>

    <%
        List s1 ;
        Gson gson = new Gson();
    %>


    <%
        s1 = new ArrayList();

        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:DSN", "UserName", "Password");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select * from WNP");
        while (rs.next()) {
           GridStore e = new GridStore();
            e.setFIRSTNAME(rs.getString(1));
            e.setLASTNAME(rs.getString(2));
            e.setEMAILID(rs.getString(3));

            e.setMOBILENO(rs.getString(4));
         

            s1.add(e);

        }





    %>

    <script type="text/javascript">
        Ext.onReady(function(){

            var stores= Ext.create('Ext.data.Store', {
 fields:['FIRSTNAME','LASTNAME','EMAILID','MOBILENO'],





               data:{'names':[

                    ]},
                proxy: {
                    type:'memory',
                    reader: {
                        type: 'json',
                        root: 'names'
                    }
                }
            });
         

            Ext.create('Ext.grid.Panel', {
                title: 'Getting Data From Database',
                width:500,
                store: stores,
                columns: [
                    { header: 'FIRSTNAME',  dataIndex: 'FIRSTNAME'},
                    { header: 'LASTNAME', dataIndex: 'LASTNAME'},
                    {header:'EMAILID',dataIndex:'EMAILID'},
                    {header:'MOBILENO',dataIndex:'MOBILENO'},
                 
                     ],
                height: 500,
                width: 800,
                id:"grid1",


                renderTo: Ext.getBody()
            });




  Ext.getCmp("grid1").store.loadData(<%=gson.toJson(s1)%>);
        });
    </script>
    <body>
     
    </body>
</html>