Created
November 1, 2013 15:08
-
-
Save leandrosilva/7266837 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.mamutelanoso; | |
import java.io.IOException; | |
import java.util.Properties; | |
import javax.mail.BodyPart; | |
import javax.mail.Folder; | |
import javax.mail.Message; | |
import javax.mail.MessagingException; | |
import javax.mail.Multipart; | |
import javax.mail.Part; | |
import javax.mail.Session; | |
import javax.mail.Store; | |
import javax.mail.internet.MimeBodyPart; | |
import javax.mail.internet.MimeMultipart; | |
public class Main { | |
public static void main(String[] args) throws MessagingException, IOException { | |
System.out.println("=== Mamute Lanoso ==="); | |
Properties props = new Properties(); | |
props.setProperty("mail.store.protocol", "imaps"); | |
Session session = Session.getInstance(props, null); | |
Store store = session.getStore(); | |
store.connect("imap.gmail.com", "xyz@gmail.com", "1234"); | |
Folder inbox = store.getFolder("INBOX"); | |
inbox.open(Folder.READ_ONLY); | |
Message msg = inbox.getMessage(inbox.getMessageCount() - 11); | |
System.out.println("Subject ::: " + msg.getSubject()); | |
String contentType = msg.getContentType(); | |
if (contentType.contains("multipart")) { | |
Multipart multiPart = (Multipart) msg.getContent(); | |
int partCount = multiPart.getCount(); | |
for (int j = 0; j < partCount; j++) { | |
BodyPart part = multiPart.getBodyPart(j); | |
String disposition = part.getDisposition(); | |
if (disposition == null) { | |
// Check if plain | |
MimeBodyPart mbp = (MimeBodyPart) part; | |
if (mbp.getContent() instanceof MimeMultipart) { | |
MimeMultipart mmp = (MimeMultipart) mbp.getContent(); | |
String bodyContent = mmp.getBodyPart(0).getContent().toString(); | |
System.out.println("Body content ::: " + bodyContent); | |
} else { | |
String msgText = multiPart.getBodyPart(0).getContent().toString(); | |
System.out.println("Message ::: " + msgText); | |
} | |
} else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) { | |
System.out.println("Attachment data ::: " + part.getContent().toString()); | |
// MailUtility.saveMail(part.getInputStream(), part.getFileName()); | |
} else { | |
String textMsg = part.getContent() != null ? part.getContent().toString() : ""; | |
System.out.println("Text msg ::: " + textMsg); | |
} | |
} | |
} else { | |
System.out.println("Msg body ::: " + msg.getContent().toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment