从BlackBerry java应用程序上的gmail smtp服务器发送邮件

时间:2013-09-17 12:22:41

标签: java blackberry

如何使用我的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 at   您的服务,[115.119.250.30] 250-SIZE 35882577 250-8BITMIME   250-STARTTLS 250-ENHANCEDSTATUSCODES 250 CHUNKING 530 5.7.0必须发布   首先是STARTTLS命令。 i8sm3937388wiy.6 - gsmtp 502 5.5.1   无法识别的命令。 i8sm3937388wiy.6 - gsmtp

0 个答案:

没有答案