无法在Android应用中使用javax发送电子邮件

时间:2014-04-16 18:16:02

标签: java android smtp javamail

我使用以下代码发送电子邮件:

 Properties props = new Properties();
       props.put("mail.smtp.host", "smtp.gmail.com");
       props.put("mail.smtp.socketFactory.port", "465");
       props.put("mail.smtp.socketFactory.class",
               "javax.net.ssl.SSLSocketFactory");
       props.put("mail.smtp.auth", "true");
       props.put("mail.smtp.port", "465");

       Session session = Session.getDefaultInstance(props,
               new javax.mail.Authenticator() {
                   protected PasswordAuthentication getPasswordAuthentication() {
                       return new PasswordAuthentication("sender@gmail.com","123456");
                   }
               });

       try {

           Message message = new MimeMessage(session);
           message.setFrom(new InternetAddress("sender@gmail.com"));
           message.setRecipients(Message.RecipientType.TO,
                   InternetAddress.parse("reciever@gmail.com"));
           message.setSubject("Testing Subject");
           message.setText("Dear Mail Crawler," +
                   "\n\n No spam to my email, please!");

           Transport.send(message);

           System.out.println("Done");

       } catch (MessagingException e) {
           throw new RuntimeException(e);
       }

我已经在我的android项目中需要.jar。没有例外,但我没有收到任何电子邮件。

Android的

Logcat

04-16 23:40:08.972  18170-18170/com.coderwall.executer W/System.err﹕    android.os.NetworkOnMainThreadException
04-16 23:40:08.972  18170-18170/com.coderwall.executer W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1126)
04-16 23:40:08.972  18170-18170/com.coderwall.executer W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-16 23:40:08.972  18170-18170/com.coderwall.executer W/System.err﹕ at java.net.InetAddress.getLocalHost(InetAddress.java:365)
04-16 23:40:08.972  18170-18170/com.coderwall.executer W/System.err﹕ at javax.mail.internet.InternetAddress.getLocalHostName(InternetAddress.java:563)
04-16 23:40:08.972  18170-18170/com.coderwall.executer W/System.err﹕ at javax.mail.internet.InternetAddress._getLocalAddress(InternetAddress.java:542)
04-16 23:40:08.982  18170-18170/com.coderwall.executer W/System.err﹕ at javax.mail.internet.InternetAddress.getLocalAddress(InternetAddress.java:513)
04-16 23:40:08.982  18170-18170/com.coderwall.executer W/System.err﹕ at javax.mail.internet.UniqueValue.getUniqueMessageIDValue(UniqueValue.java:99)
04-16 23:40:08.982  18170-18170/com.coderwall.executer W/System.err﹕ at    javax.mail.internet.MimeMessage.updateMessageID(MimeMessage.java:2152)
04-16 23:40:08.982  18170-18170/com.coderwall.executer W/System.err﹕ at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2181)
04-16 23:40:08.982  18170-18170/com.coderwall.executer W/System.err﹕ at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2141)
04-16 23:40:08.982  18170-18170/com.coderwall.executer W/System.err﹕ at javax.mail.Transport.send(Transport.java:123)
04-16 23:40:08.982  18170-18170/com.coderwall.executer W/System.err﹕ at com.coderwall.executer.MainActivity.getComputation(MainActivity.java:275)
04-16 23:40:08.982  18170-18170/com.coderwall.executer W/System.err﹕ at com.coderwall.executer.MainActivity$1$1$2.run(MainActivity.java:90)
04-16 23:40:08.982  18170-18170/com.coderwall.executer W/System.err﹕ at com.coderwall.executer.MainActivity$1$1.onPostExecute(MainActivity.java:109)
04-16 23:40:08.982  18170-18170/com.coderwall.executer W/System.err﹕ at c  com.coderwall.executer.MainActivity$1$1.onPostExecute(MainActivity.java:61)
04-16 23:40:08.992  18170-18170/com.coderwall.executer W/System.err﹕ at android.os.AsyncTask.finish(AsyncTask.java:631)
04-16 23:40:08.992  18170-18170/com.coderwall.executer W/System.err﹕ at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-16 23:40:08.992  18170-18170/com.coderwall.executer W/System.err﹕ at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
04-16 23:40:08.992  18170-18170/com.coderwall.executer W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 23:40:08.992  18170-18170/com.coderwall.executer W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
04-16 23:40:08.992  18170-18170/com.coderwall.executer W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:4744)
04-16 23:40:08.992  18170-18170/com.coderwall.executer W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
04-16 23:40:08.992  18170-18170/com.coderwall.executer W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
04-16 23:40:08.992  18170-18170/com.coderwall.executer W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-16 23:40:09.002  18170-18170/com.coderwall.executer W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-16 23:40:09.002  18170-18170/com.coderwall.executer W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

我在这里缺少什么?

我已经在AndroidManifest.xml中添加了以下行:

<intent-filter>
     <action android:name="android.intent.action.MAIN" />
     <uses-permission android:name="android.permission.INTERNET"/>
     <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

1 个答案:

答案 0 :(得分:1)

您无法在主线程中发出网络请求。在单独的线程中进行:

new Thread(new Runnable(){
  public void run(){
    //...your code
  }
}).start();

或使用AsyncTask:http://developer.android.com/reference/android/os/AsyncTask.html