简体   繁体   English

Java 和 Microsoft Exchange 连接被服务器丢弃?

[英]Java and Microsoft Exchange Connection Dropped by server?

I'm using Java Springboot to read the inbox of a Microsoft Exchange account (I am already able to send emails programmatically).我正在使用 Java Springboot 读取 Microsoft Exchange 帐户的收件箱(我已经能够以编程方式发送电子邮件)。 When I try to read the inbox folder I receive a general error:当我尝试读取收件箱文件夹时,我收到一个一般错误:

javax.mail.MessagingException: Connection dropped by server?

Username and password are of course correct.用户名和密码当然是正确的。

I use the following configuration:我使用以下配置:

  Properties mailProps = new Properties();
  mailProps.setProperty("mail.transport.protocol","smtp");
  mailProps.setProperty("mail.smtp.auth","true");
  mailProps.setProperty("mail.smtp.starttls.enable","true");
  mailProps.setProperty("mail.debug","false");      
  mailProps.setProperty("mail.smtp.sasl.mechanisms.oauth2.oauthToken", password);
  Session session = Session.getDefaultInstance(mailProps);   
  Store store = session.getStore("imaps");      
  store.connect("outlook.office365.com", 143,  username, password); //username and password are omitted
  Folder emailFolder = store.getFolder("inbox");
  emailFolder.open(Folder.READ_ONLY);

UPDATE: Since I can get the inbox from the Thunderbird client, I set up the code following the configuration on Thunderbird.更新:因为我可以从 Thunderbird 客户端获取收件箱,所以我按照 Thunderbird 上的配置设置了代码。 Now I have:我现在有:

DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle]
DEBUG IMAPS: mail.imap.fetchsize: 16384
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
DEBUG IMAPS: enable STARTTLS
DEBUG IMAPS: closeFoldersOnStoreFailure
DEBUG IMAPS: trying to connect to host "outlook.office365.com", port 143, isSSL true
javax.mail.MessagingException: Unsupported or unrecognized SSL message
at it.spring.platform.services.communication.mail.MailTest.read(MailTest.java:53)

For the sake of clarity and completeness I add the full Spring code I wrote to read the inbox:为了清楚和完整起见,我添加了我为阅读收件箱而编写的完整 Spring 代码:

import javax.mail.MessagingException;
import javax.mail.Store;

public class JavaMailReader
{
 private Store store;
 private String username;
 private String password;
 private String host;
 private int port;
 private String inbox;

 public JavaMailReader(Store store, String host, int port, String   username, String password, String inbox)
 { 
   this.host=host;
   this.port=port;
   this.store=store;
   this.username=username;
   this.password=password;
   this.inbox=inbox;
   this.port = port;
  }

  public void connect() throws MessagingException
  {
    store.connect(host, port, username, password);       
  }

  public Store getStore()
  {
   return store;
  }   

  public String getInboxFolderName()
  {
    return this.inbox;
  }    
}

  @Bean
public JavaMailReader emailReader(@Value("imaps") String protocol,

                                  
@Value("${mailreceiver.mail.host}")  String host,

                                  @Value("${mailreceiver.mail.port}") Integer port,

                                  @Value("${mailreceiver.mail.password}")  String password,

                                  @Value("${mailreceiver.mail.username}") String username) throws NoSuchProviderException, MessagingException

{

  Properties mailProps = new Properties();      

  mailProps.setProperty("mail.transport.protocol","imaps");

  mailProps.setProperty("mail.imaps.auth","true");      

  mailProps.setProperty("mail.debug","true");       

  mailProps.setProperty("mail.imaps.sasl.mechanisms.oauth2.oauthToken", password);      

  Session session = Session.getDefaultInstance(mailProps);   

  Store store = session.getStore(protocol);      

  return new JavaMailReader (store, host, port, username, password, "inbox");

}

 //testing
  @Test
  public void read() throws NoSuchProviderException, MessagingException

{     

  Message[] messages = mailService.getInbox(); //getInbox("Inbox")

  Message found=null;

  for(Message m: messages)

  {

    if(m.getSubject().equalsIgnoreCase(subject))

    {

        found=m;

        break;

    }

  }

  assertNotNull(found);

  mailService.closeReader();

}

UPDATE 2: As suggested, I changed the port to 993 and removed starttls:更新 2:按照建议,我将端口更改为 993 并删除了 starttls:

  Properties mailProps = new Properties(); 
  mailProps.setProperty("mail.transport.protocol","imaps");
  mailProps.setProperty("mail.imaps.auth","true");      
  mailProps.setProperty("mail.debug","true"); 
  maililProps.
  setProperty("mail.imaps.sasl.mechanisms.oauth2.oauthToken",  password);
  Session session = Session.getDefaultInstance(mailProps);  
  Store store = session.getStore(protocol);     
  return new JavaMailReader (store, host, port, username, password, "inbox");

Now I have the error:现在我有错误:

DEBUG IMAPS: trying to connect to host "outlook.office365.com", port 993, isSSL true
* OK The Microsoft Exchange IMAP4 service is ready. [ a string here omitted for security reason==]
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: AUTH: XOAUTH2
DEBUG IMAPS: protocolConnect login, host=outlook.office365.com, 
user= the user-email-here, password=<non-null>
DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAPS: AUTHENTICATE PLAIN command result: A1 NO AUTHENTICATE failed.
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 3.159 s 
javax.mail.AuthenticationFailedException: AUTHENTICATE failed.

Based on your settings, you seem to try to connect to the O365 IMAP server on port 143 (default IMAP port) using SSL protocol:根据您的设置,您似乎尝试使用 SSL 协议连接到端口 143(默认 IMAP 端口)上的 O365 IMAP 服务器:

trying to connect to host "outlook.office365.com", port 143, isSSL true

This can't work because the server will respond with the default plaintext banner text (ie * OK The Microsoft Exchange IMAP4 service is ready ) while your client is expecting an SSL handshake, hence the error Unsupported or unrecognized SSL message .这是行不通的,因为服务器将使用默认的纯文本横幅文本(即* OK The Microsoft Exchange IMAP4 service is ready )进行响应,而您的客户端正在等待 SSL 握手,因此出现错误Unsupported or unrecognized SSL message

You should use the default IMAPS protocol port which is 993 (also documented here ).您应该使用默认的 IMAPS 协议端口993 (也记录在此处)。 Then the connection will be an IMAPS (TLS) connection and your settings should work.然后连接将是 IMAPS (TLS) 连接,您的设置应该有效。

Also, STARTTLS is irrelevant in this scenario.此外, STARTTLS在这种情况下无关紧要。

Your settings may work for SMTP, but what you want is reading email, and for that you have to use IMAP or POP3 (or Microsoft Graph, but that uses a complete different API).您的设置可能适用于 SMTP,但您想要的是读取 email,为此您必须使用 IMAP 或 POP3(或 Microsoft Graph,但使用完全不同的 API)。

So you have to provide a different configuration:所以你必须提供不同的配置:

Properties mailProps = new Properties();
mailProps.setProperty("mail.transport.protocol","imaps");
mailProps.setProperty("mail.imaps.auth","true");
mailProps.setProperty("mail.imaps.starttls.enable","true");
mailProps.setProperty("mail.debug","false");      
mailProps.setProperty("mail.imaps.sasl.mechanisms.oauth2.oauthToken", password);
Session session = Session.getDefaultInstance(mailProps);   
Store store = session.getStore("imaps");      
store.connect("outlook.office365.com", 143,  username, password); //username and password are omitted
Folder emailFolder = store.getFolder("inbox");
emailFolder.open(Folder.READ_ONLY);

I have replaced all occurences of "smtp" with "imaps", to configure the IMAPS protocol (otherwise, you would have provided settings for the SMTP protocol, and that is for sending emails …我已将所有出现的“smtp”替换为“imaps”,以配置 IMAPS 协议(否则,您将提供 SMTP 协议的设置,用于发送电子邮件……

If you want to do both in one go, you have to have both settings (one with "imaps", the other one with "smtp"), and when you want to use POP3, you use "pop3" instead (or "imap" if it is not IMAPS, but IMAP).如果你想在一个 go 中同时执行这两项操作,你必须同时设置这两种设置(一个使用“imaps”,另一个使用“smtp”),当你想使用 POP3 时,你使用“pop3”代替(或“imap " 如果它不是 IMAPS,而是 IMAP)。

I do not have an Exchange account at hand, so I cannot test the exact settings, but I can tell that the default settings that you used will not work for sure (you did not provide any particular setting for IMAP(S), so it had been the default …).我手头没有 Exchange 帐户,所以我无法测试确切的设置,但我可以看出您使用的默认设置肯定无法正常工作(您没有为 IMAP(S) 提供任何特定设置,所以它一直是默认的……)。

Next you should keep in mind that Office365 is a beast, You can have a lot of fun with it.接下来你应该记住 Office365 是一个野兽,你可以从中获得很多乐趣。 in particular when accessing it to get your mail out of it, As long as you move one or the other mail once in a blue moon.特别是在访问它以从中取出邮件时,只要您千载难逢地移动一封或另一封邮件。 everything is fine.一切都好。 But when you start with bulk volumes it will get nasty if you do not have especially paid for it, Although this affects mainly sending.但是,当您从批量开始时,如果您没有为此特别付费,它会变得令人讨厌,尽管这主要影响发送。 you can get also funny error messages when reading emails.阅读电子邮件时,您还会收到有趣的错误消息。

Again, the settings I provided will show only how the configuration property names should look like, I do not guarantee that they work with the given values.同样,我提供的设置将仅显示配置属性名称的外观,我不保证它们适用于给定的值。 You even may have to provide additional configuration settings.您甚至可能必须提供额外的配置设置。

And "STARTTLS" does not make sense for the *S protocols, as they are running on TLS right from the beginning.并且“STARTTLS”对于 *S 协议没有意义,因为它们从一开始就在 TLS 上运行。 Nevertheless, you can set it;不过,您可以设置它; you can also set "mail.imaps.clothilde" as "She is nice" … it will just be ignored.您还可以将“mail.imaps.clothilde”设置为“她很好”……它会被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Microsoft Exchange Server与Java连接 - Connecting microsoft Exchange server with java Exchange Server 2013与Java的连接 - Exchange server 2013 connection with java 通过Microsoft Exchange Server API发送电子邮件(Java) - To send email via microsoft exchange Server api (java) 使用Java从Microsoft Mail Exchange服务器读取带有附件的邮件 - Read mails with attachments from Microsoft mail Exchange server using Java 通过Java中的IMAPS从Microsoft Exchange Server 2007中提取邮件 - Extracting Mail from Microsoft Exchange server 2007 through IMAPS in java 是否有任何API将Microsoft Exchange服务器与Java应用程序集成以进行任务同步? - Are there any API to integrate Microsoft exchange server with Java application for Task synchronization? ServiceRequestException:使用Java的Microsoft Exchange Server请求失败 - ServiceRequestException: The request failed with Microsoft Exchange Server using Java 与Microsoft SQL Server的连接内出现Java问题 - Java issue within connection to Microsoft SQL Server Java 邮件 Microsoft Exchange NTLM - Java Mail Microsoft Exchange NTLM 使用MS Exchange Server将邮件发送到Microsoft Outlook中的帐户的连接属性是什么? - What are the connection properties to send mail to an account in Microsoft Outlook using MS Exchange Server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM