0

我可以发送邮件gmailgodaddy mail service但相同的代码在这里不起作用exchange server

它的抛出异常

线程“主”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();
            }
        }


}
4

1 回答 1

0

每个邮件服务器都有自己的一套规则和身份验证机制。最好与您的管理员核实与交换服务器 SMTP 详细信息(例如端口号、是否启用 ssl)相关的信息,是否需要身份验证,如果需要,是否与您的 IMAP 身份验证详细信息相同。

于 2013-10-23T05:15:03.337 回答