1

我想使用 android 设备从我们自己的域(如 xyz@mycompany.com)发送一封电子邮件。

我们可以使用 mainActivity 中编写的以下代码从 gamil 帐户发送邮件。

字符串 managerUserName = "xyz@gmail.com";

        System.out.println("Üser name = " + userName + "   Password =="
                + password);
        if ((userName != null && userName.length() != 0)
                && (password != null && password.length() != 0)
                && (managerUserName != null && managerUserName.length() != 0)) {
            if (Util.isInternetOn(OrderActivity.this)) {
                Mail m = new Mail(userName, password);
                String[] toArr = { managerUserName };
                m.setTo(toArr);
                m.setFrom("wooo@wooo.com");

                m.setSubject("MForce");
                m.setBody("This mail is sended by Mforce application");

在 Mail.java 中,我们使用以下代码设置主机和端口

public Mail() { 
    _host = "smtp.gmail.com"; // default smtp server 
    _port = "465"; // default smtp port 
    _sport = "465"; // default socketfactory port 

      }

当我们尝试使用自己的域时,我们像这样更改了 Mail.java 中的主机名和端口号。

public Mail() { 

      _host = "smtp.myServerIp.com"; // default smtp server 
        _port = "25"; // default smtp port 
        _sport = "25"; // default socketfactory port 

}

当我尝试使用我们的个人域从我的应用程序发送邮件时,使用 smtp,我得到 javax.mail.messagingexception 无法连接到 logCat 中的 smtp 主机异常,这是我的 logCat。

LOGCAT

05-29 12:36:24.403: E/MailApp(14784): Could not send email
05-29 12:36:24.403: E/MailApp(14784): javax.mail.MessagingException: Could not connect to SMTP host: 213.42.106.10, port: 25;
05-29 12:36:24.403: E/MailApp(14784):   nested exception is:
05-29 12:36:24.403: E/MailApp(14784):   javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5d5c34a0: Failure in SSL library, usually a protocol error
05-29 12:36:24.403: E/MailApp(14784): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x56cbb76a:0x00000000)
05-29 12:36:24.403: E/MailApp(14784):   at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
05-29 12:36:24.403: E/MailApp(14784):   at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
05-29 12:36:24.403: E/MailApp(14784):   at javax.mail.Service.connect(Service.java:310)
05-29 12:36:24.403: E/MailApp(14784):   at javax.mail.Service.connect(Service.java:169)
05-29 12:36:24.403: E/MailApp(14784):   at javax.mail.Service.connect(Service.java:118)
05-29 12:36:24.403: E/MailApp(14784):   at javax.mail.Transport.send0(Transport.java:188)
05-29 12:36:24.403: E/MailApp(14784):   at javax.mail.Transport.send(Transport.java:118)
05-29 12:36:24.403: E/MailApp(14784):   at com.intertec.iforce.mail.Mail.send(Mail.java:134)
05-29 12:36:24.403: E/MailApp(14784):   at com.intertec.iforce.OrderActivity$MailSender.doInBackground(OrderActivity.java:1977)
05-29 12:36:24.403: E/MailApp(14784):   at com.intertec.iforce.OrderActivity$MailSender.doInBackground(OrderActivity.java:1)
05-29 12:36:24.403: E/MailApp(14784):   at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-29 12:36:24.403: E/MailApp(14784):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-29 12:36:24.403: E/MailApp(14784):   at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-29 12:36:24.403: E/MailApp(14784):   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-29 12:36:24.403: E/MailApp(14784):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-29 12:36:24.403: E/MailApp(14784):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-29 12:36:24.403: E/MailApp(14784):   at java.lang.Thread.run(Thread.java:856)
05-29 12:36:24.403: E/MailApp(14784): Caused by: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5d5c34a0: Failure in SSL library, usually a protocol error
05-29 12:36:24.403: E/MailApp(14784): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x56cbb76a:0x00000000)
05-29 12:36:24.403: E/MailApp(14784):   at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:412)
05-29 12:36:24.403: E/MailApp(14784):   at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.<init>(OpenSSLSocketImpl.java:623)
05-29 12:36:24.403: E/MailApp(14784):   at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:594)
05-29 12:36:24.403: E/MailApp(14784):   at com.sun.mail.smtp.SMTPTransport.initStreams(SMTPTransport.java:1449)
05-29 12:36:24.403: E/MailApp(14784):   at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1366)
05-29 12:36:24.403: E/MailApp(14784):   ... 16 more
05-29 12:36:24.403: E/MailApp(14784): Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5d5c34a0: Failure in SSL library, usually a protocol error
05-29 12:36:24.403: E/MailApp(14784): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:766 0x56cbb76a:0x00000000)
05-29 12:36:24.403: E/MailApp(14784):   at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
05-29 12:36:24.403: E/MailApp(14784):   at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:371)
05-29 12:36:24.403: E/MailApp(14784):   ... 20 more

请一些人帮助我,在此先感谢。

4

1 回答 1

2

有个窍门。

在您的 mail.java 中必须有此代码..

props.put("mail.smtp.socketFactory.port", "465"); 
   props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
   props.put("mail.smtp.port", _port); 
   props.put("mail.smtp.socketFactory.fallback", "false");

这里开始的两行不适用于不同的服务器,要么你需要评论它,要么你可以这样做

 if(_host.contains("gmail")||_host.contains("yahoo")){
   props.put("mail.smtp.socketFactory.port", "465"); 
   props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
 }

   props.put("mail.smtp.port", _port); 
   props.put("mail.smtp.socketFactory.fallback", "false");

现在看看魔法:)

于 2013-06-04T07:16:43.807 回答