Pages

Tuesday 31 December 2013

GET NUMBER OF PAGE OF PDF FILE USING iTEXT DEMO

For perform this practical you need itext java API. Download it from iText official website.

A simple practical that will count the total number of pages of any pdf file. This may use in e-library like application that i already use.


CountPDFPage.java

import com.itextpdf.text.pdf.PdfReader;

public class CountPDFPage {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        PdfReader reader;
        try {
            reader = new PdfReader("D:\\jreports.pdf");
            int page = reader.getNumberOfPages();
            System.out.println("Number Of pages : "+page); // IT WILL RESULT 339 AS A NUMBER OF TOTAL PDF PAGES.
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
    }

SENDING HTML PAGE AS A EMAIL USING GMAIL SMTP

Main.java

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         String[] recipients = new String[]{"maheshwarianand@ymail.com"}; 
            String[] bccRecipients = new String[]{"anandmaheshwari15790@gmail.com"}; 
            String subject = "Hi this is test Mail"; 
            String messageBody = "Test Mail from codesstore.blogspot.com"; 
     
            new MailUtil().sendMail(recipients, bccRecipients, subject, messageBody); 
    }

}



MainUtil.java

import java.io.UnsupportedEncodingException; 
import java.util.Properties; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator; 
import javax.mail.BodyPart;
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart;
 
public class MailUtil { 
 
    private String SMTP_HOST = "smtp.gmail.com"; 
    private String FROM_ADDRESS = "sender_mail@gmail.com"; 
    private String PASSWORD = "mail_password"; 
    private String FROM_NAME = "Anand"; 
 
    public boolean sendMail(String[] recipients, String[] bccRecipients, String subject, String message) { 
        try { 
            Properties props = new Properties(); 
            props.put("mail.smtp.host", SMTP_HOST); 
            props.put("mail.smtp.auth", "true"); 
            props.put("mail.debug", "false"); 
            props.put("mail.smtp.ssl.enable", "true"); 
 
            Session session = Session.getInstance(props, new SocialAuth()); 
           
           
            Message msg = new MimeMessage(session); 
           
           
 
            InternetAddress from = new InternetAddress(FROM_ADDRESS, FROM_NAME); 
            msg.setFrom(from); 
 
            InternetAddress[] toAddresses = new InternetAddress[recipients.length]; 

           
            for (int i = 0; i < recipients.length; i++) { 
                toAddresses[i] = new InternetAddress(recipients[i]); 
            } 
           
            msg.setRecipients(Message.RecipientType.TO, toAddresses); 
 
 
            InternetAddress[] bccAddresses = new InternetAddress[bccRecipients.length]; 
            for (int j = 0; j < bccRecipients.length; j++) { 
                bccAddresses[j] = new InternetAddress(bccRecipients[j]); 
            } 
            msg.setRecipients(Message.RecipientType.BCC, bccAddresses); 
 
            msg.setSubject(subject); 
           
            //
            // This HTML mail have to 2 part, the BODY and the embedded image
            //
            MimeMultipart multipart = new MimeMultipart("related");

            // first part  (the html)
            BodyPart messageBodyPart = new MimeBodyPart();
            String htmlText = "<center><H1>Hello</H1></center><img src=\"cid:image\">";
            messageBodyPart.setContent(htmlText, "text/html");

            // add it
            multipart.addBodyPart(messageBodyPart);
           
            // second part (the image)
            messageBodyPart = new MimeBodyPart();
            DataSource fds = new FileDataSource
              ("D:\\a.jpg");
            messageBodyPart.setDataHandler(new DataHandler(fds));
            messageBodyPart.setHeader("Content-ID","<image>");

            // add it
            multipart.addBodyPart(messageBodyPart);
           
           
            //msg.setContent(message, "text/plain"); 
           
            msg.setContent(multipart);
            Transport.send(msg); 
           
            System.out.println("Sendded Successfully...");
            return true; 
        } catch (UnsupportedEncodingException ex) { 
            Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex); 
            return false; 
 
        } catch (MessagingException ex) { 
            Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex); 
            return false; 
        } 
    } 
 
    class SocialAuth extends Authenticator { 
 
        @Override 
        protected PasswordAuthentication getPasswordAuthentication() { 
 
            return new PasswordAuthentication(FROM_ADDRESS, PASSWORD); 
 
        } 
    } 


=====================================================================

Saturday 28 December 2013

SEND EMAIL IN JAVA WITH gmail SMTP



Send a mail using JAVA mail API with gmail smtp server.
You need mail.jar JAVA API file. You can download it from oracle-java site.  

MailUtil.java

import java.io.UnsupportedEncodingException; 
import java.util.Properties; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.mail.Authenticator; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
 
public class MailUtil { 
 
    private String SMTP_HOST = "smtp.gmail.com"; 
    private String FROM_ADDRESS = "sender_mail@gmail.com"; 
    private String PASSWORD = "***********"; 
    private String FROM_NAME = "Anand"; 
 
    public boolean sendMail(String[] recipients, String[] bccRecipients, String subject, String message) { 
        try { 
            Properties props = new Properties(); 
            props.put("mail.smtp.host", SMTP_HOST); 
            props.put("mail.smtp.auth", "true"); 
            props.put("mail.debug", "false"); 
            props.put("mail.smtp.ssl.enable", "true"); 
 
            Session session = Session.getInstance(props, new SocialAuth()); 
            Message msg = new MimeMessage(session); 
 
            InternetAddress from = new InternetAddress(FROM_ADDRESS, FROM_NAME); 
            msg.setFrom(from); 
 
            InternetAddress[] toAddresses = new InternetAddress[recipients.length]; 
            for (int i = 0; i < recipients.length; i++) { 
                toAddresses[i] = new InternetAddress(recipients[i]); 
            } 
            msg.setRecipients(Message.RecipientType.TO, toAddresses); 
 
 
            InternetAddress[] bccAddresses = new InternetAddress[bccRecipients.length]; 
            for (int j = 0; j < bccRecipients.length; j++) { 
                bccAddresses[j] = new InternetAddress(bccRecipients[j]); 
            } 
            msg.setRecipients(Message.RecipientType.BCC, bccAddresses); 
 
            msg.setSubject(subject); 
            msg.setContent(message, "text/plain"); 
            Transport.send(msg); 
            System.out.println("Sendded Successfully...");
            return true; 
        } catch (UnsupportedEncodingException ex) { 
            Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex); 
            return false; 
 
        } catch (MessagingException ex) { 
            Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex); 
            return false; 
        } 
    } 
 
    class SocialAuth extends Authenticator { 
 
        @Override 
        protected PasswordAuthentication getPasswordAuthentication() { 
 
            return new PasswordAuthentication(FROM_ADDRESS, PASSWORD); 
 
        } 
    } 
} 


Main.java

public class Main {

       /**
        * @param args
        */
       public static void main(String[] args) {
              // TODO Auto-generated method stub
               String[] recipients = new String[]{"maheshwarianand@ymail.com"}; 
               String[] bccRecipients = new String[]{"sender_mail@gmail.com"}; 
               String subject = "Hi this is test Mail"; 
               String messageBody = "Test Mail from morecomputerscience.blogspot.com"; 
        
               new MailUtil().sendMail(recipients, bccRecipients, subject, messageBody); 
       }

}


=======================================================================

Saturday 21 December 2013

Solution For ORA-28001 AND ORA-28002 ORACLE DATABASE ERROR



PROBLEM :

When you working on Oracle you may get this issue that Your account may locked, or password is expired.
To solve this problem here is the solution, it may take less than a minute. Oracle give you 28001 OR 28002 error code.

SOLUTION :

Connect as SYS AS SYSDBA user.
For example my “SCOTT” user is locked and password is expired for  “SCOTT” user.
SQL > select username,account_status from dba_users;
It will so you list of all user and status of the user’s account.
Now my scott account is “EXPIRED & LOCKED”
First of all UNLOCK the user account.
SQL> ALTER USER scott ACCOUNT UNLOCK;
Now again execute previous query.
SQL > select username,account_status from dba_users;
                Now my scott account is “EXPIRED”
Secondly, execute the following query.

SQL>  select 'alter user "'||d.username||'" identified by values '''||u.password||''';' c
           from dba_users d, sys.user$ u
           where d.username = upper('&&username')
           and u.user# = d.user_id;
                It will give you update query. Execute that update query.
SQL > alter user "SCOTT" identified by values 'F894844C34402B67';
That’s all your work is done.
Now check the status of the scott user.
SQL > select username,account_status from dba_users;
                Now my scott account is “OPEN”

IF YOU HAVE ANY OTHER SOLUTION THAN PLEASE COMMENT IT.