2013-10-23 113 views
0

我可以通過gmailgodaddy mail service發送郵件,但相同的代碼在這裏不適用於exchange server無法通過使用java郵件的交換服務器發送郵件?

其在線程 「主要」 javax.mail.MessagingException的拋出異常

異常:無法 連接到SMTP主機:,端口:**

請幫我一把。

這裏是我的代碼:

 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(); 
      } 
     } 


} 

回答

0

每一個郵件服務器有自己的一套規則和認證機制。更好地檢查你的管理員與交換服務器有關的SMTP詳細信息,如端口號,是否啓用ssl,是否需要驗證,如果是,是否與IMAP驗證細節相同。

相關問題