JavaMail的语法格式(以获取一个邮件为例)

This section defines the syntax and lists the order in which a client application calls some JavaMail methods in order to access and open a message located in a folder:


1.A JavaMail client typically begins a mail handling task by obtaining a JavaMail Session object.

Session session = Session.getInstance(props, authenticator);

2.The client uses the Session object’s getStore method to connect to the default store. The getStore method returns a Store object subclass that supports the access protocol defined in the user properties object, which will typically contain per-user preferences.

Store store = session.getStore();
store.connect();

3.If the connection is successful, the client can list available folders in the Store, and then fetch and view specific Message objects.

// get the INBOX folder
Folder inbox = store.getFolder("INBOX");
// open the INBOX folder
inbox.open(Folder.READ_WRITE);
Message m = inbox.getMessage(1); // get Message # 1
String subject = m.getSubject(); // get Subject
Object content = m.getContent(); // get content
...
...

4.Finally, the client closes all open folders, and then closes the store.

inbox.close(); // Close the INBOX
store.close(); // Close the Store

5.example:

public class JavaMailSyntax {

    @Test
    public void testStore() {

        Session session = Session.getInstance(this.getProperties(), this.getAuthenticator());
        Store store = null;
        POP3Folder inbox = null;

        try {
            store = session.getStore();
            store.connect();

            if ((inbox = (POP3Folder) store.getFolder("INBOX")).exists()) {
                inbox.open(Folder.READ_WRITE);
                Message msg = inbox.getMessage(3); // get Message #3
                String subject = msg.getSubject(); // get Subject
                Object content = ((Part) msg).getContent(); // get content
                Address[] froms = msg.getFrom();
                Address[] recipients = msg.getAllRecipients();
                Date sentDate = msg.getSentDate();

                System.out.println("getMessageNumber : " + msg.getMessageNumber());
                System.out.println("subject : " + subject);
                System.out.println("content : " + content.toString());
                System.out.println("froms : " + Arrays.toString(froms));
                System.out.println("recipients : " + Arrays.toString(recipients));
                System.out.println("sentDate : " + sentDate.toString());
            } // end if-condition
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {// Close the INBOX
            if (null != inbox) {
                inbox.close(false);
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        try {// Close the Store
            if (null != store) {
                store.close();
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }// end method-testStore

    private Properties getProperties() {
        Properties props = System.getProperties();
        props.setProperty("mail.pop3.auth", "true");
        props.setProperty("mail.store.protocol", "pop3");
        props.setProperty("mail.host", "localhost");
        return props;
    }// end method-getProperties

    private Authenticator getAuthenticator() {
        return new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("admin", "admin");
            }
        };
    }// end method-getAuthenticator

}// end class-JavaMailSyntax

/**possible output
getMessageNumber : 3
subject : subject-1146
content : javax.mail.internet.MimeMultipart@71bc1ae4
froms : [admin <admin@mike.com>]
recipients : [admin@mike.com]
sentDate : Mon Mar 13 11:47:15 CST 2017
 */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值