使用Javamail for Android发送电子邮件

时间:2014-04-24 01:35:36

标签: android javamail

以下是我使用的代码: http://javapapers.com/android/android-email-app-with-gmail-smtp-using-javamail/

错误似乎出现在GMail.sendMail行中,传输尝试使用有效的用户名和密码连接到gmail。

我似乎无法找到任何解决方法,这很奇怪,因为大约一个月前,这段代码运作得很好。

DEBUG:

 04-23 21:21:41.526: I/System.out(27546): DEBUG: getProvider() returning  javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport, Sun   Microsystems, Inc.,1.4.1]
 04-23 21:21:41.526: I/System.out(27546): DEBUG SMTP: useEhlo true, useAuth true 
 04-23 21:21:41.526: I/System.out(27546): DEBUG SMTP: trying to connect to host  "smtp.gmail.com", port 587, isSSL false
 04-23 21:21:41.526: D/AndroidRuntime(27546): Shutting down VM

Logcat:

04-23 21:25:53.651: E/AndroidRuntime(28125): java.lang.IllegalStateException: Could not execute method of the activity
04-23 21:25:53.651: E/AndroidRuntime(28125): Caused by:java.lang.reflect.InvocationTargetException
04-23 21:25:53.651: E/AndroidRuntime(28125): at  android.view.View$1.onClick(View.java:3964) This is clicking a button
04-23 21:25:53.651: E/AndroidRuntime(28125): Caused by: android.os.NetworkOnMainThreadException
04-23 21:25:53.651: E/AndroidRuntime(28125): at  com.javapapers.android.androidjavamail.GMail.sendEmail(GMail.java:74)

谢谢你们!

3 个答案:

答案 0 :(得分:0)

您的应用程序中出现“NetworkOnMainThreadException”。您必须在后台线程上实现任何网络操作。如果您使用MainThread,如活动,服务等,它可能会产生ANR。请使用“AsyncTask”或“IntentService”发送电子邮件。

请看这个答案。 How to fix android.os.NetworkOnMainThreadException?

答案 1 :(得分:0)

如果您遇到此问题,那么您应该检查您的api级别 -

Till Donut它用于创建之前使用的1.6-2.3的多个线程 但如果现在使用3.0或更高版本,那么它将用于创建单线程模型 使用AsyncTask,否则你将获得NetworkOnMainThreadException。

答案 2 :(得分:0)

1.在InvocationTargetException上使用getCause()方法来检索原始异常。

2.从Method.invoke()的Javadoc

Throws: InvocationTargetException - if the underlying method throws an exception.

如果调用的方法引发异常,则抛出此异常。

you got this exception due to:

  (i)List all jar files from the Eclipse Navigator mode

  (ii)Verify that all the jar files are in binary mode.

3.您可以使用以下代码从Android客户端发送电子邮件。

 import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.mail.Authenticator;import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EMailSender {
public EMailSender(String host, final String from, final String pass, String to, String  sub, String mess) throws Exception {
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
Authenticator auth = new Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}};
Session session = Session.getInstance(props, auth);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(sub);
message.setText(mess);
Transport.send(message);
}

public static void main(String arg[]) throws Exception {
if(arg.length == 5) {
StringBuilder message = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String temp = "", subject;
System.out.print("Enter subject: ");
subject = br.readLine();
System.out.println("Enter the message (end it with a . on a single line):");
while((temp = br.readLine()) != null) {
if(temp.equals("."))
break;
message.append(temp+"\n");
}
System.out.println("Sending message...");
new EMailSender(arg[0], arg[1], arg[2], arg[3], subject, message.toString());
System.out.println("Sent the message.");
  }
else System.err.println("Usage:\njava SendTextMail <host> <port> <from> <pass> <to>");
}
}

在按钮上的活动中调用此类的构造函数单击。