Есть задача получать письма в разных папках на почтовом ящике и по своему обрабатывать.
import com.sun.xml.internal.messaging.saaj.packaging.mime .MessagingException;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class ReceiveMailImap {
public ReceiveMailImap() {}
//
// inspired by :
// http://www.mikedesjardins.net/content/2008/03/using-javamail-to-read-and-extract/
//
public static void doit() throws MessagingException, IOException, javax.mail.MessagingException {
Folder folder = null;
Store store = null;
try {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, new mMailAuthenticator("[email protected]", "*******"));
// session.setDebug(true);
store = session.getStore("imaps");
store.connect("imap.mail.ru","<[email protected]>", "<*******>");
folder = store.getFolder("Inbox");
folder.open(Folder.READ_WRITE);
Message messages[] = folder.getMessages();
System.out.println("No of Messages : " + folder.getMessageCount());
System.out.println("No of Unread Messages : " + folder.getUnreadMessageCount());
for (int i=0; i < messages.length; ++i) {
System.out.println("MESSAGE #" + (i + 1) + ":");
Message msg = messages[i];
/*
if we don''t want to fetch messages already processed
if (!msg.isSet(Flags.Flag.SEEN)) {
String from = "unknown";
...
}
*/
String from = "unknown";
if (msg.getReplyTo().length >= 1) {
from = msg.getReplyTo()[0].toString();
}
else if (msg.getFrom().length >= 1) {
from = msg.getFrom()[0].toString();
}
String subject = msg.getSubject();
System.out.println("Saving ... " + subject +" " + from);
// you may want to replace the spaces with "_"
// the TEMP directory is used to store the files
String filename = "c:/temp/" + subject;
saveParts(msg.getContent(), filename);
msg.setFlag(Flags.Flag.SEEN,true);
// to delete the message
// msg.setFlag(Flags.Flag.DELETED, true);
}
}
finally {
if (folder != null) { folder.close(true); }
if (store != null) { store.close(); }
}
}
public static void saveParts(Object content, String filename)
throws IOException, MessagingException, javax.mail.MessagingException {
OutputStream out = null;
InputStream in = null;
try {
if (content instanceof Multipart) {
Multipart multi = ((Multipart)content);
int parts = multi.getCount();
for (int j=0; j < parts; ++j) {
MimeBodyPart part = (MimeBodyPart)multi.getBodyPart(j);
if (part.getContent() instanceof Multipart) {
// part-within-a-part, do some recursion...
saveParts(part.getContent(), filename);
}
else {
String extension = "";
if (part.isMimeType("text/html")) {
extension = "html";
}
else {
if (part.isMimeType("text/plain")) {
extension = "txt";
}
else {
// Try to get the name of the attachment
extension = part.getDataHandler().getName();
}
filename = filename + "." + extension;
System.out.println("... " + filename);
out = new FileOutputStream(new File(filename));
in = part.getInputStream();
int k;
while ((k = in.read()) != -1) {
out.write(k);
}
}
}
}
}
}
finally {
if (in != null) { in.close(); }
if (out != null) { out.flush(); out.close(); }
}
}
public static void main(String args[]) throws Exception {
ReceiveMailImap.doit();
}
}
class mMailAuthenticator extends Authenticator {
String user;
String pw;
public mMailAuthenticator(String username, String password) {
super();
this.user = username;
this.pw = password;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pw);
}
}
Проблемма в том что письма приходят, но пустые.. помогите плиз..
Социальные закладки