Wednesday 27 March 2013

Read Inbox in Java (through IMAP host)


The following is a simple Java code for reading inbox mails of Yahoo from Internet Messaging Access Protocol  or IMAP host for Yahoo.
Caution: The application downloads entire inbox to your desktop application on the console screen.
So be careful as it takes long time for more number of emails.
You may further enhance the following code to download only unread emails.
Java code for filtering the unread emails will be published soon  . 

package inbox;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.Properties;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;


/**
 *
 * @author SUNEET
 */
public class Inbox {

    /**
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) throws MessagingException, IOException {
        // TODO code application logic here
        Properties props = System.getProperties();
        props.setProperty( "mail.transport.protocol", "smtps");
        try {
                Session session = Session.getDefaultInstance(props, null);
                Store store = session.getStore("imaps");

                // IMAP host for yahoo.
                // Replace <username> with the valid username of your Email ID.
                // Replace <password> with a valid password of your Email ID.              
                // IMAP host for yahoo.
                store.connect("imap.mail.yahoo.com", "<username>", "<password>");

                System.out.println(store);
             
                Folder inbox = store.getFolder("Inbox");
                inbox.open(Folder.READ_ONLY);
             
                BufferedReader optionReader = new BufferedReader(new InputStreamReader(System.in));
                showAllMails(inbox);
             
        } catch (NoSuchProviderException e) {
            System.out.println(e.toString());
            System.exit(1);
        } catch (MessagingException e) {
            System.out.println(e.toString());
            System.exit(2);
        }

    }  
 
 
    static public void showAllMails(Folder inbox){
        try {
            Message msg[] = inbox.getMessages();
            System.out.println("MAILS: "+msg.length);
            for(Message message:msg) {
                try {
                    System.out.println("DATE: "+message.getSentDate().toString());
                    System.out.println("FROM: "+message.getFrom()[0].toString());          
                    System.out.println("SUBJECT: "+message.getSubject().toString());
                    System.out.println("CONTENT: "+message.getContent().toString());
                    System.out.println("******************************************");
                } catch (Exception e) {
                    System.out.println("No Information");
                }
            }
        } catch (MessagingException e) {
            System.out.println(e.toString());
        }
    }
}

Saturday 26 January 2013

Send SMTP E-Mail in Java

This post will give you the understanding of very basic Java required to send SMTP e-mail.
You basically need to download (click here) "mail-1.4.jar".
Link the "mail-1.4.jar" file to your library: (See pic below)
Compile the following java code:

import java.util.Properties;
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;


/**
 *
 * @author SUNEET
 */
public class Web {

    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) {                
final String username = "s_lady@rocketmail.com";
final String password = "Password";
                //set properties
Properties props = new Properties();
props.setProperty( "mail.transport.protocol", "smtps");
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.host", "plus.smtp.mail.yahoo.com");
                props.put("mail.smtp.port", "465");
                props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                props.put("mail.smtp.ssl","true");
                props.put("mail.stmp.user", "username");
                props.put("mail.smtp.password", "password");
                //password authetication
Session session = Session.getInstance(props,
 new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
 });
                //send e-mail
try {

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("s_lady@rocketmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("s_lady@rocketmail.com"));
message.setSubject("Test mail");
message.setText("Dear reciever,"
+ "\n\n No spam to my email, please!");

Transport.send(message);

System.out.println("Email Sended.");

} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}