I used this code to connect to my Gmail account in java using javamail API but i get an exception when i run it. I am able to access gmail.com in my network through a browser though.:
import java.util.Properties;
import javax.mail.Session;
import javax.mail.Folder;
import javax.mail.Address;
import javax.mail.Store;
import javax.mail.Message;
public class Mailread {
public static void main(String args[])
{
Properties prop=new Properties();
prop.put("mail.pop3.host","pop.gmail.com" );
prop.put("mail.pop3.port","995");
prop.put("mail.pop3.starttls.enable",true);
Session session= Session.getDefaultInstance(prop);
try
{
Store store=session.getStore("pop3s");
store.connect("pop.gmail.com","myusername@gmail.com","password");
Folder folder=store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message message=folder.getMessage(3);
System.out.println(message.getMessageNumber());
System.out.println(message.getFrom());
}
catch(Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
}
Exception:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: pop.gmail.com, 995; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:211)
at javax.mail.Service.connect(Service.java:364)
at javax.mail.Service.connect(Service.java:245)
at MailPackage.Mailread.main(Mailread.java:32)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:329)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236)
at com.sun.mail.pop3.Protocol.<init>(Protocol.java:112)
at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:264)
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:207)
... 3 more
.................................................................................
You need to use Authneticator while connecting to gmail.
The below code should work for you
// Setup authentication, get session
Session emailSession = Session.getInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"manisha@gmail.com", "manisha123");
}
});
Store store = session.getStore("imaps");
store.connect("smtp.gmail.com", "*************@gmail.com","your_password");
Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_ONLY);
int messageCount = inbox.getMessageCount();
System.out.println("Total Messages:- " + messageCount);
Message[] messages = inbox.getMessages();
System.out.println("------------------------------");
for (int i = 0; i < 10; i++) {
System.out.println("Mail Subject:- " + messages[i].getSubject());
}
Please let me know if any further information is required.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.