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 8464091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:38:12+00:00 2026-06-10T14:38:12+00:00

I have two activities, one for recieving emails using Imap the second is to

  • 0

I have two activities, one for recieving emails using Imap the second is to send email using SMTP. The button for send email activity is inside the Recieve Email activity, So the Recieve Email Activity has to run before Send Email ACitivty..

My problem is I keep getting an Exception saying
Could not connect to SMTP host: localhost, port: 25 at Transport.send(new_message)

However, if I run the same activity without calling the recieve email activity first, then it works without any problem.
Can someone help me as to why this happens?

Here’s the Code for
RecieveEmail Async task (the full Activity code is too long)

 try{


               Properties props = new Properties();
               props.setProperty( "mail.imaps.socketFactory.class", "com.X509TrustAll.DummySSLSocketFactory" );
               Log.v("EmailList", "Stting properties");

              // Get the default Session object.
              session = Session.getDefaultInstance(props, null);
              Log.v("EmailList", "Geting Default Instance");

              // Get a Store object that implements the specified protocol.
              store = session.getStore("imaps");
              Log.v("EmailList", "Getting Sesion");

              //Connect to the current host using the specified username and password.
              Log.v("EmailList", "Connecting...");
              store.connect(host,port, user, password);
              Log.v("EmailList", "Connected");

              //Create a Folder object corresponding to the given name.
              Folder[] folders = store.getDefaultFolder().list("*");
              Log.v("EmailList", "Got Folder List");
              folder =  folders[5];



              // Open the Folder.
              Log.v("EmailList", "Opening Folder");
              folder.open(Folder.READ_ONLY);

              Log.v("EmailList", "Getting Messages");
              messages = folder.getMessages();

              Log.v("EmailList", "Got Messages");


          } catch (Exception e){

              Log.v("EmailList", "Exception");

          }
        }

              for (int i = lastMessageNumber -1;  i >= interateUpto; i--) {

                  Log.v("EmailRecieve", "Email no. " + Integer.toString(i));

                  javax.mail.Address[] froms = messages[i].getFrom();
                  String emailAdress = froms == null ? null : ((InternetAddress) froms[0]).getAddress();


                  FromAsync.add(emailAdress);

                  SubjectAsync.add(messages[i].getSubject());

                  SentDateAsync.add(messages[i].getSentDate().toString());


                  Part part = messages[i];

                  checkAttachments(part);



                  if (messages[i].isSet(Flag.SEEN)){

                      SeenAsync.add("true");

                  }else{

                      SeenAsync.add("false");

                  }


              }



              lastMessageNumber = lastMessageNumber - 19;   //new last number is stored

              folder.close(false);
              store.close();

        } catch (Exception e){

                  Log.v("EmailList", "Exception");

        }



      Log.v("EmailList", "Retrieve Email Finished");



        return null;

    }

And Here’s the code for Send Email (again the full code is too long so I’m only posting whats relevant, i.e UI etc. are not posted

@Override
    protected Void doInBackground(Void... arg0) {

        //Recipient's email ID needs to be mentioned.
          String to = To.getText().toString();

          // Sender's email ID needs to be mentioned
          String from = "foobar@foobar.hehe";

          // The Host
          String smtphost = "www.isleworthsyon.hounslow.sch.uk";

          // Get system properties
          Properties smtpproperties = System.getProperties();

          // Setup mail server
          smtpproperties.setProperty("mail.smtp.host", smtphost);

          // Get the default Session object.
          Session smtpsession = Session.getDefaultInstance(smtpproperties);

          try{
             MimeMessage new_message = new MimeMessage(smtpsession);

             // Set From:
             new_message.setFrom(new InternetAddress(from));

             // Set To:
             new_message.addRecipient(Message.RecipientType.TO,
                                      new InternetAddress(to));

             // Set Subject: 
             new_message.setSubject(Subject.getText().toString());

             if (hasAttachment == true){

                 // Create the message part 
                 BodyPart messageBodyPart = new MimeBodyPart();

                 // Fill the message
                 messageBodyPart.setText(Body.getText().toString());

                 // Create a multipar message
                 Multipart multipart = new MimeMultipart();

                 // Set text message part
                 multipart.addBodyPart(messageBodyPart);

                 // Iterating over all Attachments    
                 for (int i=0; i < attachmentFiles.size(); i++){

                    if (attachmentFiles.get(i).equals("null") != true)    {

                         messageBodyPart = new MimeBodyPart();
                         String filename = attachmentFiles.get(i);
                         FileDataSource source = new FileDataSource(filename);
                         messageBodyPart.setDataHandler(new DataHandler(source));
                         messageBodyPart.setFileName(source.getName());
                         multipart.addBodyPart(messageBodyPart);

                    }
                 }


                 new_message.setContent(multipart);

             }else{

                 // Set Body if not attaching anything
                 new_message.setText(Body.getText().toString());

             }

             // Send message
             Transport.send(new_message);
             sendingSuccesfull = true;
          }catch (MessagingException mex) {
             sendingSuccesfull = false;
             Log.v("Email Compose", "Message Sending Failed, Details: " + mex.getMessage());

          }

        return null;
    }
  • 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-06-10T14:38:14+00:00Added an answer on June 10, 2026 at 2:38 pm

    Try cleaning up these common mistakes and see if that solves the problem.

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

Sidebar

Related Questions

I have one problem using ActivityGroup . I have two activities inside an ActivityGroup
I have one problem using ActivityGroup. I have two activities inside an ActivityGroup and
This is my situation. I have two activities: ONE and TWO. In TWO activity
I have one class called Global and two other activities. In each activity I
I have two activities. In first I come to the second activity from first
I have two Activities: one with VideoView attached to MediaPlayer and the second one.
I have two different activities. The first launches the second one. In the second
I have two activities. The first one executes the second one. Intent i =
I have two activities: one displays an image and a button, the other displays
I have two activities, one is main and I have another activity called Test

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.