我想编写一个java应用程序,将邮件发送到文件txt中的多个地址(每行等于一个电子邮件地址)。所以首先,我一行一行地阅读文件,我得到了一个电子邮件的数组列表(我单独测试了,它工作得很好)。
请帮我克服错误:这是代码的一部分:
try {
BufferedReader in = new BufferedReader(filepath ));
ArrayList<String> emails =new ArrayList<>();
String s;
while ((s = in.readLine()) != null) {
emails.add(s);
}
in.close();
int size = emails.size();
InternetAddress[] addresses = new InternetAddress[size];
for (int i =0;i < size;i++) {
try {
addresses[i] = new InternetAddress((emails.get(i).trim())); // line cause the problem
// addresses[i] = new InternetAddress(props.getProperty(emails.get(i).trim()));
} catch (AddressException e) {
e.printStackTrace();
}
}
message.setRecipients(Message.RecipientType.TO, addresses);
} catch (IOException e) {
e.printStackTrace();
}
I got this error:
javax.mail.internet.AddressException: Illegal address in string ``''
at javax.mail.internet.InternetAddress.<init>(InternetAddress.java:122)
at SendMails.main
at line ```addresses[i] = new InternetAddress((emails.get(i).trim())); ```// line cause the
problem
when I try ```addresses[i] = new
InternetAddress(props.getProperty(emails.get(i).trim()));```
I got this error:
Exception in thread "main" java.lang.NullPointerException
at javax.mail.internet.InternetAddress.parse(InternetAddress.java:793)
at javax.mail.internet.InternetAddress.parse(InternetAddress.java:752)
at javax.mail.internet.InternetAddress.<init>(InternetAddress.java:119)
at SendMails.main
try {
BufferedReader in = new BufferedReader(filepath);
ArrayList<String> emails =new ArrayList<>();
String s;
while ((s = in.readLine()) != null) {
if(!s.trim().isEmpty()) {
emails.add(s);
}
}
in.close();
InternetAddress[] addresses = new InternetAddress[emails.size()];
for (int i =0;i < emails.size();i++) {
try {
addresses[i] = new InternetAddress(emails.get(i)); // line cause the problem
// addresses[i] = new InternetAddress(props.getProperty(emails.get(i).trim()));
} catch (AddressException e) {
e.printStackTrace();
}
}
message.setRecipients(Message.RecipientType.TO, addresses);
} catch (IOException e) {
e.printStackTrace();
}
这是代码改进。