[英]How to use the Search Method in java mail api
I am using the javamail api to make a Search option which Searches the Emails in the Gmail Folder on the Basis of a Keyword present in the Subject of the Messages Here is the Code that i am Using 我正在使用javamail api做出“搜索”选项,该选项根据邮件主题中存在的关键字搜索Gmail文件夹中的电子邮件,这是我正在使用的代码
public class EGMail7 {
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
final String m10 = "abc@gmail.com";
final String n10 = "12345";
string host = "smtp.gmail.com";
try
{
Properties pro1 = new Properties();
pro1.put("mail.smtp.host", "smtp.gmail.com");
pro1.put("mail.smtp.socketFactory.port", "465");
pro1.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
pro1.put("mail.smtp.auth", "true");
pro1.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(pro1,
new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(m10,n10);
}
});
Store store = session.getStore("imaps");
store.connect(host, m10, n10);
Folder folderbox = store.getFolder("INBOX");
folderbox.open(Folder.READ_ONLY);
SearchTerm search = new SearchTerm(){
@Override
public boolean match(Message message) {
try
{
if(message.getSubject().contains([Keyword]))
{
return true;
}
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
return false;
}
};
Message[] found = folderbox.search(search);
int length = found.length;
for(int i = 0;i<found.length;i++)
{
Message mess1 = found[i];
System.out.println("->Message Number > "+i);
System.out.println("->Message Subject >"+mess1.getSubject());
}
folderbox.close(true);
store.close();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
}
I am using the Eclipse IDE and the problem i am facing is that whatever keyword I pass in the SearchTerm Method I always end up getting null Exception and an Message[] Found Array . 我正在使用Eclipse IDE,我面临的问题是,无论我在SearchTerm方法中传递的任何关键字,我总是最终会得到null Exception和Message [] Found Array。 I used Stack Trace to find out the Problem of this NullPointer Exception and it is at the line
我使用堆栈跟踪来发现此NullPointer异常的问题,它就在代码行
Message[] found = folderbox.search(search);
I cannot Seem to understand what is the Problem Here ? 我似乎不明白这是什么问题?
Regards, Thank you 问候,谢谢
PS - and if someone could also please post the corrected code it would be great Thanks PS-如果有人也可以发布更正的代码,那将非常感谢
Also When i Am Just Adding the Keyword Directly in the SearchTerm it give an Error like 另外,当我只是直接在SearchTerm中添加关键字时,也会出现类似的错误
SearchTerm searchCon = new SearchTerm([Keyword]);
There are 2 Errors 1.Cannot Instantiate the SearchTerm 2.The Serializable class does not Declare a static final Serial Verison UID field of Type Long 有2个错误1.无法实例化SearchTerm 2.Serializable类未声明Long类型的静态最终Serial Verison UID字段
So I Cannot Understand what is the Mistake here 所以我无法理解这里的错误
You got exception because you have mail without subject. 您之所以例外,是因为您有没有主题的邮件。 Statement should be corrected with ;
声明应该用纠正;
if(message.getSubject() != null && message.getSubject().contains([Keyword]))
Your code is full of these common JavaMail mistakes , please correct them. 您的代码中充满了这些常见的JavaMail错误 ,请更正它们。
There's no need to create your own custom SearchTerm, just use SubjectTerm : 无需创建自己的自定义SearchTerm,只需使用SubjectTerm即可 :
SearchTerm search = new SubjectTerm(keyword);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.