简体   繁体   中英

unable to send mails through exchange server using java mail?

I am able to send the mails through gmail and godaddy mail service but the same code is not working for exchange server here.

Its throwing exception like

Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: . . . , port: **

Please help me out.

Here is my code:

        public void sendMailsTest(String hostName, int portNo, final String userLoginId,
        final String userPassword, String mailtoUser, String userName,
        DocumentDetails mailDetails,String fromUserName, int type,
        List<User> mailBodyDetails,ArrayList<String> ccList) {
        String line1 = "This is an auto generated e-Mail don't reply";
        String mailto=mailtoUser;
       String from =userLoginId;

       for(int i=0; i<mailBodyDetails.size();i++){

      if(mailBodyDetails.get(i).getMailTypeId() == type){

          subject = mailBodyDetails.get(i).getMailSubject();
          body = "body";



      }
      }
           boolean sessionDebug=false;
            String  host=hostName;
             int portno=portNo;

             String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
            java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

            Properties   props= new Properties(System.getProperties());
            props.put("mail.host",host);
            props.put("mail.smtp.starttls.enable","true");
            props.put("mail.smtp.auth", "true");
            props.put("mail.debug", "true");
            props.put("mail.transport.protocol","smtp");
            props.put("mail.smtp.port",""+portno);
            props.put("mail.smtp.auth","true");
            //props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.put("mail.smtp.socketFactory.fallback", "false");

             Authenticator auth = new Authenticator()
             {
                    public PasswordAuthentication getPasswordAuthentication()
        {
            String username = userLoginId;
            String password = userPassword;
            return new PasswordAuthentication(username, password);
        }
             };
           Session mailSession=Session.getInstance(props, auth);
           mailSession.setDebug(sessionDebug);
           MimeMessage msg = new MimeMessage(mailSession);


           try {
            msg.setFrom(new InternetAddress(from));



            //InternetAddress[] CCaddress = new InternetAddress[communicationList.size()];
            InternetAddress[] CCaddress = new InternetAddress[ccList.size()];
             if(ccList.size() != 0){
            for(int i =0; i< ccList.size(); i++)
              {
                if(ccList != null){
                  CCaddress[i] = new InternetAddress(ccList.get(i));
                }
              }
             }
             if(mailto != null){
                 InternetAddress[] address={new InternetAddress(mailto)};
               msg.setRecipients(Message.RecipientType.TO, address);
             }
               msg.setRecipients(Message.RecipientType.CC, CCaddress);
               msg.setSubject(subject);
               msg.setContent("text/html");
               msg.saveChanges();



               Transport.send(msg); 
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            try {
                throw new MyTechnicalException("Messaging Server Is down,try after sometime");
            } catch (MyTechnicalException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            try {
                throw new MyTechnicalException("Messaging Server Is down,try after sometime");
            } catch (MyTechnicalException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }


}

Every mail server is having their own set of rules and authentication mechanisms. Better check with your admin related to exchange server SMTP details like port number, ssl enabled or not, is it required authentication and if so, is it same as your IMAP authentication details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM