skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Friday, October 5, 2012

Free java mail and SMS service

Posted by Raju Gupta at 11:22 PM – 1 comments
 
To address the need of free sms and email service . this is the java program which sends email to defined recipients and sms to defiend mobile numbers. For sms, one additional information of service provider we have give.

jar file required can be easily download from internet free of cost. The link for download is http://java.sun.com/products/javamail/downloads/index.html

Please change the recipient name , username and password.
Setting: Go to your mail account setting and ensure that 'IMAP' is enabled.

some of the mobile service providers are mentioned below:
  1. airtelap.com , Airtel for Andhra Pradesh 
  2. bplmobile.com , BPL Mobile 
  3. airtelmail.com , Delhi Aritel 
  4. delhi.hutch.co.in , Delhi Hutch 
  5. ideacellular.net , Idea Cellular 
  6. escotelmobile.com , Kerala Escotel 
  7. airtelkol.com , Kolkata Airtel 
  8. orangemail.co.in , Mumbai Orange 
  9. orange.net , Orange 
  10. orangemail.co.in , Orange Mumbai 
  11. escotelmobile.com , Uttar Pradesh Escotel 
  12. rpgmail.net , Chennai RPG Cellular 
  13. airtelmail.com , Airtel 
  14. orangemail.co.in , Orange Mumbai 
  15. sms.sancharnet.in , BSNL 
  16. south.hutch.co.in , Andhra Hutch

import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
import javax.activation.*;
import java.util.Properties;
import javax.mail.*;
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;
import com.sun.mail.smtp.*;


 // Public Class
 public class EmailSms
 {

 public static void main(String Args [])
 {
  EmailSms sms = new EmailSms();
  
  
  try {
  String status = sms.functionTosendSMS("Phone Number","bplmobile.com","hi");
  //System.out.println("status  == " + status );
 } 
                      catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
}
 
 
    // Global Variables
    String TO;
    String FROM;
    String SUBJECT;
    String TEXT;
    String MAILHOST;
    String LASTERROR;
    String Result;
    String PASSWORD="your_password";
    // Main function executed
  public String functionTosendSMS(String mo,String sp,String msg) throws Exception
  {
        EmailSms SMS = new EmailSms();

        SMS.setMailHost("smtp.gmail.com");
        SMS.setTo(mo+"@"+sp);    // usage :mobileno@ideacellular.net
        SMS.setFrom("anathor@gmail.com");
        SMS.setSubject("hi");
        SMS.setText(msg);
        boolean ret = SMS.send();

        if (ret) {
            Result="SMS sent!";
            System.out.println("SMS has been sent!");
        } else {
            Result="Not sending..!";
            System.out.println("Not sending - " + SMS.getLastError());
        }
        return Result;
    }

    // Public Constructor
    public EmailSms() {
        TO = "one@gmail.com";
        FROM = "another@gmail.com";
        SUBJECT = "Hi";
        TEXT = "Test";
        MAILHOST = "smtp.gmail.com";
        LASTERROR = "No method called.";
    }

    public void setTo(String to) {
        TO = to;
    }

    public String getTo() {
        return TO;
    }

    public void setFrom(String from) {
        FROM = from;
    }

    public String getFrom() {
        return FROM;
    }

    public void setSubject(String subject) {
        SUBJECT = subject;
    }

    public String getSubject() {
        return SUBJECT;
    }

    public void setText(String text) {
        TEXT = text;
    }

    public String getText() {
        return TEXT;
    }

    public void setMailHost(String host) {
        MAILHOST = host;
    }

    public String getMailHost() {
        return MAILHOST;
    }

    public String getLastError() {
        return LASTERROR;
    }

    // Will attempt to send the Email SMS and return a boolean meaning it
    // either failed or succeeded.
    public boolean send() {

        // Variables to check message length.
        int maxLength;
        int msgLength;

        // Check to make sure that the parameters are correct
        if (TO.indexOf("mobile.att.net") > 0) {
            maxLength = 140;
        } else if (TO.indexOf("messaging.nextel.com") > 0) {
            maxLength = 280;
        } else if (TO.indexOf("messaging.sprintpcs.com") > 0) {
            maxLength = 100;
        } else {
            maxLength = 160;
        }

        // Calculate message length
        msgLength = FROM.length() + 1 + SUBJECT.length() + 1 + TEXT.length();

        // Typically, there are at least two characters of delimiter
        // between the from, subject, and text. This is here to make
        // sure the message isn't longer than the device supports.
        if (msgLength > maxLength) {
            LASTERROR = "SMS length too long.";
            return false;
        }

        // Set Email Properties
        boolean debug = false;
                java.security.Security
                                .addProvider(new com.sun.net.ssl.internal.ssl.Provider());

                //Set the host smtp address
                Properties props = new Properties();
                props.put("mail.transport.protocol", "smtp");
                props.put("mail.smtps.port","465");

                props.put("mail.smtps.starttls.enable","true");
                props.put("mail.smtps.host", MAILHOST);
                props.put("mail.smtps.auth","true");

                Authenticator auth = new SMTPAuthenticator();
              //  Session session = Session.getDefaultInstance(props, auth);
                  Session session = javax.mail.Session.getInstance(props,auth);

                session.setDebug(debug);

                // create a message
                Message msg = new MimeMessage(session);

                /* Properties props = System.getProperties();

                         if (MAILHOST != null) {
                             props.put("mail.smtp.host", MAILHOST);

                         }

                         // Get a Session object
                         Session session = Session.getInstance(props, null); */

        try {

            // Construct the email
           // Message msg = new MimeMessage(session);

            // Set From
            if (FROM != null) {
                msg.setFrom(new InternetAddress(FROM));
            } else {
                msg.setFrom();
            }

            // Set Subject
            msg.setSubject(SUBJECT);

            // Set Text
            msg.setText(TEXT);

            // Add Recipient
            msg.setRecipients(Message.RecipientType.TO,
                                InternetAddress.parse(TO, false));

            // Sent Date
            msg.setSentDate(new Date());

            // Send Email SMS
            URLName urln = new URLName("smtp", MAILHOST, 587, "",FROM,"your_password"); //port numbers 587 456

            com.sun.mail.smtp.SMTPTransport trans = new com.sun.mail.smtp.SMTPTransport(session, urln);

            //trans.setStartTLS(true);;

            com.sun.mail.smtp.SMTPTransport.send(msg); 


            trans.close();

            Transport.send(msg);
            LASTERROR = "Success.";
            return true;
        } catch (MessagingException mex) {
            LASTERROR = mex.getMessage();
            System.out.println("Allwyn last error "+mex.getMessage());
        return false;
        }
    }



    private class SMTPAuthenticator extends Authenticator
    {

               public PasswordAuthentication getPasswordAuthentication()
               {
                       String username = "anathor@gmail.com";
                       String password = PASSWORD;
                       return new PasswordAuthentication(username, password);
               }
       }
       


}

One Response so far.

  1. kr mandal says:
    February 14, 2013 at 9:20 AM

    i,m faced this error...............

    Allwyn last error Could not connect to SMTP host: localhost, port: 25
    Not sending - Could not connect to SMTP host: localhost, port: 25

    help me

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger