使用gmail smtp从Android应用程序发送电子邮件

时间:2015-03-05 06:49:16

标签: android email smtp

我正在使用本教程中的代码javapapers email app

我收到这些错误

03-05 01:33:25.649: E/SendMailTask(1926): null
03-05 01:33:25.649: E/SendMailTask(1926):        javax.mail.AuthenticationFailedException
03-05 01:33:25.649: E/SendMailTask(1926):   at    javax.mail.Service.connect(Service.java:319)
03-05 01:33:25.649: E/SendMailTask(1926):   at javax.mail.Service.connect(Service.java:169)
03-05 01:33:25.649: E/SendMailTask(1926):   at   com.javapapers.android.androidjavamail.GMail.sendEmail(GMail.java:82)
03-05 01:33:25.649: E/SendMailTask(1926):   at com.javapapers.android.androidjavamail.SendMailTask.doInBackground(SendMailTask.java:39)
03-05 01:33:25.649: E/SendMailTask(1926):   at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-05 01:33:25.649: E/SendMailTask(1926):   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-05 01:33:25.649: E/SendMailTask(1926):   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-05 01:33:25.649: E/SendMailTask(1926):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-05 01:33:25.649: E/SendMailTask(1926):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-05 01:33:25.649: E/SendMailTask(1926):   at java.lang.Thread.run(Thread.java:841)

和这个

03-05 01:33:25.799: E/AndroidRuntime(1926): FATAL EXCEPTION: main
03-05 01:33:25.799: E/AndroidRuntime(1926): Process: com.javapapers.android.androidjavamail, PID: 1926
03-05 01:33:25.799: E/AndroidRuntime(1926): java.lang.NullPointerException
03-05 01:33:25.799: E/AndroidRuntime(1926):     at com.javapapers.android.androidjavamail.SendMailTask.onProgressUpdate(SendMailTask.java:51)
03-05 01:33:25.799: E/AndroidRuntime(1926):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:648)
03-05 01:33:25.799: E/AndroidRuntime(1926):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-05 01:33:25.799: E/AndroidRuntime(1926):     at android.os.Looper.loop(Looper.java:136)
03-05 01:33:25.799: E/AndroidRuntime(1926):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-05 01:33:25.799: E/AndroidRuntime(1926):     at java.lang.reflect.Method.invokeNative(Native Method)
03-05 01:33:25.799: E/AndroidRuntime(1926):     at java.lang.reflect.Method.invoke(Method.java:515)
03-05 01:33:25.799: E/AndroidRuntime(1926):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-05 01:33:25.799: E/AndroidRuntime(1926):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-05 01:33:25.799: E/AndroidRuntime(1926):     at dalvik.system.NativeStart.main(Native Method)

代码如下。在Gmail.java类中,此行内的sendEmail方法javax.mail.AuthenticationFailedException即将到来

  

transport.connect(emailHost,fromEmail,fromPassword);

public void sendEmail() throws AddressException, MessagingException {

    Transport transport = mailSession.getTransport("smtp");
    System.out.println("emailHost="+emailHost);
    System.out.println("fromEmail="+fromEmail);
    System.out.println("fromPassword="+fromPassword);

    transport.connect(emailHost, fromEmail, fromPassword);
    Log.i("GMail","allrecipients: "+emailMessage.getAllRecipients());
    transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
    transport.close();
    Log.i("GMail", "Email sent successfully.");
}

在SendMailTask​​.java类中,我在此行获取NullPointerException

  

statusDialog.setMessage(值[0]的ToString());

@Override
public void onProgressUpdate(Object... values) {
statusDialog.setMessage(values[0].toString());
}

任何人都可以帮助我。

2 个答案:

答案 0 :(得分:1)

您需要提供您需要发送邮件的gmail帐户的确切电子邮件ID和密码

答案 1 :(得分:0)

我不知道Java部分,但我知道smtp,你没有包含的是smtp端口,允许Gmail将其代码解析为电子邮件编辑器,请按照谷歌关于smtp的链接并相应地重写您的代码。 Link = https://support.google.com/a/answer/176600?hl=en