java - 使用socket连接gmail并发送gmail,但不起作用

标签 java sockets server smtp gmail

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;    
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class SMTPDemo {

  public static void main(String args[]) throws IOException,
      UnknownHostException {
    String msgFile = "file.txt";
    String from = "java2s@java2s.com";
    String to = "yourEmail@yourServer.com";
    String mailHost = "yourHost";
    SMTP mail = new SMTP(mailHost);
    if (mail != null) {
      if (mail.send(new FileReader(msgFile), from, to)) {
        System.out.println("Mail sent.");
          } else {
        System.out.println("Connect to SMTP server failed!");
      }
    }
    System.out.println("Done.");
  }

  static class SMTP {
    private final static int SMTP_PORT = 25;

    InetAddress mailHost;

    InetAddress localhost;

    BufferedReader in;

    PrintWriter out;

    public SMTP(String host) throws UnknownHostException {
      mailHost = InetAddress.getByName(host);
      localhost = InetAddress.getLocalHost();
      System.out.println("mailhost = " + mailHost);
      System.out.println("localhost= " + localhost);
      System.out.println("SMTP constructor done\n");
    }

    public boolean send(FileReader msgFileReader, String from, String to)
        throws IOException {
      Socket smtpPipe;
      InputStream inn;
      OutputStream outt;
      BufferedReader msg;
      msg = new BufferedReader(msgFileReader);
      smtpPipe = new Socket(mailHost, SMTP_PORT);
      if (smtpPipe == null) {
        return false;
      }
      inn = smtpPipe.getInputStream();
      outt = smtpPipe.getOutputStream();
      in = new BufferedReader(new InputStreamReader(inn));
      out = new PrintWriter(new OutputStreamWriter(outt), true);
      if (inn == null || outt == null) {
        System.out.println("Failed to open streams to socket.");
        return false;
      }
      String initialID = in.readLine();
      System.out.println(initialID);
      System.out.println("HELO " + localhost.getHostName());
      out.println("HELO " + localhost.getHostName());
      String welcome = in.readLine();
      System.out.println(welcome);
      System.out.println("MAIL From:<" + from + ">");
      out.println("MAIL From:<" + from + ">");
      String senderOK = in.readLine();
      System.out.println(senderOK);
      System.out.println("RCPT TO:<" + to + ">");
      out.println("RCPT TO:<" + to + ">");
      String recipientOK = in.readLine();
      System.out.println(recipientOK);
      System.out.println("DATA");
      out.println("DATA");
      String line;
      while ((line = msg.readLine()) != null) {
        out.println(line);
      }
      System.out.println(".");
      out.println(".");
      String acceptedOK = in.readLine();
      System.out.println(acceptedOK);
      System.out.println("QUIT");
      out.println("QUIT");
      return true;
    }
  }
}    

我想了解如何使 smtp 服务器使用套接字。 我在 this site 上找到了这个示例代码。

当我在 Eclipse 中编写此代码并编译时,socekt smtpPipe 出现错误。 Eclipse 错误消息:

Resource leak : 'smtpPipe is never closed'.

我不知道如何解决这个问题。

最佳答案

Eclipse error message : Resource leak : 'smtpPipe is never closed'

它表示您没有关闭资源smtpPipe。建议的做法是在不再需要资源时关闭该资源。您可以通过调用 smtpPipe.close() 方法来实现此目的。 一种方法是将代码包装在 tryfinally block 周围。阅读有关finally block 的更多信息 here .

示例:

try {
    ....
    smtpPipe = new Socket(mailHost, SMTP_PORT);
    ....

} finally {
   if (smtpPipe != null)
      smtpPipe.close();
}

此外,对其他资源(例如 InputStreamOutputStream)使用类似的方法

关于java - 使用socket连接gmail并发送gmail,但不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40255179/

相关文章:

java - 从较大目录(工作空间)中的文件获取特定单词

c# - 如何将端口 IAX2 的 UDP header 转换为可读字符串

Node.js 同时运行 http 服务器和 web 套接字服务器

c - 我的 c 套接字程序在成功执行命令后在命令上吐出垃圾,不知道为什么

mysql - 当 mysql(innoDB) 表被更改(锁定)时,插入会发生什么?

java - 使用 PaintComponent Java 绘制图像

java - 如何从 Java 9 模块导出所有包?

java - java回文客户端程序错误"Cannot find symbol"

c# - UDP 多播 : Socket. SendTo 被阻止

php - mysqli_connect() : (HY000/2002): Can't connect to local MySQL server through socket