从Outlook帐户中读取电子邮件时出错

时间:2018-10-31 06:28:40

标签: java

我正在尝试从Outlook帐户阅读电子邮件。我使用了正确的用户名和密码,但是在以下错误消息中,它显示未知的用户名或错误的密码。帮助我解决此问题。

Exception in thread "main" javax.mail.AuthenticationFailedException: Logon 
failure: unknown user name or bad password.

由于我是电子邮件API的新手,因此很难发现问题。

下面是代码:

  package mytry;

  import javax.mail.*;
  import java.io.IOException;
  import java.util.Properties;



 public class EmailService {


 public static void main(String[] s) throws MessagingException, IOException 
 {
    EmailService test = new EmailService();
    test.doTest();
    System.out.println("Ok");
 }

 private void doTest() throws MessagingException, IOException {

    final String username = "xyz@live.com";
    final String passwd = "xyz";
    Properties props = new Properties();
    props.put("mail.host", "pop-mail.outlook.com");
    props.put("mail.store.protocol", "pop3s");
    props.put("mail.pop3s.auth", "true");
    props.put("mail.pop3s.port", "995");


    Session session = Session.getInstance(props, new 
    javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, passwd);
        }
    });

    Store store = session.getStore("pop3s");
    store.connect();
    Folder emailFolder = store.getFolder("INBOX");

    emailFolder.open(Folder.READ_ONLY);

    // retrieve the messages from the folder in an array and print it
    Message[] messages = emailFolder.getMessages();
    System.out.println("messages.length---" + messages.length);

    for (int i = 0, n = messages.length; i < n; i++) {
        Message message = messages[i];
        System.out.println("---------------------------------");
        System.out.println("Email Number " + (i + 1));
        System.out.println("Subject: " + message.getSubject());
        System.out.println("From: " + message.getFrom()[0]);
    }

    //close the store and folder objects
    emailFolder.close(false);
    store.close();


   }
   }

0 个答案:

没有答案