Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6793267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:04:49+00:00 2026-05-26T18:04:49+00:00

I’m trying this code I found with google, but it didn’t connect to my

  • 0

I’m trying this code I found with google, but it didn’t connect to my gmail inbox. Why?

I have this error message:

 --------------processing mails started-----------------
    getting the session for accessing email.
    Not able to process the mail reading.
    javax.mail.MessagingException: Connection timed out: connect;
      nested exception is:
        java.net.ConnectException: Connection timed out: connect
        at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:618)
        at javax.mail.Service.connect(Service.java:291)
        at javax.mail.Service.connect(Service.java:172)
        at readEmails.processMail(readEmails.java:47)
        at readEmails.(readEmails.java:19)
        at readEmails.main(readEmails.java:165)
    Caused by: java.net.ConnectException: Connection timed out: connect
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.PlainSocketImpl.doConnect(Unknown Source)
        at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
        at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
        at com.sun.mail.iap.Protocol.(Protocol.java:109)
        at com.sun.mail.imap.protocol.IMAPProtocol.(IMAPProtocol.java:104)
        at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:585)

The code is:

import javax.mail.AuthenticationFailedException;
import javax.mail.Folder;
import javax.mail.FolderClosedException;
import javax.mail.FolderNotFoundException;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.ReadOnlyFolderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.StoreClosedException;
import javax.mail.internet.InternetAddress;

public class readEmails {

//Constructor Call
public readEmails() {
   processMail();
}

//Responsible for printing Data to Console
private void printData(String data) {
   System.out.println(data);
}

public void processMail() {
   Session session = null;
   Store store = null;
   Folder folder = null;
   Message message = null;
   Message[] messages = null;
   Object messagecontentObject = null;
   String sender = null;
   String subject = null;
   Multipart multipart = null;
   Part part = null;
   String contentType = null;

   try {
      printData("--------------processing mails started-----------------");
      session = Session.getDefaultInstance(System.getProperties(), null);

      printData("getting the session for accessing email.");
      store = session.getStore("imap");

      store.connect("imap.gmail.com","myemail@gmail.com","mypassword");
      printData("Connection established with IMAP server.");

      // Get a handle on the default folder
      folder = store.getDefaultFolder();

      printData("Getting the Inbox folder.");

      // Retrieve the "Inbox"
      folder = folder.getFolder("inbox");

      //Reading the Email Index in Read / Write Mode
      folder.open(Folder.READ_WRITE);

      // Retrieve the messages
      messages = folder.getMessages();

      // Loop over all of the messages
      for (int messageNumber = 0; messageNumber < messages.length; messageNumber++) {
           // Retrieve the next message to be read
       message = messages[messageNumber];

           // Retrieve the message content
           messagecontentObject = message.getContent();

           // Determine email type
           if (messagecontentObject instanceof Multipart) {
               printData("Found Email with Attachment");
               sender = ((InternetAddress) message.getFrom()[0]).getPersonal();

               // If the "personal" information has no entry, check the address for the sender information
               printData("If the personal information has no entry, check the address for the sender information.");

           if (sender == null) {
           sender = ((InternetAddress) message.getFrom()[0]).getAddress();
           printData("sender in NULL. Printing Address:" + sender);
           }
               printData("Sender -." + sender);

               // Get the subject information
               subject = message.getSubject();

               printData("subject=" + subject);

               // Retrieve the Multipart object from the message
               multipart = (Multipart) message.getContent();

               printData("Retrieve the Multipart object from the message");

               // Loop over the parts of the email
               for (int i = 0; i < multipart.getCount(); i++) {
                    // Retrieve the next part
                    part = multipart.getBodyPart(i);

                    // Get the content type
                    contentType = part.getContentType();

                   // Display the content type
           printData("Content: " + contentType);

                   if (contentType.startsWith("text/plain")) {
            printData("---------reading content type text/plain  mail -------------");
           } else {
            // Retrieve the file name
            String fileName = part.getFileName();
            printData("retrive the fileName="+ fileName);
           }
          }
       } else {
          printData("Found Mail Without Attachment");
          sender = ((InternetAddress) message.getFrom()[0]).getPersonal();

              // If the "personal" information has no entry, check the address for the sender information
          printData("If the personal information has no entry, check the address for the sender information.");

              if (sender == null) {
        sender = ((InternetAddress) message.getFrom()[0]).getAddress();
        printData("sender in NULL. Printing Address:" + sender);
         }

             // Get the subject information
         subject = message.getSubject();
         printData("subject=" + subject);
     }
      }

      // Close the folder
      folder.close(true);

      // Close the message store
      store.close();
  } catch(AuthenticationFailedException e) {
     printData("Not able to process the mail reading.");
     e.printStackTrace();
  } catch(FolderClosedException e) {
     printData("Not able to process the mail reading.");
     e.printStackTrace();
  } catch(FolderNotFoundException e) {
     printData("Not able to process the mail reading.");
     e.printStackTrace();
  }  catch(NoSuchProviderException e) {
     printData("Not able to process the mail reading.");
     e.printStackTrace();
  } catch(ReadOnlyFolderException e) {
     printData("Not able to process the mail reading.");
     e.printStackTrace();
  } catch(StoreClosedException e) {
     printData("Not able to process the mail reading.");
     e.printStackTrace();
  } catch (Exception e) {
     printData("Not able to process the mail reading.");
     e.printStackTrace();
  }
}

//Main  Function for The readEmail Class
public static void main(String args[]) {
    //Creating new readEmail Object
    readEmails readMail = new readEmails();

    //Calling processMail Function to read from IMAP Account
    readMail.processMail();
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:04:49+00:00Added an answer on May 26, 2026 at 6:04 pm

    You are connecting with protocol imap (IMAP without SSL), which isn’t supported by GMail. You need to use imaps (IMAP with SSL).

    See GMail help and the JavaMail FAQ entry on GMail

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.