A Program for Sending mail in java (JAVAEE)

Aim: Write a program to demonstrate java mail



program:



import java.util.*;

import java.io.*;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;



public class javamail_send extends Object {



  public static void main(String args[]){



    String smtpServer = null;

    String toEmail    = null;

    String fromEmail  = null;

    String body       = null;

  

    //--[ Parse the Command line parameters

    for ( int x=0; x < args.length-1; x++ ){

      if ( args[x].equalsIgnoreCase("-S") )

        smtpServer  = args[x+1];

      else if ( args[x].equalsIgnoreCase("-T") )

        toEmail     = args[x+1];

      if ( args[x].equalsIgnoreCase("-F") )

        fromEmail  = args[x+1];

      if ( args[x].equalsIgnoreCase("-B") )

        body  = args[x+1];

    }

  

 if ( smtpServer == null || toEmail == null || fromEmail == null || body == null ){

   System.out.println( "Usage: javamail_send -S -T -F -B " );

   System.exit(1);

 }



 //--[ Obtain a session

 try{

   //--[ Set up the default parameters

      Properties props = new Properties();

      props.put("mail.transport.protocol", "smtp" );

      props.put("mail.host", smtpServer );              

    



    //--[ Create the session and create a new mail message

      Session mailSession = Session.getInstance( props );

      Message msg = new MimeMessage( mailSession );



   //--[ Set the FROM, TO, DATE and SUBJECT fields

      msg.setFrom( new InternetAddress( fromEmail ) );

      msg.setRecipients( Message.RecipientType.TO,

                         InternetAddress.parse(toEmail) );

      msg.setSentDate( new Date() );

      msg.setSubject( "Test Mail" );



      //--[ Create the body of the mail

      msg.setText( body );



      Transport.send( msg );

    

      System.out.println( "The email below was sent successfully" );

      msg.writeTo( System.out );



 }catch(Exception E){

   System.out.println( E );

 }

  }

}







Execution Procedure:



step 1:  save this code with javamail_send.java and compile the code using javac.



  i.e.

  c:\>cd javamail  //this enters into the javamail folder

  c:\javamail> javac javamail_send.java



step 2: Set the classpath



  it can be done with the help of classpath setting in the environmental varialbles( taking GLASSFISH as app server )



 a) set the javaee.jar

 b) set the mail.jar

 c) the folder name where we are compiling our code

 d)make sure smtp server should be in our system(windows providing default smtp server as a optional component)



step 3: Run the code



c:\javamail>java javamail_send -s localhost -t localhost -f localhost -b "Hai, this is the program for jmail"



output:



The email below was sent successfully

Date: Wed, 22 Sep 2010 20:52:59 +0530 (IST)

From: localhost

To: localhost

Message-ID: <10748354.0.1285168979984.JavaMail.maxx@localhost>

Subject: Test Mail

MIME-Version: 1.0

Content-Type: text/plain; charset=us-ascii

Content-Transfer-Encoding: 7bit

0 komentar:

Post a Comment

Related Posts with Thumbnails
GiF Pictures, Images and Photos