您好我被困在我下面提到的代码中,问题是,从缓冲区我无法获取网址。我的议程是将URL格式化为邮件(用户收到邮件进行注册,邮件中存在一个URL以激活帐户)。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.SubjectTerm;
public class javamailtest {
public static void main(String[] args) {
javamailtest gmail = new javamailtest();
gmail.read();
}
public void read() {
Properties props = new Properties();
try {
props.load(new FileInputStream(new File("D:\\smtp.properties")));
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("smtp.gmail.com", "dummy@dummy.com","dummy_password");
Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_ONLY);
int messageCount = inbox.getMessageCount();
System.out.println("Total Messages:- " + messageCount);
/*
* Message[] messages = inbox.getMessages();
* System.out.println("------------------------------"); for (int i
* = 0; i < 10; i++) { System.out.println("Mail Subject:- " +
* messages[i].getSubject()); }
*/
Message[] messages2;
messages2 = inbox.search(new SubjectTerm("hello avinash test"),inbox.getMessages());
System.out.println(messages2.length);
for (int k = 0; k < messages2.length; k++) {
System.out.println(messages2[k].getSubject());
System.out.println(messages2[k].getContent().toString());
}
for (Message mail : messages2) {
if (!mail.isSet(Flags.Flag.SEEN)) {
// it's Working here
String line;
StringBuffer buffer = new StringBuffer();
System.out.println("Buffer value:- "+buffer);
BufferedReader reader = new BufferedReader(
new InputStreamReader(mail.getInputStream()));
// this function is also working and returning 12 lines
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
System.out.println("buffer content is" +buffer + buffer.length());
//System.out.println("Message Count is: "+ mail.getDescription());
// System.out.println("Message Count is: "+
// mail.getSubject());
try {
String eTest = buffer.toString().split("http://")[0];
/*System.out.println("String values are:- "+eTest);
int len = eTest.length();
System.out.println("String length is:- "+len);*/
/*if(buffer.toString().split("&gt;http://148.251.25.56:8080/quantified/?")[0].split("href=")[1].length() <= 0){
String registrationURL = buffer.toString().split("&gt;http://148.251.25.56:8080/quantified/?")[0].split("href=")[1];
int eTest = registrationURL.length();
System.out.println("length is:- "+eTest);
Thread.sleep(50);
System.out.println("Registration url value is:- "+registrationURL);
}
else{
System.out.println("No url found");
}*/
} catch (Exception e) {
System.out.println(e);
}
}
}
inbox.close(true);
store.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
缓冲区内容
--089e01633ad4e78a0c04ff3e8e58Content-Type: text/plain; charset=UTF-8Content-Transfer-Encoding: quoted-printable=E2=80=8Bhttp://148.251.25.56:8080/quantified/rewardsUser.action--089e01633ad4e78a0c04ff3e8e58Content-Type: text/html; charset=UTF-8Content-Transfer-Encoding: quoted-printable<div dir=3D"ltr"><div class=3D"gmail_default" style=3D"font-family:arial,he=lvetica,sans-serif">=E2=80=8B<a href=3D"148.251.25.56:8080/quantifie=d/… –
答案 0 :(得分:0)
首先,您不了解邮件消息的结构。您可能希望从此JavaMail FAQ entry to find the main text content of your message开始。一旦你有了文本内容,就像其他人所说的那样,它只是一个字符串解析问题。