Introduction

This is a java class file for sending mails using java APIs. It requires two jar files to be set in the class path. Also Java J2SDK is required to compile or run this code. A valid SMTP server ip is required and that should be authenticated by a valid user id and password.

Limitations
J2SDK or JRE should be installed in the machine where this program needs to be executed

import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;   

public class mailSendClass
{   

    /**************** method to send mail. The parameters which required for this method call are SMTP host IP,
     From mail address, To mail address mail subject and mail body ****************************/    

    public void sendEmail(
        String mailHost,
        String aFromEmailAddr, String aToEmailAddr,
        String aSubject, String aBody
        )
    {
        //Authenticators are used to prompt the user for user name and password.   

        Authenticator auth = null;    

        //          Get system properties   

        // To return PasswordAuthentication use a valid user id and password.   

        Properties props = System.getProperties();
        props.put("mail.smtp.host", mailHost);
        props.put("mail.smtp.auth", "true");
        props.put( "mail.transport.protocol ", "SMTP");
        auth = new Authenticator ()
        {
            public PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication("userid","password");
            } };   

        Session session = Session.getDefaultInstance( props, auth );
        MimeMessage message = new MimeMessage(session);
        System.out.println("Got a MimeMessage");   

        try
        {   

            //This bloke of code sets from, to addresses, message subject, message body and finally sends the mail.   

            message.setFrom( new InternetAddress(aFromEmailAddr) );
            message.setRecipient(
                Message.RecipientType.TO, new InternetAddress(aToEmailAddr)
                );   

            message.setSubject( aSubject );
            message.setContent(aBody,"text/plain");
            message.setText( aBody );
            Transport.send( message );   

            System.out.println("Message sent successfully.........");
        }   

            //In catch block print the error why message is not sent.   

        catch (MessagingException exm)
        {
            System.out.println("Cannot send email. " + exm);
        }
        catch (Exception e)
        {
            System.out.println("Error occured at send email method. " + e);
        }
    }   

    public static void main(String args[])
    {
        mailSendClass sentaMail = new mailSendClass();
        try
        {
            InputStreamReader reader = new InputStreamReader (System.in);
            BufferedReader buf_in = new BufferedReader (reader);
            System.out.println("Enter SMTP host IP");
            String strSmtpIp = buf_in.readLine ();
            System.out.println("Enter from mail address");
            String strFrmMailId = buf_in.readLine ();
            System.out.println("Enter to mail address");
            String strToMailId = buf_in.readLine ();
            System.out.println("Enter mail subject");
            String strSubject = buf_in.readLine ();
            System.out.println("Enter message body");
            String strMsgBody = buf_in.readLine ();
            sentaMail.sendEmail(strSmtpIp, strFrmMailId, strToMailId, strSubject, strMsgBody);
        }   

        catch (IOException eIo)
        {
            System.out.println("IO Exception occured at main method " + eIo);
        }   

        catch (Exception ex)
        {
            System.out.println("Error occured at main method " + ex);
        }   

        //Finally call the method   

    }   

}  

Related Posts

  1. Java Commons-Email API
  2. Java Developers Forgetting that Java is zero-indexed
  3. Java API for Voice Based Solutions
  4. Java Programming Best Practice Writing blank exception handlers
  5. UTL_MAIL package in Oracle 10g

Tags: , ,

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>