/** * The program displays a simple mail client................ */ import java.io.*; import java.util.*; import javax.mail.*; import javax.mail.internet.*; /** * * @author gurt0582 * * *The class extends authonticator bacause first of all it will do password authentication then *it will send any message................ */ public class MailClient extends Authenticator { /** * flag for reading message */ public static final int SHOW_MESSAGES = 1; /** * flag for clearing message */ public static final int CLEAR_MESSAGES = 2; /** * flag for reading and then clearing the message */ public static final int SHOW_AND_CLEAR = SHOW_MESSAGES + CLEAR_MESSAGES; protected String from; protected Session session; /** * PasswordAuthontication is just simple a repository for user name and password * it has only two methods getUserName() * and getPassword() */ protected PasswordAuthentication authentication; public MailClient(String user,String password, String host) { /** * calls the another construcor of the class */ this(user,password, host, false); } public MailClient(String user,String password, String host, boolean debug) { from = user + '@' + host; /** * here authontication is created means in authentication repository values are stored............. */ authentication = new PasswordAuthentication(user, password); System.out.println("User is :"+authentication.getUserName()+" Password is:"+authentication.getPassword()); /** * creating an instance of properties */ Properties props = new Properties(); /** * storing name and value pairs in property */ props.put("mail.user", user); props.put("mail.host", host); /** * storing debug information */ props.put("mail.debug", debug ? "true" : "false"); /** * storing mail retrieval protocol */ props.put("mail.store.protocol", "pop3"); /** * storing mail transfer protocol */ props.put("mail.transport.protocol", "smtp"); /** * creating a session instance of this property instance * @author gurt0582 * @param props is the property class * @param this is the authenticator instance an authenticator instance knows how * to obtain authentication for a network connection............. */ session = Session.getInstance(props, this); } /** * When authentication is required, * the system will invoke a method on the subclass (like getPasswordAuthentication). */ public PasswordAuthentication getPasswordAuthentication() { return authentication; } public void sendMessage( String to, String subject, String content) throws MessagingException { System.out.println("SENDING message from " + from + " to " + to); System.out.println(); MimeMessage msg = new MimeMessage(session); msg.addRecipients(Message.RecipientType.TO, to); msg.setSubject(subject); msg.setText(content); Transport.send(msg); } public void checkInbox(int mode) throws AuthenticationFailedException, MessagingException { if (mode == 0) return; boolean show = (mode & SHOW_MESSAGES) > 0; boolean clear = (mode & CLEAR_MESSAGES) > 0; String action = (show ? "Show" : "") + (show && clear ? " and " : "") + (clear ? "Clear" : ""); System.out.println(action + " INBOX for " + from); try { Store store = session.getStore(); store.connect(); Folder root = store.getDefaultFolder(); Folder inbox = root.getFolder("inbox"); inbox.open(Folder.READ_WRITE); Message[] msgs = inbox.getMessages(); if (msgs.length == 0 && show) { System.out.println("No messages in inbox"); } for (int i = 0; i < msgs.length; i++) { MimeMessage msg = (MimeMessage)msgs[i]; if (show) { System.out.println(" From: " + msg.getFrom()[0]); System.out.println(" Subject: " + msg.getSubject()); System.out.println(" Content: " + msg.getContent()); } if (clear) { msg.setFlag(Flags.Flag.DELETED, true); } } inbox.close(true); store.close(); System.out.println(); } catch(Exception e) { System.out.println("hi :"+e); e.printStackTrace(); } // Folder inbox = root.getFolder("inbox"); // inbox.open(Folder.READ_WRITE); // Message[] msgs = inbox.getMessages(); /* if (msgs.length == 0 && show) { System.out.println("No messages in inbox"); } for (int i = 0; i < msgs.length; i++) { MimeMessage msg = (MimeMessage)msgs[i]; if (show) { System.out.println(" From: " + msg.getFrom()[0]); System.out.println(" Subject: " + msg.getSubject()); System.out.println(" Content: " + msg.getContent()); } if (clear) { msg.setFlag(Flags.Flag.DELETED, true); } } inbox.close(true); store.close(); System.out.println();*/ } }