5

I have a thread inside my main activity, which will create an object of the class SendMail

package Logic;

import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
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.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import android.util.Log;

public class SendMail {

String from;
String to;
String subject;
String bodyText;
String fileName;

public SendMail(String to, String fileName, String PCN) {

    this.to = to;
    this.fileName = fileName;
    this.from = "Hello@gmail.com";
    this.bodyText = "FILE";
    this.subject = PCN;
}

public void sendMailWithAttatchment() {

    Properties properties = new Properties();
    properties.put("mail.smtp.host", "IP_ADDRESS");
    properties.put("mail.smtp.port", "25");
    Session session = Session.getDefaultInstance(properties, null);

    MimeMessage message = new MimeMessage(session);

    try {

        message.setFrom(new InternetAddress(from));

        message.setRecipient(Message.RecipientType.TO, new InternetAddress(
                to));
        message.setSubject(subject);
        message.setSentDate(new Date());

        MimeBodyPart messagePart = new MimeBodyPart();
        messagePart.setText(bodyText);

        MimeBodyPart attachmentPart = new MimeBodyPart();

        FileDataSource fileDataSource = new FileDataSource(fileName) {
            @Override
            public String getContentType() {
                return "application/octet-stream";

            }
        };

        attachmentPart.setDataHandler(new DataHandler(fileDataSource));
        attachmentPart.setFileName(fileDataSource.getName());

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messagePart);
        multipart.addBodyPart(attachmentPart);
        message.setContent(multipart);
        Transport.send(message);
    } catch (AddressException e) {
        Log.e("ADDRESS_EXCEPTION: ", e.getMessage());
    } catch (MessagingException e) {
        Log.e("MESSAGING_EXCEPTION: ", e.getMessage());
    }
}

}

But the compiler throws an Exception saying: Java.lang.NoClassDefFoundError. javax.activation.Datahandler

I've read this thread: NoClassDefFoundError - Eclipse and Android and the .jar files javamail.jar and javax.activation.jar is located under my libs folder, but this throws an exception even if I clean the project.

Any ideas?

These are the exception which is thrown:

08-07 10:19:49.870: E/AndroidRuntime(17736): java.lang.NoClassDefFoundError: javax.activation.DataHandler
08-07 10:19:49.870: E/AndroidRuntime(17736):    at javax.mail.internet.MimeBodyPart.setContent(MimeBodyPart.java:647)
08-07 10:19:49.870: E/AndroidRuntime(17736):    at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:892)
08-07 10:19:49.870: E/AndroidRuntime(17736):    at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:680)
08-07 10:19:49.870: E/AndroidRuntime(17736):    at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:668)
08-07 10:19:49.870: E/AndroidRuntime(17736):    at sendMailWithAttatchment(SendMail.java:56)
08-07 10:19:49.870: E/AndroidRuntime(17736):    at sendMailWithAttatchment(SendMail.java:56)
08-07 10:19:49.870: E/AndroidRuntime(17736):    at CreateNistFile(MyActivity.java:530)
Community
  • 1
  • 1
Tobias Moe Thorstensen
  • 8,861
  • 16
  • 75
  • 143

4 Answers4

5

java introduce new way for javaMail for android :

you need just add thi lines in gradle:

 android {
     packagingOptions {
         pickFirst 'META-INF/LICENSE.txt' // picks the JavaMail license file
     }
 }

 repositories { 
     jcenter()
     maven {
         url "https://maven.java.net/content/groups/public/"
     }
 }

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

and do the smtp mail like you did... good luck.

Faez Mehrabani
  • 214
  • 1
  • 3
  • 9
2

Yes, this because of your .jar file didn't import properly. Just follow my existing answer It will helps you surely. And, below snapshot is important (It notifies the additional jar files should looks like this image) -

image

Important thing is, whenever you'd Java.lang.NoClassDefFoundError exception above one is the solution to handle that.

Community
  • 1
  • 1
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • Thanks for your answe, but this doesnt work for me. Before the exception is thrown, I got this debug information: Failed resolving Ljavax/activation/DataHandler; interface 272 'Ljava/awt/datatransfer/Transferable;' – Tobias Moe Thorstensen Aug 07 '12 at 07:26
  • Are you trying for [this](http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a) – Praveenkumar Aug 07 '12 at 07:31
  • No, I am following this tutorial: [link](http://www.kodejava.org/examples/243.html) I doesnt want to have any user interactions nor any popups of installed email clients on the phone. The mail should contain subject, bodytext and an attacment. – Tobias Moe Thorstensen Aug 07 '12 at 07:34
  • Fine. Now, can you please post your full `LogCat` trace after getting exception? – Praveenkumar Aug 07 '12 at 07:53
  • @TobiasMoeThorstensen Yes it seems, `.jar` file didn't imported properly. Did you try my existing answer. – Praveenkumar Aug 07 '12 at 09:03
0

If you are using eclipse, right click on project > choose properties> Java build Path> libraries> add Jars and add the jars. Then clean the project

Kamal
  • 5,462
  • 8
  • 45
  • 58
0

Android isn't fully Java-compliant, there's the javamail-android project that adds support for Javamail to Android apps.

make sure you download and add to build path all 3 jars from here: http://code.google.com/p/javamail-android/downloads/list

marmor
  • 27,641
  • 11
  • 107
  • 150