我有一个发送电子邮件的Android应用程序。但是当我想发送电子邮件时,我的程序完全崩溃了,我收到了错误:
处理:com.example.jonas.shoppinglist,PID:16791 java.lang.RuntimeException:执行doInBackground()时发生错误 在android.os.AsyncTask $ 3.done(AsyncTask.java:300) 在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 在java.util.concurrent.FutureTask.run(FutureTask.java:242) 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 在java.lang.Thread.run(Thread.java:818) 引起:java.lang.NoClassDefFoundError:javax.activation.DataHandler 在javax.mail.internet.MimeMessage.setContent(MimeMessage.java:1508) 在javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:1155) 在javax.mail.internet.MimeMessage.setText(MimeMessage.java:1547) 在javax.mail.internet.MimeMessage.setText(MimeMessage.java:1531)
现在我显示电子邮件发件人的代码,问题行是message.setText(“实际文本”);
import android.util.Log;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.activation.*;
public class MailSender {
private String body;
public void send() {
// Recipient's email ID needs to be mentioned.
String to = "abcd@gmail.com";
// Sender's email ID needs to be mentioned
String from = "web@gmail.com";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
我已经做了一些研究,人们说这是一个问题,但我认为我都需要依赖。 这是我的依赖关系:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.google.android.gms:play-services-gcm:7.3.0'
compile 'javax.activation:activation:1.1'
compile 'javax.mail:mail:1.5.0-b01'
compile 'javax.activation:activation:1.1.1'
}
出了什么问题?
答案 0 :(得分:0)
您正在使用可能不适合Android的Java库(javax.mail:mail:1.5.0-b01)。有一个Android版本的javamail: https://code.google.com/p/javamail-android/你应该尝试一下。不要指望每个使用Java的库都可以在Android上运行。