0

I am new to android development . I am making a small email client using javamail api . I am not able to find a solution on how will that activity start . Below is my code file for email manager which I want to start when I run the application .

I just need a way to start this , I am really confused with the android activities and have clue how to implement them Thanks in Advance :)

package com.mailtest.android;

import android.app.Activity;
import android.os.Bundle;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import android.view.View;
import android.os.Bundle;
import android.content.Intent;
import android.widget.Button;

public class EmailManager extends Activity {


    **/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);** 
    } 
    private String stmpHost = "smtp.gmail.com";
        private String mailServer = "imap.gmail.com";
        private EmailAccount account;
        private Session smtpSession; 
        private Session imapSession; 
        private Folder inbox;
        private Store store;



        public EmailManager(String username, String password, String urlServer, String stmpHost, String mailServer) {
            account = new EmailAccount(username, password, urlServer);
            this.stmpHost = stmpHost;
            this.mailServer = mailServer;
            initProtocol();
        }
        private void initProtocol() {
            EmailAuthenticator authenticator = new EmailAuthenticator(account);

            Properties props1 = new Properties();  
            props1.setProperty("mail.transport.protocol", "smtps");  
            props1.setProperty("mail.host", stmpHost);  
            props1.put("mail.smtp.auth", "true");  
            props1.put("mail.smtp.port", "465");  
            props1.put("mail.smtp.socketFactory.port", "465");  
            props1.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");  
            props1.put("mail.smtp.socketFactory.fallback", "false");  
            props1.setProperty("mail.smtp.quitwait", "false");  
            smtpSession = Session.getDefaultInstance(props1, authenticator); 

            Properties props2 = new Properties();
            props2.setProperty("mail.store.protocol", "imaps");
            props2.setProperty("mail.imaps.host", mailServer);
            props2.setProperty("mail.imaps.port", "993");
            props2.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props2.setProperty("mail.imaps.socketFactory.fallback", "false");
            imapSession = Session.getInstance(props2);
        }   
        public Message[] getMails() throws MessagingException {
            store = imapSession.getStore("imaps");
            store.connect(mailServer, account.username, account.password);
            inbox = store.getFolder("Inbox");
            inbox.open(Folder.READ_ONLY);
            Message[] result = inbox.getMessages();

            return result;
        }
        public void close() {
            //Close connection 
            try {
                inbox.close(false);
                store.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
        }
        public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {  
            MimeMessage message = new MimeMessage(smtpSession);
            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);  


            }
        } 

AND THIS IS MY MAIN.XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />

</LinearLayout>  
vidstige
  • 12,492
  • 9
  • 66
  • 110
user832981
  • 49
  • 1
  • 7

2 Answers2

0

First of, you might want to have the email manager in its own class since it´s not a necessity to extend it as an Activity. However, to answer your question you could have a look at http://developer.android.com/reference/android/app/Activity.html

This is just an example and let´s say that the activity that should start the other activity is called MainActivity. Then put this in MainActivity:

  startActivity(new Intent(MainActivity.this, EmailManager.class)); 

Just remember to put the Email Manager as an Activity in your AndroidManifest.xml

<activity android:name=".EmailManager"/>

Possible duplicate answer, more can be found here: How to start activity in another application?

Community
  • 1
  • 1
David Olsson
  • 8,085
  • 3
  • 30
  • 38
0

Go back to read this: http://developer.android.com/reference/android/app/Activity.html

Do not extend Activity for your EmailManager, you don't need to, as EmailManager doesn't interact with the user interface and doesn't depend on any context.

Implement a separate Activity where you call the relevant EmailManager methods from the Activity methods (OnCreate, OnResume, etc.) OnCreate is where your activity starts by the way

When you get a better understanding of the lifecycle of android components you are better of moving things from your activity to a service: http://developer.android.com/reference/android/app/Service.html This is again because you have no ui-interaction from your EmailManager, and the type of work your EmailManager does is better suited for a service.

Marmoy
  • 8,009
  • 7
  • 46
  • 74