0

如何从我的 BlackBerry 应用程序使用 gmail smtp 服务器发送邮件。

我使用波纹管代码,但它不适用于我。

 public void sendEmail() {
    int ch;
    byte[] b = new byte[2046];

    try {

        sc = (SocketConnection) Connector.open("socket://" + smtpServerAdress + ":587" + getConnectionString());
        is = sc.openInputStream();
        os = sc.openOutputStream();
        os.write(("EHLO" + "\r\n").getBytes());
        os.write(("AUTH LOGIN" + "\r\n").getBytes());
        //authentication
        os.write((from_emailAdress + "\r\n").getBytes()); // message body // mail id here For Authentication
        os.write(("password" + "\r\n").getBytes()); // 

        os.write(("MAIL FROM: "+ from_emailAdress +"\r\n").getBytes());
        os.write(("RCPT TO: "+ to_emailAdress + "\r\n").getBytes());
        os.write("DATA\r\n".getBytes());
        // stamp the msg with date
        os.write(("Date: " + new Date() + "\r\n").getBytes()); 
        os.write(("From: "+from_emailAdress+"\r\n").getBytes());
        os.write(("To: "+to_emailAdress+"\r\n").getBytes());
        os.write(("Subject: "+"text"+"\r\n").getBytes());
        os.write(("test"+"\r\n").getBytes()); // message body
        os.write(".\r\n".getBytes());
        os.write("QUIT\r\n".getBytes());

        // debug
        StringBuffer sb = new StringBuffer();
        int c = 0;
        while (((c = is.read()) != -1) ) {
            sb.append((char) c);
        }

        System.out.println("SMTP server response - " + sb.toString());
        System.out.println("*************************** Mail Send **********************");



    } catch (Exception e) {
        System.out.println("*********************** Exception:" + e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            if (is != null) {
                is.close();
            }
            if (os != null) {
                os.close();
            }
            if (sc != null) {
                sc.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

当我调试这段代码时,我没有收到任何错误或任何异常。我得到以下回复:

220 mx.google.com ESMTP i8sm3937388wiy.6 - gsmtp 250-mx.google.com 为您服务,[115.119.250.30] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250 必须发布 530 5.7首先是 STARTTLS 命令。i8sm3937388wiy.6 - gsmtp 502 5.5.1 无法识别的命令。i8sm3937388wiy.6 - gsmtp

4

0 回答 0