2016-04-25 9 views
0

SSLソケットを使用してGmailに接続するためにソケットSMTPクライアントの例が少し変更されましたが、今から送信元のアカウントを承認する方法がわかりません。 (これは宿題なのでJAVAMAILは使用しません)ソケット認証を使用するjava on GmailのSMTPクライアント

public class SMTP { 

    public static void main(String args[]) throws IOException, 
     UnknownHostException { 
    String msgFile = "file.txt"; 
    String from = "[email protected]"; 
    String to = "[email protected]"; 
    String mailHost = "smtp.gmail.com"; 
    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 { 
     SSLSocket smtpPipe; 
     InputStream inn; 
     OutputStream outt; 
     BufferedReader msg; 
     msg = new BufferedReader(msgFileReader); 
     smtpPipe = (SSLSocket) ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(InetAddress.getByName("smtp.gmail.com"), 465); 
     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; 
    } 
    } 
} 
+0

作業にはスタックトレースが必要です。それを示してください。 –

答えて

1

コードを書き換えました。これは正常に動作します。

public class TotalTemp 
{ 
    private static DataOutputStream dos; 

    public static void main(String[] args) throws Exception 
    { 
      int delay = 1000; 
      String user = "[email protected]"; 
      String pass = "xxxxxxxx11"; 
      String username = Base64.encodeBase64String(user.getBytes(StandardCharsets.UTF_8)); 
      String password = Base64.encodeBase64String(pass.getBytes(StandardCharsets.UTF_8));  

      SSLSocket sock = (SSLSocket)((SSLSocketFactory)SSLSocketFactory.getDefault()).createSocket("smtp.gmail.com", 465); 
//   Socket sock = new Socket("smtp.gmail.com", 587); 
      final BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream())); 
      (new Thread(new Runnable() 
      { 
       public void run() 
       { 
        try 
        { 
         String line; 
         while((line = br.readLine()) != null) 
           System.out.println("SERVER: "+line); 
        } 
        catch (IOException e) 
        { 
         e.printStackTrace(); 
        } 
       } 
      })).start(); 
      dos = new DataOutputStream(sock.getOutputStream()); 

      send("EHLO smtp.gmail.com\r\n"); 
      Thread.sleep(delay); 
      send("AUTH LOGIN\r\n"); 
      Thread.sleep(delay); 
      send(username + "\r\n"); 
      Thread.sleep(delay); 
      send(password + "\r\n"); 
      Thread.sleep(delay); 
      send("MAIL FROM:<[email protected]>\r\n"); 
      //send("\r\n"); 
      Thread.sleep(delay); 
      send("RCPT TO:<[email protected]>\r\n"); 
      Thread.sleep(delay); 
      send("DATA\r\n"); 
      Thread.sleep(delay); 
      send("Subject: Email test\r\n"); 
      Thread.sleep(delay); 
      send("Test 1 2 3\r\n"); 
      Thread.sleep(delay); 
      send(".\r\n"); 
      Thread.sleep(delay); 
      send("QUIT\r\n"); 
    } 

    private static void send(String s) throws Exception 
    { 
      dos.writeBytes(s); 
      System.out.println("CLIENT: "+s); 
    } 
}