使用javamail对mail office 365的IMAP访问
我正在尝试使用JavaMail 1.4 api读取Office365电子邮件帐户文件夹中的电子邮件 我的代码是:使用javamail对mail office 365的IMAP访问,java,jakarta-mail,office365,java-5,Java,Jakarta Mail,Office365,Java 5,我正在尝试使用JavaMail 1.4 api读取Office365电子邮件帐户文件夹中的电子邮件 我的代码是: public static void main(String argv[]) throws Exception { // Get a Properties object Properties props = System.getProperties(); // Get a Session object Session session = Se
public static void main(String argv[]) throws Exception {
// Get a Properties object
Properties props = System.getProperties();
// Get a Session object
Session session = Session.getInstance(props, null);
// Get a Store object
Store store = null;
Folder rf = null;
if (CUR_MAIL_PROTOCOL != null)
store = session.getStore("imap");
// Connect
if (CUR_MAIL_HOST != null || CUR_MAIL_LOGIN != null || CUR_MAIL_PWD != null)
store.connect("outlook.office365.com", 993, "**MYEMAIL**", "**MYPASS**");
else
store.connect();
// List namespace
rf = store.getDefaultFolder();
getFolderDetails(rf, true, "");
if ((rf.getType() & Folder.HOLDS_FOLDERS) != 0) {
Folder[] f = rf.list("%");
for (int i = 0; i < f.length; i++)
getFolderDetails(f[i], recursive, " ");
}
store.close();
}
谢谢
PS:AccountOffice365使用JavaMail 1.4API解决此问题
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.FetchProfile;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;
public class OutLookReader_imaps {
Folder inbox;
// Constructor of the calss.
public OutLookReader_imaps() {
System.out.println("Inside MailReader()...");
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = System.getProperties();
// Set manual Properties
props.setProperty("mail.imaps.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.imaps.socketFactory.fallback", "false");
props.setProperty("mail.imaps.port", "993");
props.setProperty("mail.imaps.socketFactory.port", "993");
props.put("mail.imaps.host", "outlook.office365.com");
try {
/* Create the session and get the store for read the mail. */
Session session = Session.getDefaultInstance(System.getProperties(), null);
Store store = session.getStore("imaps");
store.connect("outlook.office365.com", 993, "My email ID", "my password");
/* Mention the folder name which you want to read. */
inbox = store.getFolder("INBOX");
/* Open the inbox using store. */
//inbox.open(Folder.READ_ONLY);
inbox.open(Folder.READ_WRITE);
Message messages[] = inbox.search(new FlagTerm(new Flags(
Flags.Flag.ANSWERED), false));
//Message[] msgs = inbox.getMessages();
System.out.println("No. of Unread Messages : " + inbox.getUnreadMessageCount());
System.out.println("No. of Messages : " + inbox.getMessageCount());
System.out.println("No. of Deleted Messages : " + inbox.getMode());
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
inbox.fetch(messages, fp);
try {
printAllMessages(messages);
inbox.close(true);
store.close();
} catch (Exception ex) {
System.out.println("Exception arise at the time of read mail");
ex.printStackTrace();
}
} catch (MessagingException e) {
System.out.println("Exception while connecting to server: " + e.getLocalizedMessage());
e.printStackTrace();
System.exit(2);
}
}
public void printAllMessages(Message[] msgs) throws Exception {
for (int i = 0; i < msgs.length; i++) {
System.out.println("MESSAGE #" + (i + 1) + ":");
printEnvelope(msgs[i]);
}
}
public void printEnvelope(Message message) throws Exception {
Address[] a;
if ((a = message.getFrom()) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("Email From : " + a[j].toString());
}
}
String subject = message.getSubject();
Date receivedDate = message.getReceivedDate();
Date sentDate = message.getSentDate();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("Email Subject : " + subject);
if (receivedDate != null) {
System.out.println("Received Date: " + df.format(receivedDate));
}
System.out.println("Sent Date : " + df.format(sentDate));
}
public static void main(String args[]) {
new OutLookReader_imaps();
}
}
import java.text.simpleDataFormat;
导入java.util.Date;
导入java.util.Properties;
导入javax.mail.Address;
导入javax.mail.FetchProfile;
导入javax.mail.Flags;
导入javax.mail.Folder;
导入javax.mail.Message;
导入javax.mail.MessaginException;
导入javax.mail.Session;
导入javax.mail.Store;
导入javax.mail.search.FlagTerm;
公共类了望阅读器\u imaps{
文件夹收件箱;
//calss的构造函数。
公众了望读者_imaps(){
System.out.println(“内部邮件阅读器()…”;
最终字符串SSL_FACTORY=“javax.net.SSL.SSLSocketFactory”;
Properties props=System.getProperties();
//设置手动属性
setProperty(“mail.imaps.socketFactory.class”,SSL_工厂);
props.setProperty(“mail.imaps.socketFactory.fallback”、“false”);
props.setProperty(“mail.imaps.port”、“993”);
props.setProperty(“mail.imaps.socketFactory.port”、“993”);
props.put(“mail.imaps.host”、“outlook.office365.com”);
试一试{
/*创建会话并获取用于读取邮件的存储*/
Session Session=Session.getDefaultInstance(System.getProperties(),null);
Store Store=session.getStore(“imaps”);
store.connect(“outlook.office365.com”,993,“我的电子邮件ID”,“我的密码”);
/*提及要读取的文件夹名称*/
收件箱=store.getFolder(“收件箱”);
/*使用应用商店打开收件箱*/
//收件箱。打开(文件夹。只读);
收件箱。打开(文件夹。读写);
消息消息[]=收件箱。搜索(新标志词(新标志(
Flags.Flag.responsed),false);
//Message[]msgs=inbox.getMessages();
System.out.println(“未读邮件数:+inbox.getUnderMessageCount());
System.out.println(“邮件数量:+inbox.getMessageCount());
System.out.println(“已删除邮件的数量:+inbox.getMode());
FetchProfile fp=新的FetchProfile();
fp.add(FetchProfile.Item.信封);
inbox.fetch(消息,fp);
试一试{
打印所有消息(消息);
收件箱。关闭(true);
store.close();
}捕获(例外情况除外){
System.out.println(“读取邮件时出现异常”);
例如printStackTrace();
}
}捕获(消息异常e){
System.out.println(“连接到服务器时出现异常:+e.getLocalizedMessage());
e、 printStackTrace();
系统出口(2);
}
}
public void printalmessages(Message[]msgs)引发异常{
对于(int i=0;i
就我而言,缺少的是:
props.setProperty("mail.imaps.sasl.mechanisms","XOAUTH2")
所有适合我的道具(gmail和o365):
JavaMail常见问题解答有.ok:D现在我有一个错误:DEBUG:getProvider()返回javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,sun Microsystems,Inc]*ok Microsoft Exchange IMAP4服务准备就绪。[QQBNADMAUDABASADANQBDAEEAMAYADEALGBLAHUACGBWAHIABWAHAZAWAWADUALGBWAHIABWBKAC4ABWB1AHQABABABVAG8AAWAUAGMABWBTAA=]A0能力*能力IMAP4 IMAP4rev1登录已禁用的STARTTLS SASL-IR UIDPLUS移动ID取消选择子代空闲命名空间文字+A0 OK能力已完成。A1登录XXX@xxx.com******A1错误命令在无效状态下接收。基本错误是:登录A1错误命令在无效状态下接收。请尝试使用
imaps
而不是imap
。使用imap
,服务器将返回LOGINDISABLED
功能,这意味着您无法发出简单的用户名/密码LOGIN
命令。imaps不应该是这种情况。您是否找到了通过imaps访问office365的方法?你能发布解决方案吗?我仍然收到错误:-javax.mail.AuthenticationFailedException:AuthenticationFailed。事实上,我正在尝试从我的office电子邮件“office 365”中读取邮件。您能告诉我,我缺少什么配置或属性值吗?
props.setProperty("mail.imaps.sasl.mechanisms","XOAUTH2")
"mail.imaps.ssl.enable" true
"mail.imaps.sasl.enable" true
"mail.imaps.auth.login.disable" true
"mail.imaps.auth.plain.disable" true
"mail.imaps.auth.mechanisms" "XOAUTH2"
"mail.imaps.usesocketchannels" true
"mail.event.scope" "session"
"mail.imaps.starttls.enable" true
"mail.imaps.sasl.mechanisms" "XOAUTH2"