Utiliser socket pour connecter gmail et envoyer gmail, mais pas fonctionner

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(Ssortingng args[]) throws IOException, UnknownHostException { Ssortingng msgFile = "file.txt"; Ssortingng from = "[email protected]"; Ssortingng to = "[email protected]"; Ssortingng 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(Ssortingng 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, Ssortingng from, Ssortingng 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; } Ssortingng initialID = in.readLine(); System.out.println(initialID); System.out.println("HELO " + localhost.getHostName()); out.println("HELO " + localhost.getHostName()); Ssortingng welcome = in.readLine(); System.out.println(welcome); System.out.println("MAIL From:"); out.println("MAIL From:"); Ssortingng senderOK = in.readLine(); System.out.println(senderOK); System.out.println("RCPT TO:"); out.println("RCPT TO:"); Ssortingng recipientOK = in.readLine(); System.out.println(recipientOK); System.out.println("DATA"); out.println("DATA"); Ssortingng line; while ((line = msg.readLine()) != null) { out.println(line); } System.out.println("."); out.println("."); Ssortingng acceptedOK = in.readLine(); System.out.println(acceptedOK); System.out.println("QUIT"); out.println("QUIT"); return true; } } } 

Je veux apprendre à faire du serveur smtp à utiliser socket. Je trouve cet exemple de code sur ce site .

Lorsque j’écris ce code dans Eclipse et que je le comstack, socekt smtpPipe est une erreur. Message d’erreur Eclipse:

Fuite de ressources: ‘smtpPipe n’est jamais fermé’.

Je ne sais pas comment résoudre ce problème.

    Message d’erreur Eclipse: Fuite de ressources: ‘smtpPipe n’est jamais fermé’

    Il dit que vous ne fermez pas la ressource smtpPipe . La pratique recommandée consiste à fermer la ressource lorsqu’elle n’est plus nécessaire. Vous pouvez y parvenir en appelant la méthode smtpPipe.close() . Une façon de faire est d’emballer votre code dans try and finally block. En savoir plus sur finalement bloquer ici .

    Exemple:

     try { .... smtpPipe = new Socket(mailHost, SMTP_PORT); .... } finally { if (smtpPipe != null) smtpPipe.close(); } 

    Utilisez également l’approche similaire pour d’autres ressources telles que InputStream et OutputStream