我正在编写一个程序来阅读outlook中的电子邮件。
我编写了以下程序来发送电子邮件。
package com.getEmails;
import java.util.Properties;
import javax.mail.*;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailExample {
public static void main(String args[]) {
String to = "xyz@abc.com";
String from = "ubv@abc.com";
String host = "myhost";
try {
// Get system properties
Properties props = System.getProperties();
Authenticator authenticator = new Authenticator();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
// props.put("mail.smtp.auth.mechanisms","NTLM");
props.put("mail.smtp.submitter", authenticator
.getPasswordAuthentication().getUserName());
// Get session
Session session = Session.getInstance(props, authenticator);
session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
// Set the subject
message.setSubject("JavaMail Test");
// Set the content
message.setText("Test email through Java Mail");
// Send message
Transport.send(message);
System.out.println("OK Man");
}
catch (MessagingException e) {
e.toString();
} catch (Exception mex) {
System.out.print(mex);
}
}
static class Authenticator extends javax.mail.Authenticator {
private PasswordAuthentication authentication;
public Authenticator() {
String username = "";
String password = "";
authentication = new PasswordAuthentication(username, password);
}
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
}
我已将host
作为myhost
,我很抱歉,因为这是我的组织主持人地址。
我正在尝试使用以下程序阅读收件箱中的内容。
package com.getEmails;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
public class readEmails {
public static void main(String[] args) throws Exception {
final String userName = "xyz@abc.com";
final String password = "password";
final String host = "myhost";
// properties
Properties props = System.getProperties();
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
session.setDebug(true);
// Store object
Store store = session.getStore("imaps");
store.connect(host, userName, password);
// Get folder
Folder folder = store.getFolder("Drafts");
folder.open(Folder.READ_ONLY);
Message messages[] = folder.getMessages();
for (Message message : messages) {
System.out.println(message.getSubject());
}
}
}
在这里,当我运行上述程序时,我得到以下输出。
DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: trying to connect to host "myhost", port 993, isSSL true
Exception in thread "main" javax.mail.MessagingException: Connection timed out: connect;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:670)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at com.getEmails.readEmails.main(readEmails.java:30)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:113)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:111)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:637)
... 3 more
即使我将imaps
更改为imap
,我也会遇到相同的错误。
我还想告诉我,我使用相同的host
来阅读和撰写电子邮件。
请让我知道如何解决此问题并收到我的电子邮件。我使用JavaMail(我只使用它)。
答案 0 :(得分:1)
通过“outlook”我假设您正在连接到组织中的Microsoft Exchange邮件服务器。
Exchange通常使用Microsoft专有协议和Outlook邮件阅读器。除非在Exchange服务器上启用了IMAP支持,否则您将无法使用JavaMail进行连接。请咨询您的服务器管理员,了解是否已启用IMAP。 (启用IMAP进行读取与启用SMTP以进行发送是分开的。)
此外,您使用的是非常旧版本的JavaMail。您应该考虑upgrading to the current version。