Rocky Balboa
Rocky Balboa

Reputation: 814

Sending mail in android without Intent

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.

Upvotes: 1

Views: 723

Answers (1)

Brendon
Brendon

Reputation: 1368

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.

Upvotes: 1

Related Questions