在java imap中获取X-Mailer
如何在java imap中获取x-mail? 我已经编写了一个示例程序,但它运行不正常。 我得到空值。 我哪里出错了?
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPMessage;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import java.lang.reflect.Field;
/**
*
* @author vivek
*/
public class Main {
public static void main(String[] args) {
Properties props = System.getProperties();
props.setProperty("mail.imap.socketFactory.port", "993");
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "username", "password");
System.out.println(store);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
FetchProfile fp = new FetchProfile();
fp.add("X-Mailer"");
Message[] messages=inbox.getMessages(1, 2);
inbox.fetch(messages, fp);
for (Message message : messages) {
System.out.println(message);
System.out.println(message.getHeader("X-Mailer""));
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
imap 的调试跟踪是
`DEBUG: JavaMail version 1.4.4
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: trying to connect to host "imap.gmail.com", port 993, isSSL true
* OK Gimap ready for requests from 116.202.153.186 bs33if4292090ibb.10
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA XLIST CHILDREN XYZZY SASL-IR AUTH=XOAUTH
A0 OK Thats all she wrote! bs33if4292090ibb.10
DEBUG IMAP: AUTH: XOAUTH
DEBUG: protocolConnect login, host=imap.gmail.com, user=username, password=<non-null>
A1 LOGIN username password
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE
A1 OK [email protected] vivek goel authenticated (Success)
A2 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE
A2 OK Success
imaps://[email protected]
DEBUG: connection available -- size: 1
A3 EXAMINE Inbox
* FLAGS (\Answered \Flagged \Draft \Deleted \Seen)
* OK [PERMANENTFLAGS ()]
* OK [UIDVALIDITY 2]
* 27512 EXISTS
* 0 RECENT
* OK [UIDNEXT 58271]
A3 OK [READ-ONLY] Inbox selected. (Success)
A4 FETCH 1 (BODY.PEEK[HEADER.FIELDS (X-Mailer)])
* 1 FETCH (BODY[HEADER.FIELDS (X-Mailer)] {4}
)
A4 OK Success
com.sun.mail.imap.IMAPMessage@6ba7508a
null`
How to fetch x-mail in java imap ?
I have written a sample program but it not working fine.
I am getting null value.
Where I am going wrong ?
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPMessage;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import java.lang.reflect.Field;
/**
*
* @author vivek
*/
public class Main {
public static void main(String[] args) {
Properties props = System.getProperties();
props.setProperty("mail.imap.socketFactory.port", "993");
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "username", "password");
System.out.println(store);
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
FetchProfile fp = new FetchProfile();
fp.add("X-Mailer"");
Message[] messages=inbox.getMessages(1, 2);
inbox.fetch(messages, fp);
for (Message message : messages) {
System.out.println(message);
System.out.println(message.getHeader("X-Mailer""));
}
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
Debug trace for imap is
`DEBUG: JavaMail version 1.4.4
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: trying to connect to host "imap.gmail.com", port 993, isSSL true
* OK Gimap ready for requests from 116.202.153.186 bs33if4292090ibb.10
A0 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA XLIST CHILDREN XYZZY SASL-IR AUTH=XOAUTH
A0 OK Thats all she wrote! bs33if4292090ibb.10
DEBUG IMAP: AUTH: XOAUTH
DEBUG: protocolConnect login, host=imap.gmail.com, user=username, password=<non-null>
A1 LOGIN username password
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE
A1 OK [email protected] vivek goel authenticated (Success)
A2 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE
A2 OK Success
imaps://[email protected]
DEBUG: connection available -- size: 1
A3 EXAMINE Inbox
* FLAGS (\Answered \Flagged \Draft \Deleted \Seen)
* OK [PERMANENTFLAGS ()]
* OK [UIDVALIDITY 2]
* 27512 EXISTS
* 0 RECENT
* OK [UIDNEXT 58271]
A3 OK [READ-ONLY] Inbox selected. (Success)
A4 FETCH 1 (BODY.PEEK[HEADER.FIELDS (X-Mailer)])
* 1 FETCH (BODY[HEADER.FIELDS (X-Mailer)] {4}
)
A4 OK Success
com.sun.mail.imap.IMAPMessage@6ba7508a
null`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来不错,是的。现在,您是否使用 Thunderbird 进行 imap 并检查 X-mailer 标头是否存在?
Looks ok, ish. Now, did you imap with say Thunderbird and CHECK if the X-mailer header even exists?