在Android中发送邮件抛出java.lang.NoClassDefFoundError:javax.activation.DataHandler

时间:2017-08-31 18:30:21

标签: java android javamail

我知道这个问题有很多答案,但我仍然在问这个问题,因为没有答案解决了我的问题。

我尝试了在这个问题的其他版本中讨论过的所有修复,但没有任何帮助。

我的代码:

package com.blogspot.ram914.flames;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import javax.mail.Authenticator;
import java.util.Properties;

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 SendMail extends AppCompatActivity {

private final String MAIL = "my e-mail";
private final String PASSWORD = "my password";

EditText etSenderMaildId;
EditText etSenderName;
EditText etRecipientMailId;
EditText etRecipientName;
EditText etMessage;
Button btnSend;

ProgressDialog pdialog = null;
Context context = null;

String RelationName;

static Session session = null;
// First go and Turn on less secure app access to sender mail
static String receiverMailId = "", subject = "", textMessage = "";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_send_mail);

    context = this;

    String senderName;
    String senderMailId;
    String receiverName;

    Bundle b = getIntent().getExtras();
    RelationName = b.getString("relation_name");
    senderName = b.getString("sender_name");
    receiverName = b.getString("recipient_name");

    etSenderMaildId = (EditText) findViewById(R.id.sender_mail_id);
    etSenderName = (EditText) findViewById(R.id.sender_name);
    etRecipientMailId = (EditText) findViewById(R.id.recipient_mail_id);
    etRecipientName = (EditText) findViewById(R.id.recipiend_name);
    etMessage = (EditText) findViewById(R.id.meassage);
    btnSend = (Button) findViewById(R.id.send);

    senderMailId = getSenderMailId();
    etSenderMaildId.setText(senderMailId);
    etSenderMaildId.setEnabled(false);

    etSenderName.setText(senderName);
    etSenderName.setEnabled(false);

    etRecipientName.setText(receiverName);
    etRecipientName.setEnabled(false);

}

@Override
protected void onPause() {
    super.onPause();
    if(pdialog != null)
        pdialog.dismiss();
    pdialog = null;
}

@Override
protected void onStop() {
    super.onStop();
    if(pdialog != null)
        pdialog.dismiss();
    pdialog = null;
}

public void sendTo(View view) {
    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");

    receiverMailId = etRecipientMailId.getText().toString();
    subject = "From Flames App";
    textMessage = getMessageContent();

    session = Session.getDefaultInstance(props, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(SendMail.this.MAIL, SendMail.this.PASSWORD);
        }
    });

    System.out.println("Ram Prasad");
    try{
        pdialog = ProgressDialog.show(context, "", "Sending Mail...", true);

    } catch(Exception e) {
        Log.d("\n\n\nStatus :","Failed\n\n\n");
    }

    System.out.println("Ram Prasad");
    RetreiveFeedTask task = new RetreiveFeedTask();
    task.execute("");
}

public void enableEdit(View view) {
    view.setEnabled(true);
}

public String getSenderMailId() {
    AccountManager manager = AccountManager.get(this);
    Account[] accounts = manager.getAccounts();

    for (Account account : accounts) {
        if(account.type.equals("com.google")) {
            return account.name;
        }
        System.out.println("\n\n"+account.name+"\n\n");
    }
    return "your mail id";
}

public String getMessageContent() {
    return "Hi "+etRecipientName.getText().toString()+",\n"+
            "Do you know "+etSenderName.getText().toString()+"?\n"
            +etSenderName.getText().toString()+" played flames with you and got the below relation.\n"
            +RelationName+"\n\n  and \n"
            +etSenderName.getText().toString()+" sent a message for you :\n"
            +etMessage.getText().toString()+" \n\n\n"
            +"Sender : "+etSenderMaildId.getText().toString();
}

private class RetreiveFeedTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        try{
compile            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("rbablu94@gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(SendMail.receiverMailId));
            message.setSubject(SendMail.subject);
            message.setText(SendMail.textMessage);
            Transport.send(message);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        if(pdialog != null)
            pdialog.dismiss();
        pdialog = null;
        Toast.makeText(getApplicationContext(), "Message sent", Toast.LENGTH_LONG).show();
    }
}

}

例外是:

Caused by: java.lang.NoClassDefFoundError:javax.activation.DataHandler
at javax.mail.internet.MimeMessage.setContent(MimeMessage.java:1566)
at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:1219)
at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1607)
at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1590)
at com.blogspot.ram914.flames.SendMail$RetreiveFeedTask.doInBackground(SendMail.java:168)
at com.blogspot.ram914.flames.SendMail$RetreiveFeedTask.doInBackground(SendMail.java:158)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:864)

我已将 mail.jar activation.jar 添加为库。 see here

我一直在努力让这个代码运行而不会崩溃我的应用程序。 任何人都可以帮助我吗?

编辑:

Caused by: java.lang.NoClassDefFoundError: java.nio.charset.StandardCharsets
at com.sun.mail.smtp.SMTPTransport$LoginAuthenticator.doAuth(SMTPTransport.java:982)
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:927)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:861)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:766)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
at com.blogspot.ram914.flames.SendMail$RetreiveFeedTask.doInBackground(SendMail.java:169)
at com.blogspot.ram914.flames.SendMail$RetreiveFeedTask.doInBackground(SendMail.java:158)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
at java.lang.Thread.run(Thread.java:864) 

1 个答案:

答案 0 :(得分:3)

您似乎正在使用旧方法。如果是这样,你就错过了一个罐子。

javamail

在此处查看适用于Android的javamailhttps://code.google.com/archive/p/javamail-android/downloads

注意:上述项目已经过时,可能不再受支持了。

如果您想使用具有Android支持的新JavaMail 1.6.0,您可以在此处获取javax.mail.jarhttps://github.com/javaee/javamail/releases

然后,只需将依赖项添加到gradle文件中:

dependencies {
   compile 'com.sun.mail:android-mail:1.6.0'
   compile 'com.sun.mail:android-activation:1.6.0'
}