Sunday, August 16, 2015

MAIL PROGRAM OTP EXAMPLE

2:09 AM



import java.util.Properties;

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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class OTPMail {

    public static void main(String ar[]){

        final String userName = "madhu.jse";
        final String password = "madhavi@1";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host",
                "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
        Session session = Session.getDefaultInstance(props, new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }

        });
        Message message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress("madhu.jse@gmail.com"));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress("madhavigurram.java@gmail.com"));
            message.setSubject("OTP");
            message.setText("Hi this is from Madhu");
            Transport.send(message);
            System.out.println("Message send successfully");
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
       
    }

}

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 Java Tutorials. All rights resevered. Designed by Templateism

Back To Top