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