1

I am trying to call a class from onResume() of my activity. But evrytime i am getting class not found error.

Here is the code for the onResume():

 @Override
    protected void onResume() {
        super.onResume();
      try {   
          sendHiddenMail sender = new sendHiddenMail("sidh*****@gmail.com", "pass");
          sender.sendMail("This is Subject",   
                  "This is Body",   
                  "sid*****@gmail.com",   
                  "sid*****@gmail.com");   
      } catch (Exception e) {   
          Log.e("SendMailsdfsdfsd", e.getMessage(), e);   
      }

 }

Here is the class whom I want to call:

public class sendHiddenMail extends javax.mail.Authenticator{

private String mailhost = "smtp.gmail.com";   
private String user;   
private String password;   
private Session session;   

static {   
    Security.addProvider(new com.provider.JSSEProvider());   
}  


 public sendHiddenMail(String user, String password) {   
        this.user = user;   
        this.password = password;   

        Properties props = new Properties();   
        props.setProperty("mail.transport.protocol", "smtp");   
        props.setProperty("mail.host", mailhost);   
        props.put("mail.smtp.auth", "true");   
        props.put("mail.smtp.port", "465");   
        props.put("mail.smtp.socketFactory.port", "465");   
        props.put("mail.smtp.socketFactory.class",   
                "javax.net.ssl.SSLSocketFactory");   
        props.put("mail.smtp.socketFactory.fallback", "false");   
        props.setProperty("mail.smtp.quitwait", "false");   

        session = Session.getDefaultInstance(props, this);   
    }   

    protected PasswordAuthentication getPasswordAuthentication() {   
        return new PasswordAuthentication(user, password);   
    }   

    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {   
        try{
        MimeMessage message = new MimeMessage(session);   
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));   
        message.setSender(new InternetAddress(sender));   
        message.setSubject(subject);   
        message.setDataHandler(handler);   
        if (recipients.indexOf(',') > 0)   
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
        else  
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
        Transport.send(message);   
        }catch(Exception e){

        }
    }   

    public class ByteArrayDataSource implements DataSource {   
        private byte[] data;   
        private String type;   

        public ByteArrayDataSource(byte[] data, String type) {   
            super();   
            this.data = data;   
            this.type = type;   
        }   

        public ByteArrayDataSource(byte[] data) {   
            super();   
            this.data = data;   
        }   

        public void setType(String type) {   
            this.type = type;   
        }   

        public String getContentType() {   
            if (type == null)   
                return "application/octet-stream";   
            else  
                return type;   
        }   

        public InputStream getInputStream() throws IOException {   
            return new ByteArrayInputStream(data);   
        }   

        public String getName() {   
            return "ByteArrayDataSource";   
        }   

        public OutputStream getOutputStream() throws IOException {   
            throw new IOException("Not Supported");   
        }   
    }   

}

Here is the logcat:

06-03 03:22:57.331: W/dalvikvm(4934): Unable to resolve superclass of Lcom/example/calllist/sendHiddenMail; (67)
06-03 03:22:57.331: W/dalvikvm(4934): Link of class 'Lcom/example/calllist/sendHiddenMail;' failed
06-03 03:22:57.331: E/dalvikvm(4934): Could not find class 'com.example.calllist.sendHiddenMail', referenced from method com.example.calllist.MainActivity.sendMail
06-03 03:22:57.331: W/dalvikvm(4934): VFY: unable to resolve new-instance 34 (Lcom/example/calllist/sendHiddenMail;) in Lcom/example/calllist/MainActivity;
06-03 03:22:57.331: D/dalvikvm(4934): VFY: replacing opcode 0x22 at 0x0000
06-03 03:22:57.341: W/dalvikvm(4934): Unable to resolve superclass of Lcom/example/calllist/sendHiddenMail; (67)
06-03 03:22:57.341: W/dalvikvm(4934): Link of class 'Lcom/example/calllist/sendHiddenMail;' failed
06-03 03:22:57.401: D/dalvikvm(4934): DexOpt: unable to opt direct call 0x0039 at 0x06 in Lcom/example/calllist/MainActivity;.sendMail
06-03 03:22:57.791: D/AndroidRuntime(4934): Shutting down VM
06-03 03:22:57.791: W/dalvikvm(4934): threadid=1: thread exiting with uncaught exception (group=0xb1af6b90)
06-03 03:22:57.811: E/AndroidRuntime(4934): FATAL EXCEPTION: main
06-03 03:22:57.811: E/AndroidRuntime(4934): Process: com.example.calllist, PID: 4934
06-03 03:22:57.811: E/AndroidRuntime(4934): java.lang.NoClassDefFoundError: com.example.calllist.sendHiddenMail
06-03 03:22:57.811: E/AndroidRuntime(4934):     at com.example.calllist.MainActivity.sendMail(MainActivity.java:102)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at com.example.calllist.MainActivity.getCallDetails(MainActivity.java:81)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at com.example.calllist.MainActivity.onResume(MainActivity.java:35)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at android.app.Activity.performResume(Activity.java:5322)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2759)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2798)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2231)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at android.os.Looper.loop(Looper.java:137)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at android.app.ActivityThread.main(ActivityThread.java:4998)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at java.lang.reflect.Method.invokeNative(Native Method)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at java.lang.reflect.Method.invoke(Method.java:515)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
06-03 03:22:57.811: E/AndroidRuntime(4934):     at dalvik.system.NativeStart.main(Native Method)
06-03 03:27:58.391: I/Process(4934): Sending signal. PID: 4934 SIG: 9

I have not added any information about sendHiddenMail class in mainfest.xml

I have no idea where I am going wrong.

TZHX
  • 5,291
  • 15
  • 47
  • 56
Sidharth Dash
  • 333
  • 1
  • 4
  • 13
  • i hav eposted the logcat – Sidharth Dash Jun 03 '15 at 09:00
  • It's pretty basic, but have you imported the class (assuming there's a need to). Also, what's the structure of your packages? – SlashG Jun 03 '15 at 09:24
  • That superclass `javax.mail.Authenticator` is imported? By the way, you should use class names beginning with an upper case letter. – Sven Menschner Jun 03 '15 at 09:24
  • Off-topic : you should name your classes in TitleCase. – SlashG Jun 03 '15 at 09:24
  • No javax.mail.Authenticator was not imported. But i have tried it after importing it. No change . Still throws the same error. Will keep in mind the naming conventions. The activity class and sendHiddenMail class are in the same package. – Sidharth Dash Jun 03 '15 at 10:13
  • I thing i found the error : **extends javax.mail.Authenticator** is causing the issue. I tried running without it , the code runs smoothly. But in real case i need to extend. Can any one have idea how to work with this. – Sidharth Dash Jun 03 '15 at 10:56

1 Answers1

0

When you have imported javax.mail.Authenticator correctly, get no error at "compile" time and the app crashes at runtime, the solution might be this link. Yes, if you remove the class relationship, your code may work but it doesn't solve your problem. Try cleaning and rebuilding the project.

Community
  • 1
  • 1
Sven Menschner
  • 603
  • 5
  • 12