unalbe连接smtp.gmail.com在465循环

时间:2014-05-21 06:18:54

标签: java

我使用端口465通过gmail coount发送邮件。 我正在循环发送邮件。(收件人出现在列表中)

我能够成功发送第一封邮件,但只要第二封邮件就会发送

我得到错误unalbe连接smtp.gmail.com在465。

enter code here props.put(“mail.smtp.host”,smtp_host);              props.put(“mail.smtp.port”,smtp_port);              props.put(“mail.smtp.user”,smtp_user);

         props.put("mail.smtp.auth", AUTH);
         props.put("mail.smtp.starttls.enable", STARTTLS);
         props.put("mail.smtp.debug", DEBUG);

         props.put("mail.smtp.socketFactory.port", smtp_port);
         props.put("mail.smtp.socketFactory.class", socket_factory));
         props.put("mail.smtp.socketFactory.fallback", "false");

尝试{

             //Obtain the default mail session
             Session session = Session.getDefaultInstance(props, null);
             session.setDebug(true);

             //Construct the mail message
             MimeMessage message = new MimeMessage(session);
             message.setFrom(new InternetAddress(from));
             message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));


             message.setRecipient(Message.RecipientType.CC, new InternetAddress(cc));
                              message.setSubject(subject);

             message.setContent(messageText, "text/html");
             message.setHeader("Content-Type" , "text/html" );
             message.saveChanges();

             //Use Transport to deliver the message
             Transport transport = session.getTransport("smtp");
             transport.connect(HOST, USER, PASSWORD);
             transport.sendMessage(message, message.getAllRecipients());
             transport.close();

         } catch (Exception e) {
             e.printStackTrace();
         }

1 个答案:

答案 0 :(得分:1)

将端口587用于TLS / STARTTLS。仅当您使用TLS / STARTTLS

时才使用以下属性
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

如果您尝试通过SSL连接。试试这个 -

props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

...

Session session = Session.getDefaultInstance(props, null);

使用 -

更改此设置
Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("username@gmail.com", "password");
        }
      });