Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
hengike
Posts: 47
Registered: ‎08-26-2011
My Device: dsa

Smtp gmail problem

[ Edited ]

Hey,
I write an app what use gmail to send mails 
The code is here


 

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;


public class SSlEmail {
     static private int count;
     static private byte[] buffer = new byte[1024];
    private String host;
    private int port;
    private String un;
    private String pw;
    private String from;
    private String to;
    private String subject;
    private String text;
    static StreamConnection socket;
    String[] recipients;
    static InputStream in;
    OutputStream out;
    private boolean debug = false;
    BufferedReader bfr;
    
    //
    

    public boolean isDebug() {
        return debug;
    }

    public void setDebug(boolean debug) {
        this.debug = debug;
    }

    public SSlEmail(String host, int port, String un, String pw, String from, String to, String subject, String text) {
        this.host = host;
        this.port = port;
        this.un = un;
        this.pw = pw;
        this.from = from;
        this.to = to;
        this.subject = subject;
        this.text = text;
    }

    public void open() throws IOException {
        String protocol =  "ssl";
        String url2 = null;
        if (protocol.equals("ssl")) {
            try {
                url2 = host + ":" + port + ";interface=wifi";
                socket = (StreamConnection) Connector.open(protocol + "://" + url2);
            } catch (IOException ex) {
                try {
                    url2 = host + ":" + port + ";deviceside=false";
                    socket = (StreamConnection) Connector.open(protocol + "://" + url2);
                } catch (IOException ex1) {
                    url2 = host + ":" + port + ";deviceside=true";
                    try {
                        socket = (StreamConnection) Connector.open(protocol + "://" + url2);
                    } catch (IOException ex2) {
                        System.out.println(No avalaible Connection);
                    }
                }
            }
        }
        //socket = (StreamConnection)Connector.open(protocol + "://" + host + ":" + port, Connector.READ_WRITE, true);
        in = socket.openInputStream();
        out = socket.openOutputStream();
        
    }

    public void sendMessage() throws IOException, MailException {
        // javase ben kiprobáltam sorrend mukodik
        // valami akapcsolattal lessz
        receive();
            out.write("EHLO localhost\r\n".getBytes(), 0, "EHLO localhost\r\n".getBytes().length);
            System.out.println("[SEND] "+"EHLO localhost\r\n");
            receive();
            receive();
            receive();
            receive();
            receive();
            out.write("AUTH LOGIN\r\n".getBytes(),0,"AUTH LOGIN\r\n".getBytes().length);
            System.out.println("[SEND] "+"AUTH LOGIN\r\n");
            receive();
            String username = Base64.encode(un);
            String password = Base64.encode(pw);
            out.write(username.getBytes(),0,username.getBytes().length);
            receive();
            out.write(password.getBytes(),0,password.getBytes().length);
            receive();
            out.write(("MAIL FROM:<"+un+"@gmail.com>\r\n").getBytes(),0,("MAIL FROM:<"+un+"@gmail.com>\r\n").getBytes().length);
            receive();
            out.write(("RCPT TO:<"+to+"@infomatix.hu>\r\n").getBytes(),0,("RCPT TO:<"+to+"@infomatix.hu>\r\n").getBytes().length);
            receive();
            out.write("DATA\r\n".getBytes(),0,"DATA\r\n".getBytes().length);
            receive();
            
            out.write(("Subject:"+subject+" \r\n").getBytes(),0,("Subject:"+subject+" \r\n").getBytes().length);
            out.write(("To: "+to+"\r\n").getBytes(),0,("To: "+to+"\r\n").getBytes().length);
            out.write(("From: "+from+"\r\n").getBytes(),0,("From: "+from+"\r\n").getBytes().length);
            out.write("Cc: dszekely@infomatix.hu\r\n".getBytes(),0,"Cc: dszekely@infomatix.hu\r\n".getBytes().length);
            out.write("Date: dsadasdsad\r\n".getBytes(),0,"Date: dsadasdsad\r\n".getBytes().length);    
            out.write("Bcc: asd@gmail.com\r\n\r\n".getBytes(),0,"Bcc: asd@gmail.com\r\n\r\n".getBytes().length);
            out.write((""+text+"\r\n").getBytes(),0,(""+text+"\r\n").getBytes().length);

            out.write(".\r\n".getBytes(),0,".\r\n".getBytes().length);
            receive();
            out.write("QUIT\r\n".getBytes(),0,"QUIT\r\n".getBytes().length);
            receive();
      
    }
    private static String getAuthString(String user) {

        char[] charTab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();

        byte[] data = ("\000" + user).getBytes();

        StringBuffer buf = new StringBuffer();

        int start = 0;
        int len = data.length;

        int end = len - 3;
        int i = start;

        while (i <= end) {
            int d = ((((int) data [i]) & 0x0ff) << 16)
                | ((((int) data [i+1]) & 0x0ff) << 8)
                | (((int) data [i+2]) & 0x0ff);

            buf.append(charTab [(d >> 18) & 63]);
            buf.append(charTab [(d >> 12) & 63]);
            buf.append(charTab [(d >> 6) & 63]);
            buf.append(charTab [d & 63]);

            i += 3;
        }

        if (i == start + len - 2) {
            int d = ((((int) data [i]) & 0x0ff) << 16)
                | ((((int) data [i+1]) & 255) << 8);

            buf.append(charTab [(d >> 18) & 63]);
            buf.append(charTab [(d >> 12) & 63]);
            buf.append(charTab [(d >> 6) & 63]);
            buf.append("=");
        }
        else if (i == start + len - 1) {
            int d = (((int) data [i]) & 0x0ff) << 16;

            buf.append(charTab [(d >> 18) & 63]);
            buf.append(charTab [(d >> 12) & 63]);
            buf.append("==");
        }

        return buf.toString();
    }
    public static String receive() throws IOException {

        String result = "";
        boolean stop = false;

        while (!stop) {
            count = 0;

    
	        while (true) {
                int actual = read(buffer, count, 1);
                
    
	            if (actual == -1) {
	                try {
	                    close();
	                }
	                catch (IOException ignored) {
	                }
	                
	                throw new IOException("Connection closed");
	            }

	            else if (actual == 0) {
	                try {
	                    Thread.yield();
	                }
	                catch (Exception ignored) {
	                }
	            }
  
	            else {
                    byte b = buffer[count];
                    

                    if (b == 0x0D) {
                      
                    }
                   
                    else if (b == 0x0A) {
                        stop = true;
                        break;
                    }
                    
                    else {
                        count++;
                        if (count == buffer.length) break;
                    }
	            }
	        }

            result = result + new String(buffer, 0, count);
        }
        
        if (true) System.out.println("[RECV] " + result);

        return result;
    }
    protected static int read(byte[] buffer, int offset, int count) throws IOException {
        return in.read(buffer, offset, count);
    }
    public static void close() throws IOException {

        if (socket != null) {
            socket.close();
            socket = null;
        }
    }

            
            
}

 But i get an dont get the answer for the EHLO message:
[RECV] 220 mx.google.com ESMTP ne3sm10019247lab.7

[SEND] EHLO localhost
And nothing more:smileysad:
But it works fine on desktop application
Thanks for any reply :smileyhappy: 

Please use plain text.
Contributor
hengike
Posts: 47
Registered: ‎08-26-2011
My Device: dsa

Re: Smtp gmail problem

up

Please use plain text.