मैंने इस अंतहीन समस्या को हल किया है।
नोटिस:
- मैं IMAP प्रोटोकॉल
- मैं केवल कनेक्शन का उपयोग और इस वर्ग का उपयोग कर रहा ईमेल मैं
प्राप्त हुआ है मैं गुण इस के इन सेट के साथ आशा है कि देखने के लिए उन लोगों की मदद कर सकता है जिन्होंने इसे पढ़ने, लिखने, जो भी ईमेल बल्कि दृष्टिकोण के साथ संघर्ष किया है।
public class OutlookReader_imap {
Folder inbox;
// Constructor of the calss.
public OutlookReader_imap() {
System.out.println("Inside MailReader()...");
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
/* Set the mail properties */
/*
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props);
MimeMessage msg = new MimeMessage(session);
// set the message content here
Transport.send(msg, username, password);
*/
Properties props = System.getProperties();
// Set manual Properties
props.setProperty("mail.imaps.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.imaps.socketFactory.fallback", "false");
props.setProperty("mail.imaps.port", "993");
props.setProperty("mail.imaps.socketFactory.port", "993");
props.put("mail.imaps.host", "imap-mail.outlook.com");
try {
/* Create the session and get the store for read the mail. */
Session session = Session.getDefaultInstance(System.getProperties(), null);
Store store = session.getStore("imaps");
store.connect("imap-mail.outlook.com", 993, "<email>", "<password>");
/* Mention the folder name which you want to read. */
// inbox = store.getDefaultFolder();
// inbox = inbox.getFolder("INBOX");
inbox = store.getFolder("INBOX");
/* Open the inbox using store. */
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.search(new FlagTerm(new Flags(
Flags.Flag.SEEN), false));
System.out.println("No. of Unread Messages : " + inbox.getUnreadMessageCount());
/* Use a suitable FetchProfile */
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
inbox.fetch(messages, fp);
try {
printAllMessages(messages);
inbox.close(true);
store.close();
} catch (Exception ex) {
System.out.println("Exception arise at the time of read mail");
ex.printStackTrace();
}
} catch (MessagingException e) {
System.out.println("Exception while connecting to server: " + e.getLocalizedMessage());
e.printStackTrace();
System.exit(2);
}
}
public void printAllMessages(Message[] msgs) throws Exception {
for (int i = 0; i < msgs.length; i++) {
System.out.println("MESSAGE #" + (i + 1) + ":");
printEnvelope(msgs[i]);
}
}
public void printEnvelope(Message message) throws Exception {
Address[] a;
// FROM
if ((a = message.getFrom()) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("De : " + a[j].toString());
}
}
String subject = message.getSubject();
Date receivedDate = message.getReceivedDate();
Date sentDate = message.getSentDate(); // receivedDate is returning
// null. So used getSentDate()
//Dar Formato a la fecha
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("Asunto : " + subject);
if (receivedDate != null) {
System.out.println("Recibido: " + df.format(receivedDate));
}
System.out.println("Enviado : " + df.format(sentDate));
}
public static void main(String args[]) {
new OutlookReader_imap();
}
}
स्रोत
2016-05-03 20:57:59
यह उत्पादन लोगों के लिए वास्तविक समय की समस्या हो सकती है! – Jayy