1

Following is the code for Main class.

public class Main extends Activity {

EditText to, from, message, subject, pass; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button send =  (Button) findViewById(R.id.buttonSend);
    to = (EditText) findViewById(R.id.editTextTo);
    from = (EditText) findViewById(R.id.editTextFrom);
    subject = (EditText) findViewById(R.id.editTextSubject);
    message = (EditText) findViewById(R.id.editTextMessage);
    pass = (EditText) findViewById(R.id.editPassFrom);

   final String toString = to.toString();
   final String fromString = from.toString();
   final String passString = pass.toString();
   final String subjString = subject.toString();
   final String msgString = message.toString();

    send.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {


            try {   
                GMailSender sender = new GMailSender(fromString, passString);
                sender.sendMail(subjString,   
                        msgString,   
                        fromString,   
                        toString);   
            } catch (Exception e) {   
                Log.e("SendMail", e.getMessage(), e);   
            } 
            to.setText("");
            from.setText("");
            subject.setText("");
            message.setText("");
            pass.setText("");
        }
    });

}
}

Here is the code for GMailSender class

public class GMailSender 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 GMailSender(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");   
    }   
}   
}  

I worked on this code but it isn't working.

Here is the Logcat status :

02-15 01:15:47.132: I/Choreographer(1196): Skipped 30 frames! The application may be doing too much work on its main thread.

5
  • You have to use Asyntask... Commented Feb 15, 2014 at 6:26
  • just that? @InnocentKiller but in which class? Commented Feb 15, 2014 at 6:33
  • In which class you are getting error?? In gmailsender or Main Commented Feb 15, 2014 at 6:34
  • No error is coming.... jst logcat is showing for Main class Commented Feb 15, 2014 at 6:35
  • Okay then in Onclick of button call Asynctask and copy paste your code of button click event inside Asynctask class doInbackground method. Commented Feb 15, 2014 at 6:37

1 Answer 1

1

Just add Background Mail library and setup mail address very easy.

Check this library, it is the same what you expecting, Read the Read me file in this library to know the steps. Background Mail Library

Download the library, extract it, it will come up with library and demo project, import this library to your Eclipse IDE or whatever IDE you are working with android

Here is code for sample,

import com.kristijandraca.backgroundmaillibrary.BackgroundMail;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;


public class MainActivity extends Activity {

    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BackgroundMail bm = new BackgroundMail(MainActivity.this);
        bm.setGmailUserName("[email protected]");
        bm.setGmailPassword("yourpassword");
        bm.setMailTo("receivermail id");
        bm.setFormSubject("Backgroundmail");
        bm.setFormBody("TEsting");
        bm.setSendingMessage("Sending mail...");
        bm.setSendingMessageSuccess("Your message was sent successfully.");
        bm.setProcessVisibility(true);
        bm.send();

    }
}

Right click your project folder in project explorer and select properties goto android tab add the library in "is Library" section and that's it, Add Internet permission.

Sign up to request clarification or add additional context in comments.

5 Comments

so where do I have to make changes? @ Brendon Instead of mimemessage i have to use Background Mail?
Just visit tat link and download the library and extract it and import the library to your IDE and goto your project and add this Background Mail Library and use the code specified with in tat library to setup mail credentials to your project.. Clear?
Yes I have downloaded it.... But can show me how to add this? In which way? and what changes should i provide in my code?
When i am extracting it.. It is not providing any library file.. :-(
i think you dont know how to download in github, so i updated the download link, you can now click the link it will download the library in rar format, then extract it and import in eclipse. @Jay

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.