开发者

Java email parser

开发者 https://www.devze.com 2023-04-03 23:53 出处:网络
I have to parse the email message like this and figure out the body,date etc.Any help would be appreciated.

I have to parse the email message like this and figure out the body,date etc.Any help would be appreciated.

Date:        XXXXX
Reply-To:     XXXXX
Sender:       XXXXX
From:        XXXX
Subject:     Test
In-Reply-To:  XXXX
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="=__Part416ABDAE.0__="
=__Part416ABDAE.0__=
 Content-Type: text/plain; charset=US-ASCII
 Content-T开发者_如何学Pythonransfer-Encoding: quoted-printable
Test


Here's a regex solution:

import java.util.regex.*;

class Test {

    public static void main(String[] args) {

        String input = "Date:        XXXXX\n"
                     + "Reply-To:     XXXXX\n"
                     + "Sender:       XXXXX\n"
                     + "From:        XXXX\n"
                     + "Subject:     Test\n"
                     + "In-Reply-To:  XXXX\n"
                     + "Mime-Version: 1.0\n"
                     + "Content-Type: multipart/alternative; " +
                                 "boundary=\"=__Part416ABDAE.0__=\"";

        Pattern p = Pattern.compile("(.*?):\\s+(.*)");
        Matcher m = p.matcher(input);
        while (m.find()) {
            System.out.println("Key: " + m.group(1));
            System.out.println("Val: " + m.group(2));
            System.out.println();
        }
    }
}

Output:

Key: Date
Val: XXXXX

Key: Reply-To
Val: XXXXX

Key: Sender
Val: XXXXX

Key: From
Val: XXXX

Key: Subject
Val: Test

Key: In-Reply-To
Val: XXXX

Key: Mime-Version
Val: 1.0

Key: Content-Type
Val: multipart/alternative; boundary="=__Part416ABDAE.0__="


If your serious about parsing email (and this is not a home work parsing assignment) you should look at Mime4J.

0

精彩评论

暂无评论...
验证码 换一张
取 消