Friday, 18 January 2013

Java Code to send mail with attachment


import java.io.File;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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 Mail {

  public static void main(String args[]) {
 try{
 String host = "smtp.gmail.com";
   String from = "shivsriv@gmail.com";
   String pass ="Your password";
   Properties props = System.getProperties();
   props.put("mail.smtp.starttls.enable", "true"); // added this line
   props.put("mail.smtp.host", host);
   props.put("mail.smtp.user", from);
   props.put("mail.smtp.password", pass);
   props.put("mail.smtp.port", "587");
   props.put("mail.smtp.auth", "true");

   String to = "jyotika@gmail.com"; // added this line
   System.out.println("preparing mail.....");
   Session session = Session.getDefaultInstance(props, null);
   MimeMessage message = new MimeMessage(session);

  // Get system properties
  Properties properties = System.getProperties();

  // Setup mail server
  properties.setProperty("mail.smtp.host", host);

  // Get the default Session object.


  // Define message

  message.setFrom(new InternetAddress(from));
   message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
  message.setSubject("Mail With Attachment");

  // Create the message part
  BodyPart messageBodyPart = new MimeBodyPart();

  // Fill the message
  messageBodyPart.setText("Hi this is demo mail with attachment");

  Multipart multipart = new MimeMultipart();
  multipart.addBodyPart(messageBodyPart);

  // Part two is attachment
  messageBodyPart = new MimeBodyPart();
  File filename = new File("F:/cassandra10.pdf");
  DataSource source = new FileDataSource(filename);
  messageBodyPart.setDataHandler(new DataHandler(source));
  messageBodyPart.setFileName("my.pdf");
  multipart.addBodyPart(messageBodyPart);

  // Put parts in message
  message.setContent(multipart);

  // Send the message
  Transport transport = session.getTransport("smtp");
  transport.connect(host, from, pass);
  System.out.println("Mail prepared ....");
  transport.sendMessage(message, message.getAllRecipients());
  transport.close();
   System.out.println("mail Sended ....");
 }catch (Exception e) {
 System.out.println("Mail failed ....");
e.printStackTrace();
}
  }
}

Saturday, 21 April 2012

Distinct value in two column

I have a query in mysql
select f1,f2,f3 from tableName;
I want to remove duplicate value in field f1 and f2 only,

actual query is:
SELECT f1,f2,f3
FROM (SELECT f1,f2,f3 FROM tableName  GROUP BY f1) AS table1 GROUP BY f2;

Wednesday, 29 February 2012

Send Mail Through Your Gmail Account

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.
RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.smtp.SMTPSSLTransport;

public class SendMailExample {
public static void main(String[] args)throws Exception {

String host = "smtp.gmail.com";
    String from = "sheo.hclcdc@gmail.com";
    String pass ="Your Password";

    Properties props = System.getProperties();
    props.put("mail.smtp.starttls.enable", "true"); // added this line
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");

    String[] to = {"shivsriv@gmail.com","triptishukla29@gmail.com"}; // added this line

    Session session = Session.getDefaultInstance(props, null);
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));

    InternetAddress[] toAddress = new InternetAddress[to.length];

    // To get the array of addresses
    for( int i=0; i < to.length; i++ ) { // changed from a while loop
        toAddress[i] = new InternetAddress(to[i]);
    }
    System.out.println(Message.RecipientType.TO);

    for( int i=0; i < toAddress.length; i++) { // changed from a while loop
        message.addRecipient(Message.RecipientType.TO, toAddress[i]);
    }
    message.setSubject("Notification Mail");
    message.setText("Welcome !!!!......This is Automated Mail  \nThanks & Regards \n sheo");
    Transport transport = session.getTransport("smtp");
    transport.connect(host, from, pass);
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
}
}

Thursday, 26 January 2012

Netbeans Problem: How To Change Port Number For GlassFish

Both Oracle an GlassFish runs by default on port number 8080,results in port conflict
Following are simple steps to change the port number of glassfish server :

  1. Go to the folder where Glassfish is installed.
  2. Locate config folder which is as follows: C:\Program Files\glassfish-3.0.1\glassfish\domains\domain1\config
  3. Open domain.xml using any text editor.
  4. Look for 8080 and change it to some other port number that doesn’t conflict with other port numbers.
  5. Save domain.xml.
  6. Now  remove GlassFish from NetBeans and add it again so that NetBeans IDE understands the new port number.
  7. Restart GlassFish, if it was already running.

How to change the port number of tomcat

Default port number of tomcat is 8080, However there is  chances of port conflict with others program. Sometime we just need to change the Tomcat port number.
4 simple Steps to change the port number

1.Just search server.xml file in the conf directory [within  tomcat installation folder]

2.Search for content like following:

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

3. change the port attribute to any other port number.

4. save the server.xml file and Restart Tomcat.

Thursday, 19 January 2012

what is loadfactor in HashSet and HashMap


The load factor is a measure of how full the hash set is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.
If you specify a capacity of 128 and a load factor of .75. The load factor  decide when it is time to double the size of the table, in this case, just after you have added 96 elements, not in deciding its initial size.
source:
http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html

Wednesday, 18 January 2012

Difference between URL encoding and URL Rewriting

URL Rewriting is a technique for saving state or session information on the user's browser between page hits. It is a session management technique. The session information gets appended at the end of the URL, as an additional parameter.

Morevoer, if query string parameters are added to the URL, it is important that they
be properly URL encoded. URL encoding refers to the process of encoding special
characters in a string, according to the rules defined in RFC 2396. For example, a
space must be encoded in a URL string as a '+':


http://mypage.com/app/travel?country=Dominican+Republic

Why char[] array is prefere over String to store password in JAVA ?


Monday, 9 January 2012

Differentiate JVM JRE JDK JIT

Java Virtual Machine (JVM) is an abstract computing machine. Java Runtime Environment (JRE) is an implementation of the JVM. Java Development Kit (JDK) contains JRE along with various development tools like Java libraries, Java source compilers, Java debuggers, bundling and deployment tools.
JVM becomes an instance of JRE at runtime of a java program. It is widely known as a runtime interpreter.
JIT is the part of the Java Virtual Machine (JVM) that is used to speed up the execution time. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation.

Friday, 30 December 2011