通过java使用gmail smtp服务器发送邮件时出现java.net.SocketException异常

3

我正在尝试通过Java代码通过Gmail smtp服务器发送邮件,但遇到了java.net.SocketException: Connection Reset问题。

以下是发送邮件的代码:

  Properties props = new Properties();
  props.put("mail.smtp.host",host);  //host is smtp.gmail.com
  if(host.equalsIgnoreCase("smtp.gmail.com"))
  {
      props.put("mail.smtp.socketFactory.port",port);
      props.put("mail.smtp.socketFactory.class",
              "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.port",port);//port is 465

  }
  else
  {
      props.put("mail.smtp.auth", "true");
  }
  Session session = Session.getDefaultInstance(props,
          new javax.mail.Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(uname,pwd);
      }
  });

      try {

          session.setDebug(true);
          MimeMessage message = new MimeMessage(session);

          message.setFrom(new InternetAddress(uname));

          message.addRecipient(Message.RecipientType.TO,new InternetAddress(toMail));

          message.setSubject(NewFolderName);
          System.out.println("Composing message body.");

          StringBuilder htmlStreamBuilder = new StringBuilder();
          // we create some html string here.....
          Multipart multipart = new MimeMultipart();

          MimeBodyPart htmlPart = new MimeBodyPart();
          MimeBodyPart attachmentPart= new MimeBodyPart();              

          DataSource source = new FileDataSource(filename);
          attachmentPart.setDataHandler(new DataHandler(source));
          attachmentPart.setFileName(KMAConstants.TESTNG_ATTACHMENT_FILE);

          htmlPart.setContent(htmlStream, "text/html; charset=utf-8");//we give some html stream here ....


          multipart.addBodyPart(htmlPart);
          multipart.addBodyPart(attachmentPart);


          message.setContent(multipart);

          Transport.send(message);



      } catch (MessagingException e)
      {

          System.out.println("Issue in message sending, MessagingExceptionn raised.");
          e.printStackTrace();

          throw e;
      }
  }

  `

它抛出以下错误:-
DEBUG: getProvider() 返回 javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: 正在尝试连接到主机“smtp.gmail.com”,端口465,isSSL false DEBUG SMTP: 读取响应时发生异常,THROW: java.net.SocketException: 连接重置 at java.net.SocketInputStream.read(SocketInputStream.java:209) at java.net.SocketInputStream.read(SocketInputStream.java:141) at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:124) at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) at java.io.BufferedInputStream.read(BufferedInputStream.java:265) at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89) at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2184) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1939) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654) at javax.mail.Service.connect(Service.java:367) at javax.mail.Service.connect(Service.java:226) at javax.mail.Service.connect(Service.java:175) at javax.mail.Transport.send0(Transport.java:253) at javax.mail.Transport.send(Transport.java:124) at com.rsi.kma.common.utils.KmaUtil.sendMailNow(KmaUtil.java:1907) at com.rsi.kma.ui.testconfigureui.FileProcessingTask.call(FileProcessingTask.java:401) at com.rsi.kma.ui.testconfigureui.FileProcessingTask.call(FileProcessingTask.java:1) at javafx.concurrent.Task$TaskCallable.call(Task.java:1423) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.lang.Thread.run(Thread.java:745) 发送消息时出现问题,引发MessagingException。
这里需要注意的一个重要点是,这段代码在某些计算机上可以正常工作。
请帮帮我,提前致谢。

在那些电脑上,这段代码总是能够正常工作吗? - aldebaran-ms
你是否在使用代理? - Sanjeev
@mauros直到几天前,这段代码在所有计算机上都能运行,但现在只有一些计算机能够正常工作...就你的问题而言,是的,在某些计算机上它能正常工作...我已经检查了五六次。 - humble_wolf
很难猜测发生了什么。也许是防病毒软件或防火墙阻止了您?尝试禁用它们。如果这不起作用,可能是其他程序正在使用端口465。 - aldebaran-ms
1
@mauros,你说得对,是一种防火墙阻止了我的SMTP服务器请求。感谢Mauros、Sanjeev和Bill的帮助。 - humble_wolf
显示剩余4条评论
1个回答

3

网络策略阻止了对SMTP服务器的请求。我联系了我的网络管理员,他给了我所需的权限。现在一切都正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接