Java : 使用 Javamail 阅读电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30319850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java : Using Javamail to read email
提问by Jesper
I've been working on a program, that's suppose to use javamail, to read through an inbox, and if an email contains a specific String, it should be opened.
我一直在开发一个程序,假设使用 javamail 来阅读收件箱,如果电子邮件包含特定的字符串,则应该打开它。
First off, this is my first time trying to use this javamail api - I'm pretty sure that my properties are all messed up - Can anyone give me at tip on correctly setting up properties for javamail?
首先,这是我第一次尝试使用这个 javamail api - 我很确定我的属性都搞砸了 - 任何人都可以给我正确设置 javamail 属性的提示吗?
Secondly, it seems like i can connect via the API, but if I try to search for subjects AND the subject is null, I get a null pointer exception - If I however don't try to match the subject with "Ordretest", I get no nullpointer - Any tips would be a great help :)
其次,我似乎可以通过 API 进行连接,但是如果我尝试搜索主题并且主题为空,则会出现空指针异常 - 但是如果我不尝试将主题与“Ordretest”匹配,我没有空指针 - 任何提示都会有很大帮助:)
package vildereMail;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.mail.search.FromTerm;
import javax.mail.search.SearchTerm;
import javax.mail.search.SubjectTerm;
public class vildereMail {
public boolean match(Message message) {
try {
if (message.getSubject().contains("Ordretest")) {
System.out.println("match found");
return true;
}
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
};
public static void main(String[] args) {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
props.put("mail.imap-mail.outlook.com.ssl.enable", "true");
props.put("mail.pop3.host", "outlook.com");
props.put("mail.pop3.port", "995");
props.put("mail.pop3.starttls.enable", "true");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap-mail.outlook.com", "[email protected]", "MyPassword");
session.setDebug(true);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
SearchTerm sender = new FromTerm(new InternetAddress("[email protected]"));
Message[] messages = inbox.search(sender);
System.out.println(messages);
for (int i = 0 ; i < messages.length ; i++) {
System.out.println(messages[i].getSubject());
if (messages[i].getSubject().equals(null)) {
System.out.println("null in subject");
break;
}
else if (messages[i].getSubject().contains("Ordretest")){
System.out.println("1 match found");
}
else {
System.out.println("no match");
}
}
System.out.println("no more messages");
store.close();
} catch (Exception mex) {
mex.printStackTrace();
}
}
}
采纳答案by User404
Without knowing on which line you get the NPE (Null Pointer Exception) I guess it occurs here:
不知道你在哪一行得到 NPE(空指针异常)我猜它发生在这里:
if (messages[i].getSubject().equals(null))
If getSubject() returns null and you try to do .equals() it will throw a NPE (because you try to invoke a method). So try to rewrite it to (assuming your message object can't be null):
如果 getSubject() 返回 null 并且您尝试执行 .equals() 它将抛出 NPE (因为您尝试调用一个方法)。所以尝试将其重写为(假设您的消息对象不能为空):
if (messages[i].getSubject() == null)
回答by Bill Shannon
Where to start...
从哪儿开始...
Have you found the JavaMail FAQ?
您找到JavaMail 常见问题解答了吗?
You can get rid of all the property settings because they're doing nothing since you're using the "imaps" protocol, not the "pop3" protocol.
您可以摆脱所有属性设置,因为它们什么都不做,因为您使用的是“imaps”协议,而不是“pop3”协议。
As others have explained, the getSubject method might return null, so you need to check for that.
正如其他人所解释的那样,getSubject 方法可能会返回 null,因此您需要对此进行检查。