0

i'm trying to access my Gmail account using Java Gmail API then access the body of the first Email and then save it into text file using this path in my computer D:\Email , so any suggestion ? i have tried so many examples such as using saveFile but none of them worked for me !

NOTE: i want only to save the body of the Email

 package ReadingEmail;
import java.util.*;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
//===================================================================================

public class ReadingEmail {
      public static Scanner input = new Scanner(System.in);    
public static void main(String[] args) {
        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "imaps");
        try {
            Session session = Session.getInstance(props, null);
            Store store = session.getStore();
             System.out.println("enter your email");
             String email= input.nextLine();
              System.out.println("enter your password");
             String pass= input.nextLine();

            store.connect("imap.gmail.com", email, pass); //connect to email

            Folder inbox = store.getFolder("INBOX");// go to index 


            inbox.open(Folder.READ_ONLY); // open index 
//==============================================================================================
            int messageCount = inbox.getMessageCount();// line 20,21 show the number of emails
        System.out.println("Total Messages" + messageCount);
         int Message1 = messageCount;
       int Message2 = Message1 -1 ;


        Message msg1= inbox.getMessage(messageCount);
        Message msg2= inbox.getMessage(Message2);


         Address[] in1 = msg1.getFrom();
            for (Address address1 : in1) {}

//================================================================================
            Message msg = inbox.getMessage(inbox.getMessageCount());
            Address[] in = msg.getFrom();
            for (Address address : in) {

            }
            Multipart mp = (Multipart) msg.getContent();
            BodyPart bp = mp.getBodyPart(0);



            System.out.println("CONTENT:" + bp.getContent());
        } 
        catch (Exception ex) // wrong password
     { System.out.println("the email or password is wrong ");  }


    }
}
Harmlezz
  • 7,972
  • 27
  • 35

1 Answers1

0

Are you getting any error? Right now I am unable to see any code to save to a file.

Also when you use System.out.println("CONTENT:" + bp.getContent()); are you able to print the text to console or not?

AdityaKeyal
  • 1,208
  • 8
  • 14
  • No, there's no error.the program will ask the user to enter his email and password then it will show for him the body of the first email in his account at the output screen .. so i want to save that email body in a text file to my D drive ! – user3564384 Apr 23 '14 at 12:08
  • try to use the suggestion in this [link](http://stackoverflow.com/questions/2885173/java-how-to-create-and-write-to-a-file) or [this](http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/). I am asuming the body of the email is available in the SOP you have added above. – AdityaKeyal Apr 23 '14 at 12:16
  • i have tried the second link, when i try this line in the code bw.write(bp); it cause me an error tells (error: no suitable method found for write(BodyPart) bw.write(bp);) .. sorry but i'm new for java how i can solve it ? – user3564384 Apr 23 '14 at 12:39
  • You need to write a string into the bw.write(). So make your code like this : `bw.write(bp.getContent()); ` – AdityaKeyal Apr 23 '14 at 12:52
  • i still get an error `error: no suitable method found for write(Object) bw.write(bp.getContent()); ` and it does create new file but it's empty ! can you please help me ? – user3564384 Apr 23 '14 at 14:05
  • Apologies for the late response. change to the below code : `bw.write(bp.content().toString());` This will work. – AdityaKeyal Apr 23 '14 at 17:19
  • OMG IT"S WORK !!! THANK YOU SO SO SO MUCH !! I really appreciate your big help !!! :D – user3564384 Apr 23 '14 at 18:45