我正在尝试编写一个程序,它将通过java自动从outlook下载附件。
我正在使用javamail api。我能够使用api发送邮件,但是在下载附件,甚至阅读邮件时,我遇到了一些问题。我只是对我为此目的而阅读的邮件协议有了基本的了解。除此之外,我对这个领域并不熟悉。
public class MailReceiver {
public static void main(String[] args) {
getMail();
sendMail();
}
private static void sendMail() {
// TODO Auto-generated method stub
// Sender's email ID needs to be mentioned
String from = "my.email@organization.com";
String host = "something.organization.com";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipients(Message.RecipientType.TO,
InternetAddress.parse("some.email@organization.com"));
// Set Subject: header field
message.setSubject("Test Mail");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
StringBuilder email = new StringBuilder();
email.append("Hello World!");
// Fill message
messageBodyPart.setContent(email.toString(), "text/html");
// Create a multipar message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
// Send message
Transport.send(message);
System.out.println("Mail sent successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
private static void getMail() {
// Sender's email ID needs to be mentioned
String user = "my.email@organization.com";
String password = "my.password";
String host = "something.organization.com";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host",host );
properties.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});
try {
Store store = session.getStore("pop3");
store.connect(host,user,password);
Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_WRITE);
Message[] message = folder.getMessages();
for (int a = 0; a < message.length; a++) {
System.out.println("-------------" + (a + 1) + "-----------");
System.out.println(message[a].getSentDate());
Multipart multipart = (Multipart) message[a].getContent();
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
InputStream stream = bodyPart.getInputStream();
BufferedReader br = new BufferedReader(
new InputStreamReader(stream));
while (br.ready()) {
System.out.println(br.readLine());
}
System.out.println();
}
System.out.println();
}
folder.close(true);
store.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
由于我不确定我应该在session.getStore()
(getMail()
)中设置什么,我尝试了所有可以做的变化。
对于pop3
,我收到以下错误:
DEBUG POP3: server doesn't support TOP, disabling it
javax.mail.AuthenticationFailedException: Logon failure: unknown user name or bad password.
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:174)
at javax.mail.Service.connect(Service.java:313)
at javax.mail.Service.connect(Service.java:172)
at xxx.yyy.zzz.data.MailReceiver.getMail(MailReceiver.java:109)
at xxx.yyy.zzz.data.MailReceiver.main(MailReceiver.java:28)
当我有smtp
时:
javax.mail.NoSuchProviderException: invalid provider
at javax.mail.Session.getStore(Session.java:570)
at javax.mail.Session.getStore(Session.java:536)
at javax.mail.Session.getStore(Session.java:515)
at xxx.yyy.zzz.data.MailReceiver.getMail(MailReceiver.java:108)
at xxx.yyy.zzz.data.MailReceiver.main(MailReceiver.java:28)
和imap
,以下内容:
javax.mail.AuthenticationFailedException: AUTHENTICATE failed.
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:613)
at javax.mail.Service.connect(Service.java:313)
at javax.mail.Service.connect(Service.java:172)
at xxx.yyy.zzz.data.MailReceiver.getMail(MailReceiver.java:109)
at xxx.yyy.zzz.data.MailReceiver.main(MailReceiver.java:28)
当我提供imaps
或pop3s
时,我收到unable to find valid certification path to requested target
错误。
我无法弄清楚错误是什么,因为我能够毫无问题地发送邮件。我也确定我应该在session.getStore()
中使用什么。我关注的教程有pop3
。
答案 0 :(得分:0)
首先,您需要修复证书路径问题。 This JavaMail FAQ entry会有所帮助。然后,使用“imaps”或将“mail.imap.ssl.enable”属性设置为“true”。您可能还想修复这些common mistakes。看看这个JavaMail FAQ entry about debugging,一旦遇到这些问题,这可能会有所帮助。