| 4241 |
anupam.sin |
1 |
package in.shop2020.util;
|
|
|
2 |
|
|
|
3 |
import java.io.File;
|
|
|
4 |
import java.io.FileOutputStream;
|
|
|
5 |
import java.io.IOException;
|
|
|
6 |
import java.io.InputStream;
|
|
|
7 |
import java.util.Calendar;
|
|
|
8 |
import java.util.Date;
|
|
|
9 |
import java.util.Properties;
|
|
|
10 |
import java.io.*;
|
|
|
11 |
import javax.activation.DataHandler;
|
|
|
12 |
import javax.mail.BodyPart;
|
|
|
13 |
import javax.mail.Folder;
|
|
|
14 |
import javax.mail.Message;
|
|
|
15 |
import javax.mail.MessagingException;
|
|
|
16 |
import javax.mail.Multipart;
|
|
|
17 |
import javax.mail.NoSuchProviderException;
|
|
|
18 |
import javax.mail.Part;
|
|
|
19 |
import javax.mail.Session;
|
|
|
20 |
import javax.mail.Store;
|
|
|
21 |
import javax.mail.search.ComparisonTerm;
|
|
|
22 |
import javax.mail.search.ReceivedDateTerm;
|
|
|
23 |
import javax.mail.internet.*;
|
|
|
24 |
|
|
|
25 |
public class EmailUtils {
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
private static final int DESCRIPTION_MAX_WIDTH = 10;
|
|
|
29 |
|
|
|
30 |
private static void getAttachment(Part p) throws MessagingException, IOException {
|
|
|
31 |
String listOfFiles = "";
|
|
|
32 |
if (p.isMimeType("multipart/*")) {
|
|
|
33 |
Multipart multipart = (Multipart) p.getContent();
|
|
|
34 |
for (int x = 0; x < multipart.getCount(); x++) {
|
|
|
35 |
BodyPart bodyPart = multipart.getBodyPart(x);
|
|
|
36 |
if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
|
|
|
37 |
continue; // we want attachments only
|
|
|
38 |
}
|
|
|
39 |
String fileName = bodyPart.getFileName();
|
|
|
40 |
InputStream is = bodyPart.getInputStream();
|
|
|
41 |
File f = new File("/var/crm/archive/" + fileName);
|
|
|
42 |
FileOutputStream fos = new FileOutputStream(f);
|
|
|
43 |
byte[] buf = new byte[4096];
|
|
|
44 |
int bytesRead;
|
|
|
45 |
while((bytesRead = is.read(buf))!=-1) {
|
|
|
46 |
fos.write(buf, 0, bytesRead);
|
|
|
47 |
}
|
|
|
48 |
fos.close();
|
|
|
49 |
listOfFiles += "/var/crm/archive/" + fileName + ";";
|
|
|
50 |
if (listOfFiles.length() > DESCRIPTION_MAX_WIDTH) {
|
|
|
51 |
listOfFiles = listOfFiles.substring(0, DESCRIPTION_MAX_WIDTH);
|
|
|
52 |
listOfFiles += "\n\nTHIS TEXT IS TRUNCATED. PLEASE VISIT INBOX TO SEE COMPLETE DETAILS.";
|
|
|
53 |
}//end of if
|
|
|
54 |
}//end for loop
|
|
|
55 |
}//end if
|
|
|
56 |
System.out.println("name of attachments: " + listOfFiles);
|
|
|
57 |
}//end getAttachment
|
|
|
58 |
|
|
|
59 |
public static void main(String[] args) throws MessagingException, IOException {
|
|
|
60 |
/* Store store = null;
|
|
|
61 |
Properties props = System.getProperties();
|
|
|
62 |
props.setProperty("mail.store.protocol", "imaps");
|
|
|
63 |
try {
|
|
|
64 |
Session session = Session.getDefaultInstance(props, null);
|
|
|
65 |
store = session.getStore("imaps");
|
|
|
66 |
store.connect("imap.gmail.com", "anupam.singh@shop2020.in", "M&&840aa");
|
|
|
67 |
} catch (NoSuchProviderException e) {
|
|
|
68 |
e.printStackTrace();
|
|
|
69 |
} catch (MessagingException e) {
|
|
|
70 |
e.printStackTrace();
|
|
|
71 |
}
|
|
|
72 |
Folder inbox = store.getFolder("inbox");
|
|
|
73 |
inbox.open(Folder.READ_ONLY);
|
|
|
74 |
|
|
|
75 |
Date lastReceivedEmailTimestamp = Calendar.getInstance().getTime();
|
|
|
76 |
lastReceivedEmailTimestamp.setHours(10);
|
|
|
77 |
for (Message message : inbox.search(new ReceivedDateTerm(ComparisonTerm.GE, lastReceivedEmailTimestamp)))
|
|
|
78 |
{
|
|
|
79 |
EmailUtils.getAttachment(message);
|
|
|
80 |
}
|
|
|
81 |
*/
|
|
|
82 |
}
|
|
|
83 |
}
|