1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import com.sun.mail.pop3.POP3Store;
import javax.mail.Flags;
public class EmailReceiver {
public void getEmail(String host, String port, String userName, String password)
throws MessagingException, IOException {
// sets POP3 properties
Properties properties = new Properties();
properties.put("mail.pop3.host", host);
properties.put("mail.pop3.port", port);
// sets POP3S properties
properties.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.pop3.socketFactory.fallback", "false");
properties.setProperty("mail.pop3.socketFactory.port", "995");
// connects to the POP3 server
Session session = Session.getDefaultInstance(properties);
POP3Store emailStore = (POP3Store) session.getStore("pop3");
emailStore.connect(userName, password);
Folder emailFolder = emailStore.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
Message[] messages = emailFolder.getMessages();
for (int messageCount = 0; messageCount < messages.length; messageCount++) {
Message aMessage = messages[messageCount];
String senderAddress = aMessage.getFrom()[0].toString();
String subject = aMessage.getSubject();
// parses recipient address in To field
String toAddresses = "";
Address[] listTO = aMessage.getRecipients(RecipientType.TO);
if (listTO != null) {
for (int toCount = 0; toCount < listTO.length; toCount++) {
toAddresses += listTO[toCount].toString() + ", ";
}
}
if (toAddresses.length() > 1) {
toAddresses = toAddresses.substring(0, toAddresses.length() - 2);
}
// parses recipient addresses in CC field
String ccAddresses = "";
Address[] listCC = aMessage.getRecipients(RecipientType.CC);
if (listCC != null) {
for (int ccCount = 0; ccCount < listCC.length; ccCount++) {
ccAddresses = listCC[ccCount].toString() + ", ";
}
}
if (ccAddresses.length() > 1) {
ccAddresses = ccAddresses.substring(0, ccAddresses.length() - 2);
}
String sentDate = aMessage.getSentDate().toString();
String contentType = aMessage.getContentType();
String textMessage = "";
String attachFiles = "";
if (contentType.contains("text/plain") || contentType.contains("text/html")) {
textMessage = aMessage.getContent() != null ? aMessage.getContent().toString() : "";
} else if (contentType.contains("multipart")) {
Multipart multiPart = (Multipart) aMessage.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
BodyPart part = multiPart.getBodyPart(partCount);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
attachFiles += part.getFileName() + ", ";
storeAttachment(part);
} else {
textMessage = part.getContent() != null ? part.getContent().toString() : "";
}
}
if (attachFiles.length() > 1) {
attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
}
}
// prints out the message details
System.out.println("Message #" + messageCount + ":");
System.out.println("\t From: " + senderAddress);
javax.mail.Folder[] folders = emailStore.getDefaultFolder().list("*");
for (javax.mail.Folder folder : folders) {
if ((folder.getType() & javax.mail.Folder.HOLDS_MESSAGES) != 0) {
System.out.println(folder.getFullName() + ": " + folder.getMessageCount());
}
}
if (senderAddress.equalsIgnoreCase("test@gmail.com")){
aMessage.setFlag(Flags.Flag.DELETED, true);
}
//System.out.println("\t To: " + toAddresses);
//System.out.println("\t CC: " + ccAddresses);
System.out.println("\t Subject: " + subject);
//System.out.println("\t Sent Date: " + sentDate);
//System.out.println("\t Message: " + textMessage);
//System.out.println("\t Attachments: " + attachFiles);
//System.out.println("--------------------------------------");
}
// closes the folder and disconnects from the server
emailFolder.close(false);
emailStore.close();
}
private void storeAttachment(BodyPart part) throws MessagingException, IOException {
String destFilePath = "C:/Temp6/" + part.getFileName();
FileOutputStream output = new FileOutputStream(destFilePath);
InputStream input = part.getInputStream();
byte[] buffer = new byte[4096];
int byteRead;
while ((byteRead = input.read(buffer)) != -1) {
output.write(buffer, 0, byteRead);
}
output.close();
}
} |
Partager