2
votes

I am building a simple mail client for the google mail server "smtp.gmail.com" on port 25 using JAVA socket connections as a school project. The initiation and connection to the server are done well. But when it comes to authenticating after issuing a "STARTTLS" command, I am getting a weird reply and sometimes no reply from the server. The following is the portion of my code responsible for this error:

public class Client {

public static void main(String[] args) throws IOException {
    
    System.out.println("Connecting to server...");
    Socket cs = new Socket("smtp.gmail.com", 25);
    System.out.println("Connected to server! via port: " + cs.getLocalPort());
    
    OutputStreamWriter osw = new OutputStreamWriter(cs.getOutputStream());
    InputStreamReader isr = new InputStreamReader(cs.getInputStream());
    
    BufferedWriter bw = new BufferedWriter(osw);
    
    char[] inputBuffer = new char[1024];
    
    int len = isr.read(inputBuffer);
    String s = "";
    for(int i = 0; i < len; ++i){
        s += inputBuffer[i];
    }
    System.out.println("S: " + s);
    
    System.out.println("C: HELO localhost");
    bw.write("HELO localhost");
    bw.newLine();
    bw.flush();
    len = isr.read(inputBuffer);
    s = "";
    for(int i = 0; i < len; ++i){
        s += inputBuffer[i];
    }
    System.out.println("S: " + s);
    
    System.out.println("C: STARTTLS");
    bw.write("STARTTLS");
    bw.newLine();
    bw.flush();
    len = isr.read(inputBuffer);
    s = "";
    for(int i = 0; i < len; ++i){
        s += inputBuffer[i];
    }
    System.out.println("S: " + s);
    
    System.out.println("C: EHLO localhost");
    bw.write("EHLO localhost");
    bw.newLine();
    bw.flush();
    len = isr.read(inputBuffer);
    s = "";
    for(int i = 0; i < len; ++i){
        s += inputBuffer[i];
    }
    System.out.println("S: " + s);
    
    System.out.println("C: AUTH LOGIN");
    bw.write("AUTH LOGIN");
    bw.newLine();
    bw.flush();
    len = isr.read(inputBuffer);
    s = "";
    for(int i = 0; i < len; ++i){
        s += inputBuffer[i];
    }
    System.out.println("S: " + s);
    // extra code goes here, no need to show
    }
}

The following is the output:

Connecting to server...

Connected to server! via port: 39167

S: 220 smtp.gmail.com ESMTP v52sm3768873wrc.53 - gsmtp

C: HELO localhost

S: 250 smtp.gmail.com at your service

C: STARTTLS

S: 220 2.0.0 Ready to start TLS

C: EHLO localhost

S: F

C: AUTH LOGIN

S:

As you may see, the reply to "EHLO localhost" after "STARTTLS" is ambiguous, and then no reply for "AUTH LOGIN". I first thought that it should be that the server is sending Base64 text, so I tried to decode the response as follows:

    System.out.println("C: EHLO localhost");
    bw.write("EHLO localhost");
    bw.newLine();
    bw.flush();
    len = isr.read(inputBuffer);
    s = "";
    for(int i = 0; i < len; ++i){
        s += inputBuffer[i];
    }
    System.out.println("S: " + new String(Base64.getDecoder().decode(s)));
    
    System.out.println("C: AUTH LOGIN");
    bw.write("AUTH LOGIN");
    bw.newLine();
    bw.flush();
    len = isr.read(inputBuffer);
    s = "";
    for(int i = 0; i < len; ++i){
        s += inputBuffer[i];
    }
    System.out.println("S: " + new String(Base64.getDecoder().decode(s)));

But the following error is obtained:

Connecting to server...

Connected to server! via port: 39199

S: 220 smtp.gmail.com ESMTP l27sm4475707wrb.65 - gsmtp

C: HELO localhost

S: 250 smtp.gmail.com at your service

C: STARTTLS

S: 220 2.0.0 Ready to start TLS

C: EHLO localhost

Exception in thread "main" java.lang.IllegalArgumentException: Illegal base64 character 15

      at java.util.Base64$Decoder.decode0(Unknown Source)

      at java.util.Base64$Decoder.decode(Unknown Source)

      at java.util.Base64$Decoder.decode(Unknown Source)

      at khaldoun.kassam.servertest.Client.main(Client.java:64)

I thought that maybe I should use SSLSockets but from what I see is that some people were able to use the normal Sockets and it worked for them. I searched a lot for this problem without any result. I don't know if something is missing in the sent commands to the server. I really need help. Thank you in advance for your time.

1

1 Answers

1
votes

Have you checked with turning on "Allow less secure Apps" check anything missing in this link also I guess that you cant use port 25 with SMTP

Google mail setup