我无法使用javamailAPI程序查看我的电子邮件?

时间:2015-05-28 07:35:22

标签: java

我使用此代码使用javamail API连接到我在Gmail中的Gmail帐户,但是当我运行它时会出现异常。我可以通过浏览器在我的网络中访问gmail.com。:

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);

    }

  }

   } 

例外:

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

............................................... ..................................

1 个答案:

答案 0 :(得分:1)

连接到Gmail时需要使用Authneticator。

以下代码应该适合您

private void simpleButton1_Click(object sender, EventArgs e)
{

    int seatno = int.Parse(textEdit1.Text.Trim().ToString());
    foreach (Button c in gp_seatss.Controls)
    {
        foreach (string str in Convert.ToString( c.Text))
        {
            if (str == Convert.ToString(seatno))
            {
                c.Enabled = false;
            }
        }
        return;
    }
}

如果需要任何进一步的信息,请告诉我。