使用javamail API接收邮件 [英] Receiving mail using javamail API
本文介绍了使用javamail API接收邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用javamail api配置接收器,但会引发异常.我不知道如何解决它.我只是javamail的初学者.我实际上没有得到我想要做的事情.请任何人给我适当的解决方案. 我的代码是:
i am using javamail api to configure a receiver but its throwing an exception. i don't know how to resolve it. i am just a beginner to javamail. i am actaully not getting what exactly it wants me to do. please anybody give me the proper solution. my code is:
package com.message;
import javax.mail.*;
import java.util.*;
import java.io.*;
public class Receiver
{
public static void main(String[] args)
{
Properties props = new Properties();
String host = "pop3.gmail.com";
String username = "emailid";
String password = "pasword";
String provider = "pop3";
try
{
// Connect to the POP3 server
Session session = Session.getInstance(props);
Store store = session.getStore(provider);
store.connect(host,username, password);
// Open the folder
Folder inbox = store.getFolder("INBOX");
if (inbox == null)
{
System.out.println("No INBOX");
System.exit(1);
}
inbox.open(Folder.READ_ONLY);
// Get the messages from the server
Message[] messages = inbox.getMessages();
for (int i = 0; i < messages.length; i++)
{
System.out.println("Message"+(i+1));
messages[i].writeTo(System.out);
}
// Close the connection
// but don't remove the messages from the server
inbox.close(false);
store.close();
}
catch (MessagingException ex)
{
ex.printStackTrace();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
,唯一的例外是:
javax.mail.MessagingException: Connect failed;
nested exception is:
java.net.UnknownHostException: pop3.gmail.com
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:160)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at com.message.Receiver.main(Receiver.java:20)
Caused by: java.net.UnknownHostException: pop3.gmail.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:367)
at java.net.Socket.connect(Socket.java:524)
at java.net.Socket.connect(Socket.java:474)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:267)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
at com.sun.mail.pop3.Protocol.<init>(Protocol.java:91)
at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:213)
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:156)
... 3 more
任何人都可以解决这个问题.
anybody please solve this problem.
推荐答案
尝试使用pop.gmail.com
显然是正确的地址.
Try pop.gmail.com
that is apparently the right address.
这篇关于使用javamail API接收邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文