How to Implement Gateway Service something similar to Oracle API gateway Using Java and Java based Open Source frameworks only?

I want to receive an email using java api?

  • import javax.mail.* ; import javax.mail.internet.* ; import javax.activation.* ; import java.util.*; import java.io.*; import com.sun.mail.pop3.POP3Store; ////////////// CLASS pop Authenticator ////////////// class popAuthenticator extends javax.mail.Authenticator { String username = ""; String password = ""; public popAuthenticator(String username, String password) { this.username = username; this.password = password; } public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } } class EmailReceiveTest { public static void main(String[] args) { String mailPop3Host = "pop.gmail.com"; String mailStoreType = "pop3"; String mailUser = "[email protected]"; String mailPassword = ""; receiveEmail(mailPop3Host, mailStoreType, mailUser, mailPassword); } public static void receiveEmail(String pop3Host, String storeType, String user, String password) { try { Properties properties = new Properties(); properties.put("mail.pop3.host", pop3Host); //properties.put("mail.pop3.user",u… properties.put("mail.transport.prot… "pop3"); //properties.put("mail.pop3.port", "995"); //properties.put("mail.pop3.starttl… "true"); properties.put("mail.pop3.auth", "true"); //properties.put("mail.pop3.socketF… "995"); //properties.put("mail.pop3.socketF… //properties.put("mail.pop3.socketF… "false"); //Email Address Authentification Authenticator auth = new popAuthenticator(user,password); Session emailSession = Session.getDefaultInstance(properties,au… POP3Store emailStore = (POP3Store) emailSession.getStore(storeType); emailStore.connect(user, password); Folder emailFolder = emailStore.getFolder("INBOX"); emailFolder.open(Folder.READ_ONLY); Message[] messages = emailFolder.getMessages(); for (int i = 0; i < messages.length; i++) { Message message = messages[i]; System.out.println("==============… System.out.println("Email #" + (i + 1)); System.out.println("Subject: " + message.getSubject()); System.out.println("From: " + message.getFrom()[0]); System.out.println("Text: " + message.getContent().toString()); } emailFolder.close(false); emailStore.close(); }catch (NoSuchProviderException e) {e.printStackTrace();} catch (MessagingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} } } I AM FACING THE FOLLOWING ERROR javax.mail.AuthenticationFailedExcepti… EOF on socket at com.sun.mail.pop3.POP3Store.protocolConn… at javax.mail.Service.connect(Service.java:… at javax.mail.Service.connect(Service.java:… at javax.mail.Service.connect(Service.java:… at test.EmailReceiveTest.receiveEmail(Rec2.… at test.EmailReceiveTest.main(Rec2.java:42) Process completed.

  • Answer:

    here I have an simple program using java to recieve the mail... Steps involved in receive mail: Step 1: Define the mail properties (i.e.) Define the protocol, mail server by using the properties class. Step 2: Create the session for read the mail with the properties which we already defined. Step 3: Create and connect the store for read the mail. Step 4: Define and open the folder which we need to read. Open the folder in read-only mode. Step 5: Search the unread contents in the specified folder and stored it into messages array. Step 6: Display the messages. NOW THE PROGRAM IS AS FOLLOWS.. package com.info.mail; import java.io.*; import java.util.*; import javax.mail.*; import javax.mail.Flags.Flag; import javax.mail.search.FlagTerm; public class MailReader { Folder inbox; //Constructor of the calss. public MailReader() { /* Set the mail properties */ Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); try { /* Create the session and get the store for read the mail. */ Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("imap.gmail.com","<mail ID> ", "<Password>"); /* Mention the folder name which you want to read. */ inbox = store.getFolder("Inbox"); System.out.println("No of Unread Messages : " + inbox.getUnreadMessageCount()); /*Open the inbox using store.*/ inbox.open(Folder.READ_ONLY); /* Get the messages which is unread in the Inbox*/ Message messages[] = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false)); /* Use a suitable FetchProfile */ FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfile.Item.CONTENT_INFO); inbox.fetch(messages, fp); try { printAllMessages(messages); inbox.close(true); store.close(); } catch (Exception ex) { System.out.println("Exception arise at the time of read mail"); ex.printStackTrace(); } } catch (NoSuchProviderException e) { e.printStackTrace(); System.exit(1); } catch (MessagingException e) { e.printStackTrace(); System.exit(2); } } public void printAllMessages(Message[] msgs) throws Exception { for (int i = 0; i < msgs.length; i++) { System.out.println("MESSAGE #" + (i + 1) + ":"); printEnvelope(msgs[i]); } } /* Print the envelope(FromAddress,ReceivedDate,Subjec… */ public void printEnvelope(Message message) throws Exception { Address[] a; // FROM if ((a = message.getFrom()) != null) { for (int j = 0; j < a.length; j++) { System.out.println("FROM: " + a[j].toString()); } } // TO if ((a = message.getRecipients(Message.RecipientT… != null) { for (int j = 0; j < a.length; j++) { System.out.println("TO: " + a[j].toString()); } } String subject = message.getSubject(); Date receivedDate = message.getReceivedDate(); String content = message.getContent().toString(); System.out.println("Subject : " + subject); System.out.println("Received Date : " + receivedDate.toString()); System.out.println("Content : " + content); getContent(message); } public void getContent(Message msg) { try { String contentType = msg.getContentType(); System.out.println("Content Type : " + contentType); Multipart mp = (Multipart) msg.getContent(); int count = mp.getCount(); for (int i = 0; i < count; i++) { dumpPart(mp.getBodyPart(i)); } } catch (Exception ex) { System.out.println("Exception arise at get Content"); ex.printStackTrace(); } } public void dumpPart(Part p) throws Exception { // Dump input stream .. InputStream is = p.getInputStream(); // If "is" is not already buffered, wrap a BufferedInputStream // around it. if (!(is instanceof BufferedInputStream)) { is = new BufferedInputStream(is); } int c; System.out.println("Message : "); while ((c = is.read()) != -1) { System.out.write(c); } } public static void main(String args[]) { new MailReader(); } }

Destiny Maker at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.