0
我試圖使用我寫的應用程序連接到我公司的Exchange服務器。我連續搜索了2天,我無法弄清楚爲什麼它不起作用。爲IMAP設置JavaMail的問題
首先,這裏是我的代碼:
String to = "[email protected]";
String from = "[email protected]";
String host = "mail.exchange.server.net";
final String username = "[email protected]";
final String password = "password";
String provider = "imap";
Properties props = new Properties();
// props.put("mail.smtp.host", host);
props.put("mail.store.protocol", "imap");
// props.put("mail.smtp.user", username);
// props.put("mail.smtp.password", password);
props.put("mail.imap.port", "143");
// props.put("mail.smtp.auth", "true");
//Removed to simplify method//
// Authenticator authenticator = new Authenticator() {
// @Override
// protected PasswordAuthentication getPasswordAuthentication() {
// return new PasswordAuthentication(username, password);
// }
// };
Session session = Session.getInstance(props);
Store store = session.getStore(provider);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Test");
msg.setSentDate(new Date());
msg.setText("This works.");
Transport transport = session.getTransport();
transport.connect(username, password);
transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
Transport.send(msg);
transport.sendMessage(msg, address);
if (transport == null) {
JOptionPane.showMessageDialog(rootPane, "Mail carrier was shot. Sorry.");
}
我想保持這一儘可能簡單。沒有SSL,沒有IMAPS,沒有驗證器,沒有什麼奇特的。它拒絕接受提供者。它會回傳一條消息(通過System.err.println
),表明「無效的提供者」或「提供者爲空」。我假設只有JavaMail API有足夠的內容才能使用IMAP協議連接到普通的vanilla Exchange服務器。
這裏是我得到:
javax.mail.NoSuchProviderException: Invalid protocol: null
at javax.mail.Session.getProvider(Session.java:449)
at javax.mail.Session.getTransport(Session.java:667)
at javax.mail.Session.getTransport(Session.java:648)
at javax.mail.Session.getTransport(Session.java:634)
對我來說,這似乎是JavaMail的不具有任何IMAP和我將要導入另一個lib放到我的項目。有人看到發生了什麼事嗎?
Ero附錄:我可以通過我的手機,iPad和筆記本電腦建立會話。這使我相信它不是端口或連接問題。我只是試圖連接到服務器,暫時。稍後我可以詳細瞭解身份驗證的細節。
謝謝!