Could not establish the connection to the Exchange Web Service - Java API

后端 未结 3 2063
暗喜
暗喜 2020-12-19 12:15

I am testing the Microsoft Exchange Web Service Java API (version 1.2) to read mails from a server. Here is my code:

String url = \"https://         


        
3条回答
  •  不知归路
    2020-12-19 12:44

    I got the above mentioned error and spend many hours trying to fix it. My ultimate solution was to give up on EWS Version 1.2 completely and use the following javax.mail code like this:

    package javaapplication4;
    import java.util.Date;
    import java.util.Properties;
    
    import javax.mail.Message;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.MimeMessage;
    
    
    public class JavaApplication4 {
        public static void main(String[] args) throws Exception {
               new JavaApplication4().send_email();
        }
        private void send_email() throws Exception
        {
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.yourserver.net");
            props.put("mail.from", "yourusername@youremailaddress.com");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.ssl.enable", "false");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "587");
    
            Authenticator authenticator = new Authenticator();
            props.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
    
            Session session = Session.getInstance(props, authenticator);
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom();
            msg.setRecipients(Message.RecipientType.TO, "yourusername@youremailaddress.com");  
                // also tried @gmail.com
            msg.setSubject("JavaMail ssl test");
            msg.setSentDate(new Date());
            msg.setText("Hello, world!\n");
    
            Transport transport;
            transport = session.getTransport("smtp");
            transport.connect();
            msg.saveChanges(); 
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
        }
        private class Authenticator extends javax.mail.Authenticator {
           private PasswordAuthentication authentication;
           public Authenticator() {
               String username = "yourusername@youremailaddress.com";
               String password = "yourpassword";
               authentication = new PasswordAuthentication(username, password);
           }
           protected PasswordAuthentication getPasswordAuthentication() {
               return authentication;
           }
       }
    }
    

    You will need to get the javamail-1.4.7 and load the mail.jar from (http://www.oracle.com/technetwork/java/index-138643.html) into the project.

    One thing we did which shed light on the situation is download Thunderbird mail client which can auto-discover information about the exchange server to make sure all of our settings were right.

提交回复
热议问题