Choose Your Language

Sunday 30 December 2012

java program to send mail using gmail server

package aravind.common
import java.security.Security;
import java.util.Properties;
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;
 
/**
 * @author Aravind Sankaran Nair
 */
public class MailSender {
 
    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final String SMTP_PORT = "465";
    private static final String emailMsgTxt = "Hi this is my body content";
    private static final String emailSubjectTxt = "A test mail using Java mail";
    private static final String emailFromAddress = "your gmail id";
    private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    private static final String[] sendTo =
                                                    { "aravindsankaran123@gmail.com"      ,"aravindsankaran@ymail.com"};
    public static void main(String args[]) throws Exception {
 
        MailSender mailSender = new MailSender ();
        mailSender.sendSSLMessage(sendTo, emailSubjectTxt,
                emailMsgTxt, emailFromAddress);
        System.out.println("Sucessfully sent mail to all Users");
    }
 
    /**
     * @param recipients
     * @param subject
     * @param message
     * @param from
     * @throws MessagingException
     */
    public void sendSSLMessage(String recipients[], String subject,
            String message, String from) throws MessagingException {
        boolean debug = true;   
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "true");
        props.put("mail.smtp.port", SMTP_PORT);
        props.put("mail.smtp.socketFactory.port", SMTP_PORT);
        props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.put("mail.smtp.socketFactory.fallback", "false");
 
        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("your gmail id",
                                "your gmail password");
                    }
                });
 
        session.setDebug(debug);   
        Message msg = new MimeMessage(session);
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);   
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);
 
        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        Transport.send(msg);
    }

Monday 17 December 2012

First Java Program Using Netbeans IDE

Step 1: Open Netbeans IDE


Step 2: Choose New Project from File menu


Step 3:  Select java from categories and java Application from projects



Step 4: click Next

Step 5:  Give a Project Name, choose project location, Class Name


Step 6:click Finish


Step 7: Add this line inside main()
            System.out.println("First Java Program Using Netbeans");


Step 8: Right click FirstClass.java and select Run File


Step 9: it will produce the output
                               First Java Program Using Netbeans

Sunday 16 December 2012

Java Program Execution Steps


Creating and Running Your First Java Program Using Notepad

Creating and Running Your First Java Program Using Notepad

Step 1: Copy the following code into a notepad.

class FirstProgram{
 public static void main(String args[]){
     System.out.println("My First Java Program");
 }
}

Step 2: Save the file in the directory C:workspace ,  as FirstProgram.java  & not as  FirstProgram.java.txt
Step 3:  Open the command prompt. Go to Directory C:workspace . Compile the code using command,  javac FirstProgram.java

Step 4: Run the code using command, java FirstProgram
output:
 My First Java Program

Java Application Programming Interface (Java API)

Java Application Programming Interface (Java API)

An application programming interface (API), in the context of Java, is a collection of predefined packages, classes, and interfaces with their respective methods, fields and constructors. Similar to a user interface, which facilitates interaction between humans and computers, an API serves as a software program interface facilitating interaction.

In Java, most basic programming tasks are performed by the API’s classes and packages, which are helpful in minimizing the number of lines written within pieces of code.

The API is a library of available Java classes, packages and interfaces.The API help programmers determine class or package functions, parameters and other necessary information for implementation.

The three API types are as follows:
  • Official Java core API, which is bundled with the JDK download.
  • Optional official Java APIs, which may be downloaded if needed.
  • Unofficial APIs, which are third-party APIs that may be downloaded from source websites.

Java Runtime Environment (JRE)

The Java Runtime Environment (JRE), also known as Java Runtime, is part of the Java Development Kit (JDK), a set of programming tools for developing Java applications. The Java Runtime Environment provides the minimum requirements for executing a Java application. it consists of the Java Virtual Machine (JVM), core classes and supporting files. The JRE is available for multiple computer platforms, including Mac, Windows, and Unix.

What Java is?? Why Java?? Where Java??

What Java is??
Java is a high level programming Language. It was introduced by “SUN Micro Systems” in June 1995. It was officially released in November 1995. It was developed by a team under James Gosling.

Why Java??
Java has become the standard for Internet applications. It provides interactive processing and easy use of graphics and animation on the Internet. Internet consists of different types of Computers and Operating Systems. A common language. needed to enable computers. To run programs that run on multiple platforms. This need was fulfilled by Java. Because the it is the language of choice for the Internet. Java is Object-Oriented language built on C and C++. It derives its syntax from C and its Object-Oriented features are influenced by C++. 

Where Java??
Java can be used to create two types of programs. Those are Applications and Applets. An application is a programs that runs on the user’s computers under the operating system. An Applet is a small window based programs that runs on HTML page using a java enabled web browser like internet Explorer, Netscape Navigator or an Applet Viewer.

 

Java Virtual Machine



What is JVM (Java Virtual Machine) and how it works??

At the heart of the Java platform lies the Java Virtual Machine, or JVM. Most programming languages compile source code directly into machine code, suitable for execution on particular microprocessor architecture. The difference with Java is that it uses bytecode - a special type of machine code.
      
Java bytecode executes on a special type of microprocessor. Strangely enough, there wasn’t a hardware implementation of this microprocessor available when Java was first released. Instead, the processor architecture is emulated by what is known as a “virtual machine”. This virtual machine is an emulation of a real Java processor - a machine within a machine. The only difference is that the virtual machine isn’t running on a CPU - it is being emulated on the CPU of the host machine.

The Java Virtual Machine is responsible for interpreting Java bytecode, and translating this into actions or operating system calls.

For example, a request to establish a socket connection to a remote machine will involve an operating system call. Different operating systems handle sockets in different ways - but the programmer doesn’t need to worry about such details.  It is the responsibility of the JVM to handle these translations, so that the operating system and CPU architecture on which Java software is running is completely irrelevant to the developer.

The Java Virtual Machine forms part of a large system, the Java Runtime Environment (JRE). Each operating system and CPU architecture requires a different JRE. The JRE comprises a set of base classes, which are an implementation of the base Java API, as well as a JVM. The portability of Java comes from implementations on a variety of CPUs and architectures. Without an available JRE for a given environment, it is impossible to run Java software.

Differences between JVM implementations
  
Though implementations of Java Virtual Machines are designed to be compatible, no two JVMs are exactly alike.

For example, garbage collection algorithms vary between one JVM and another, so it becomes impossible to know exactly when memory will be reclaimed. The thread scheduling algorithms are different between one JVM and another (based in part on the underlying operating system), so that it is impossible to accurately predict when one thread will be executed over another.
      
Initially, this is a cause for concern from programmers new to the Java language. However, it actually has very little practical bearing on Java development. Such predictions are often dangerous to make, as thread scheduling and memory usage will vary between different hardware environments anyway. The power of Java comes from not being specific about the operating system and CPU architecture - to do so reduce the portability of software.