I'm writing a program to read emails from outlook.
I wrote the below program to send email.
package com.getEmails;
import java.util.Properties;
import javax.mail.*;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailExample {
public static void main(String args[]) {
String to = "[email protected]";
String from = "[email protected]";
String host = "myhost";
try {
// Get system properties
Properties props = System.getProperties();
Authenticator authenticator = new Authenticator();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
// props.put("mail.smtp.auth.mechanisms","NTLM");
props.put("mail.smtp.submitter", authenticator
.getPasswordAuthentication().getUserName());
// Get session
Session session = Session.getInstance(props, authenticator);
session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
// Set the subject
message.setSubject("JavaMail Test");
// Set the content
message.setText("Test email through Java Mail");
// Send message
Transport.send(message);
System.out.println("OK Man");
}
catch (MessagingException e) {
e.toString();
} catch (Exception mex) {
System.out.print(mex);
}
}
static class Authenticator extends javax.mail.Authenticator {
private PasswordAuthentication authentication;
public Authenticator() {
String username = "";
String password = "";
authentication = new PasswordAuthentication(username, password);
}
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
}
Here i've given host
as myhost
, i'm sorry for that, since it is my organizations host address.
And i'm trying to read the contents of my inbox using the below program.
package com.getEmails;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
public class readEmails {
public static void main(String[] args) throws Exception {
final String userName = "[email protected]";
final String password = "password";
final String host = "myhost";
// properties
Properties props = System.getProperties();
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
session.setDebug(true);
// Store object
Store store = session.getStore("imaps");
store.connect(host, userName, password);
// Get folder
Folder folder = store.getFolder("Drafts");
folder.open(Folder.READ_ONLY);
Message messages[] = folder.getMessages();
for (Message message : messages) {
System.out.println(message.getSubject());
}
}
}
here when i run the above program i'm getting the below output.
DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: trying to connect to host "myhost", port 993, isSSL true
Exception in thread "main" javax.mail.MessagingException: Connection timed out: connect;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:670)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at com.getEmails.readEmails.main(readEmails.java:30)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:113)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:111)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:637)
... 3 more
Here even if i change imaps
to imap
, i'm getting the same error.
And also i want to tell that I used the same host
for both reading and writing emails.
please let me know how can i fix this and get my emails. I'm using JavaMail(I've to use only this).
By "outlook" I assume you're connecting to a Microsoft Exchange mail server in your organization.
Exchange normally uses a Microsoft proprietary protocol with the Outlook mail reader. Unless IMAP support has been enabled on your Exchange server, you won't be able to connect using JavaMail. Check with your server administrator to see if IMAP has been enabled. (Enabling IMAP for reading is separate from enabling SMTP for sending.)
Also, you're using a very old version of JavaMail. You should consider upgrading to the current version.