如何编写java程序来读取任何emailid的新邮件 [英] How to write java program to read new emails from any emailid

查看:122
本文介绍了如何编写java程序来读取任何emailid的新邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个java程序,在这里我将提供我的电子邮件ID和密码。并且我想读取到达该电子邮件ID的所有新的未读邮件。我不知道如何编写程序。



以下程序适用于gmail。但它不适用于yahoomail,因为yahoo pop3没有配置。我想要一个通用代码,将适用于所有电子邮件ID。

  import java.io. *; 
import java.util。*;
import javax.mail。*;

public class ReadMail {

public static void main(String args [])throws异常{



/ / String host =pop.gmail.com;
// String user =xyz;
// String password =12345;

//获取系统属性
属性属性= System.getProperties();

//获取默认的Session对象。
会话session = Session.getDefaultInstance(properties,null);

//获取实现指定协议的Store对象。
Store store = session.getStore(pop3s);

//使用指定的用户名和密码连接到当前主机。
store.connect(host,user,password);

//创建与给定名称相对应的文件夹对象。
文件夹文件夹= store.getFolder(inbox);

//打开文件夹。
folder.open(Folder.READ_ONLY);

消息[] message = folder.getMessages();

//显示消息。 (int i = 0; i< message.length; i ++)


System.out.println(------------ Message +(i + 1)+------------);

System.out.println(SentDate:+ message [i] .getSentDate());
System.out.println(From:+ message [i] .getFrom()[0]);
System.out.println(Subject:+ message [i] .getSubject());
System.out.print(Message:);

InputStream stream = message [i] .getInputStream();
while(stream.available()!= 0){
System.out.print((char)stream.read());
}
System.out.println();
}

folder.close(true);
store.close();
}
}


解决方案

你需要知道的不仅仅是登录通过。邮件服务器地址,邮件服务器类型,连接端口等邮件服务器地址等。
您应该可以查看 Java Mail API ,或 Commons Email



UPD:



使用 Session.getDefaultInstance()方法(连接)创建一个会话属性对象和身份验证器),使用会话获取 Store c> Session.getStore()方法,使用 Store.getFolder(FOLDER_NAME)从该商店获取文件夹使用 Folder.open(Folder.READ)方法打开文件夹并获取所有消息,使用类似消息[] messages = inboxFolder.getMessages();



这是你在找什么?



UPD2:



没有办法写通用程序,它将与任何邮件提供商一起使用,仅使用服务器路径,用户ID和密码。因为不同的邮件服务器配置不同。他们在不同的端口上讨论不同的协议(imap / pop3 / pop3 ssl)。总有一些人,谁配置了他的邮件服务器,只能在31337端口上对ssl进行通话,所有其他端口和协议都被禁止。这个家伙打破你的节目。因此,您必须在属性对象中指定所有这些属性。请查看属性的此处,您必须指定。



UPD3:



第二个想法,你实际上有一个选项。只需尝试使用不同的协议连接到服务器。如果没有帮助,开始遍历端口。适合的是您的配置。如果这真的是你想要的。


Hi I want to write a java program where I will provide my email id and password. and I want to read all new unread messages that arrived to that email id. I donot know how to write program for that.

The below program works fine for gmail. but it does not work for yahoomail because for yahoo pop3 is not configured. I want a generic code which will work for all email id.

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

public class ReadMail {

    public static void main(String args[]) throws Exception {



//        String host = "pop.gmail.com";
//        String user = "xyz";
//        String password = "12345";

        // Get system properties
       Properties properties = System.getProperties();

        // Get the default Session object.
        Session session = Session.getDefaultInstance(properties, null);

        // Get a Store object that implements the specified protocol.
        Store store = session.getStore("pop3s");

        //Connect to the current host using the specified username and password.
        store.connect(host, user, password);

        //Create a Folder object corresponding to the given name.
        Folder folder = store.getFolder("inbox");

        // Open the Folder.
        folder.open(Folder.READ_ONLY);

        Message[] message = folder.getMessages();

        // Display message.
        for (int i = 0; i < message.length; i++) {

            System.out.println("------------ Message " + (i + 1) + " ------------");

            System.out.println("SentDate : " + message[i].getSentDate());
            System.out.println("From : " + message[i].getFrom()[0]);
            System.out.println("Subject : " + message[i].getSubject());
            System.out.print("Message : ");

            InputStream stream = message[i].getInputStream();
            while (stream.available() != 0) {
                System.out.print((char) stream.read());
            }
            System.out.println();
        }

        folder.close(true);
        store.close();
    }
}

解决方案

You need to know more than just login-pass. Things like mail server address, mail server type, port for connections, etc. You should probably check out Java Mail API, or Commons Email.

UPD:

You create a Session using Session.getDefaultInstance() method (which takes connection Properties object and authenticator), get a Store from this Session using Session.getStore() method, get a Folder from that store using Store.getFolder("FOLDER_NAME") method, open that Folder, using Folder.open(Folder.READ) method, and get all messages, using something like Message[] messages = inboxFolder.getMessages();

Is that what you were looking for?

UPD2:

There is simply no way to write a generic program, which will work with any mail provider, using just server path, userID and password. Because different mail servers are configured differently. They talk differen protocols (imap/pop3/pop3 ssl) on different ports. There's always some guy, who has configured his mail server to talk imap over ssl on 31337 port only, all the other ports and protocols are banned. And this guy breaks your program. So, you'll have to specify all this properties in your properties object. Look here for properties, you'll have to specify.

UPD3:

On second thought, you actually have one option. Just try connecting to the server using different protocols. If that does not help, start iterating through ports. The one that fits is your configuration. If that's really what you want.

这篇关于如何编写java程序来读取任何emailid的新邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆