Is it possible to fetch mail source data using Java Api [Mail is not saved as a file]

2016-04-26T14:54:13

I am able to get mail subject and body contents using Java Api.

But in my application i received a template in an email which contains a url behind an image. I need to get that url, I found that the url was displayed when i view the source for that email manually.

Remember that i am not downloading the email instead i am connecting to mail server and then reading the mail data for any specific user

Is there a way that i can view the source of email like i am getting the subject of mail.

Here is the code:

    import org.jsoup.Jsoup;
    import javax.mail.*;
    import javax.mail.internet.MimeMessage;
    import java.io.IOException;
    import java.util.Properties;

    public class VerifyEmails {
     public Message message;
     public int i, n;
     public String result;
     public void check(String host, String user, String password) throws IOException, MessagingException {

      Properties properties = new Properties();
      properties.put("mail.imap.host", host);
      properties.put("mail.imap.user", user);
      properties.put("mail.imap.port", "143");
      properties.put("mail.imap.starttls.enable", "false");
      Session emailSession = Session.getDefaultInstance(properties);
      Store store = emailSession.getStore("imap");
      System.out.println("test1 " + store);
      store.connect(host, user, password);
      Folder emailFolder = store.getFolder("INBOX");
      emailFolder.open(Folder.READ_ONLY);
      Message[] messages = emailFolder.getMessages();
      System.out.println("messages.length---" + messages.length);
      for (i = 0, n = messages.length; i < n; i++) {
       message = messages[i];
       System.out.println("---------------------------------");
       System.out.println("Email Number " + (i + 1));
       System.out.println("Subject: " + message.getSubject());
       System.out.println("From: " + message.getFrom()[0]);
       System.out.println("Email contents are :" + message.getContentType());
       System.out.println("Email headers are :" + message.getContent());
      }

      if (message instanceof MimeMessage) {
       MimeMessage m = (MimeMessage) message;
       Object contentObject = message.getContent();
       if (contentObject instanceof Multipart) {
        BodyPart clearTextPart = null;
        BodyPart htmlTextPart = null;
        Multipart content = (Multipart) contentObject;

        int count = content.getCount();
        for (int i = 0; i < count; i++) {
         BodyPart part = content.getBodyPart(i);
         if (part.isMimeType("text/plain")) {
          clearTextPart = part;
          String test = String.valueOf(clearTextPart.getAllHeaders());

          System.out.println("check1" + test);
          break;
         } else if (part.isMimeType("text/html")) {

          htmlTextPart = part;
         }
        }
        if (clearTextPart != null) {
         result = (String) clearTextPart.getContent();
         String test = String.valueOf(clearTextPart.getAllHeaders());

         System.out.println("check2" + test);

         System.out.println("plain text: " + result);
        } else if (htmlTextPart != null) {
         String html = (String) htmlTextPart.getContent();
         result = Jsoup.parse(html).text();
         System.out.println("html text: " + result);
        }
       } else if (contentObject instanceof String) // a simple text message
       {
        result = (String) contentObject;
        System.out.println("String text: " + result);
       } else // not a mime message
       {
        result = null;
        System.out.println("null : " + result);
       }
       emailFolder.close(false);
       store.close();
      }
     }
}

Copyright License:
Author:「vish」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/36857382/is-it-possible-to-fetch-mail-source-data-using-java-api-mail-is-not-saved-as-a

About “Is it possible to fetch mail source data using Java Api [Mail is not saved as a file]” questions

I am able to get mail subject and body contents using Java Api. But in my application i received a template in an email which contains a url behind an image. I need to get that url, I found that ...
I have been using Java mail API to send mails generated through the Java program. What I want to do now is to send a mail through Thunderbird mail client so that the mail get saved to the sent items
How to add mail accounts to mail server through java code using mail api? Is it possible to add mail accounts using Runtime class?
Trying to send an email using java mail api. And I keep getting MailConnectException. I have tried multiple ways to solve it without success. Exception is thrown by this statement transport.conn...
I use JavaMail API to download mails via IMAP. I want to save them locally, in files. I have a DB, but I only want to store some meta-data in it (like mail file location). Is there a possibility, ...
i have written a program which has a form consisting of name , phone no and email and a submit button to send the above details to a mail address , i have also tried using intent and it works fine ...
Is there a way to fetch the mail bodies of multiple emails with a single call to an IMAP server using the Javamail API? I know I can get to the body of a given message using the Message.getContent()
I have an email server to which people reply from emails that have been sent to them from that mail server. Then I want to fetch these replies using the Java Mail API, but I am running into problem...
I'm trying to compress a MIME message using java mail APIs and IAIK. I've written the following code: MimeBodyPart wrappedMessage = new MimeBodyPart(new InternetHeaders(), content); CompressedCont...
I wanna know how to fetch new email which just came in my yahoo mail inbox using java code. Instead of using a time-interval based job I want that whenever a new mail arrives in my yahoo mail inbox,

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.