简体   繁体   中英

JavaMail API - error getting messages

I am working through oracle's tutorial on using the javamail api to access my email. Here is my code:

 import javax.mail.*;
 import javax.mail.internet.*;
 import java.util.Scanner;
 import java.util.Properties;

 public class MailClient {
    public static void main(String[] args) {
    Properties props = new Properties();
    Session session = Session.getDefaultInstance(props, null);


    Store store = session.getStore("pop3");
    store.connect("pop.gmail.com","email@gmail.com","password");

    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_ONLY);
    Message message[] = folder.getMessages();
    int i = message.length;
    for (int a=0;a<i;a++) {
        System.out.println(message[i].writeTo());
    }
    Scanner pause = new Scanner(System.in);
    folder.close(false);
    store.close();
}
}

And here is the error I am receiving:

MailClient.java:20 error: method writeTo in interface Part cannot be applied to given types;
System.out.println(message[i].writeTo());
required: OutputStream
found: no arguments
reason: actual and formal argument lists differ in length
1 error

Any ideas what am I doing wrong?

Also, on Google's page, they stated that users would need to use SSL to connect via POP3. How will I implement that in the JavaMail API? Thanks!

As indicated, your error is in

System.out.println(message[i].writeTo());

There are two problems:

  1. Message.writeTo() takes an OutputStream as an argument, and you havent supplied one.

  2. writeTo() returns void, so is not a valid argument to println()

Here is some sample code that connects to GMail via the java mail api, using POP3 and SSL

消息[I] .writeTo(System.out的);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM