Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.emailreading;
- import java.util.Properties;
- import javax.mail.Address;
- 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.search.SearchTerm;
- public class EmailAttachReceiver {
- String fileName;
- public void downloadEmailAttachments(String userName, String password) {
- try {
- //Code used to connect to a gmail account
- //Starts Here
- 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", userName, password);
- Folder folderInbox = store.getFolder("INBOX");
- folderInbox.open(Folder.READ_ONLY);
- String RetailerName="Sudha Medicals";
- // Ends Here
- //Code used to search particular parameter from the email.
- //Starts Here
- SearchTerm searchCondition = new SearchTerm() {
- @Override
- public boolean match(Message message) {
- try {
- if (message.getSubject().contains("Sudha Medicals Reports")) {
- return true;
- }
- } catch (MessagingException ex) {
- ex.printStackTrace();
- }
- return false;
- }
- };
- //Ends Here.
- //Loop iteration based on matching parameters given in search method.
- //Starts Here
- Message[] arrayMessages = folderInbox.search(searchCondition);
- System.out.println(RetailerName);
- System.out.println("Total Email Count "+arrayMessages.length);
- for (int i = 0; i < arrayMessages.length; i++) {
- //i=arrayMessages.length-1;
- Message message = arrayMessages[i];
- //Address[] fromAddress = message.getFrom();
- //String from = fromAddress[0].toString();
- String subject = message.getSubject();
- // String sentDate = message.getSentDate().toString();
- String contentType = message.getContentType();
- String messageContent = "";
- String attachFiles = "";
- //Code to handle the multipart text in email body.
- //Starts Here
- if (contentType.contains("multipart")) {
- // System.out.println("contentType 1 "+contentType);
- Multipart multiPart = (Multipart) message.getContent();
- int numberOfParts = multiPart.getCount();
- // System.out.println("numberOfParts "+numberOfParts);
- for (int partCount = 0; partCount < numberOfParts; partCount++) {
- MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
- if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
- // System.out.println("partCount 1 " + partCount);
- } else {
- // System.out.println("partCount 2 "+partCount);
- messageContent = part.getContent().toString();
- }
- }
- } else if (contentType.contains("text/plain")
- || contentType.contains("text/html")) {
- //System.out.println("contentType 2 " + contentType);
- Object content = message.getContent();
- if (content != null) {
- messageContent = content.toString();
- }
- }
- //Ends Here
- /* Code To Replace The HTML Tags From The Content */
- //Starts Here
- String noHTMLString = messageContent.replaceAll("\\<.*?>","");
- String str = noHTMLString;
- String delimiter = "Total stock value";
- String[] temp = str.split(delimiter);
- System.out.println("Message #" + (i + 1) + ":");
- //System.out.println("\t Subject: " + subject );
- String[] temp1 = temp.length>1?temp[1].split("Stock adjustments done"):temp;
- if(temp1[0].contains(": ₹ ")){
- System.out.println("\t Subject: " + subject + " Total Stock Value "+ temp1[0].replace(": ₹ ", ""));
- //System.out.println( subject + " "+temp1[0].replace(": ₹ ", ""));
- //System.out.println("Total Stock Value:"+temp1[0].replace(": ₹ ", ""));
- }
- }
- folderInbox.close(false);
- store.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- //Ends Here
- public static void main(String[] args) {
- String userName = "abc@gmail.com";
- String password = "PSWD";
- EmailAttachReceiver receiver = new EmailAttachReceiver();
- receiver.downloadEmailAttachments(userName, password);
- }
- }
Add Comment
Please, Sign In to add comment