Here is an example to send email using Gmail Sender
package com.mail.app;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class email extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.email);
Log.i("Inside","create");
final EditText to = (EditText) findViewById(R.id.ET_Mailid);
final EditText content = (EditText) findViewById(R.id.ET_Mailmessage);
Button b = (Button) findViewById(R.id.BT_Sendmail);
// Button b1 = (Button) findViewById(R.id.button2);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String rec = to.getText().toString();
String cntnt = content.getText().toString();
boolean sent = sendMail(rec, cntnt);
if(sent)
Toast.makeText(email.this, "mail sent to \n"+rec, Toast.LENGTH_SHORT).show();
else
Toast.makeText(email.this, "mail not sent", Toast.LENGTH_SHORT).show();
}
});
}
public boolean sendMail(String reciever, String body){
try{
new GMailSender("your email id", "myapplication").sendMail("subject", ""+body, "your email id", ""+reciever, "");
// to send mail with file attached use this
// new GMailSender("pushapp53@gmail.com", "myapplication").sendMail("subject", ""+body, "pushapp53@gmail.com", ""+reciever, "/sdcard/image.jpg");
return true;
}catch(Exception e){
return false;
}
}
}
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;
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 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, String filePath) 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);
//modification starts here
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(body, "text/plain");
multipart.addBodyPart(messageBodyPart);
//File Attachment part
// if(!filePath.equals("")){
// BodyPart messageAttachmentPart = new MimeBodyPart();
// DataSource source = new FileDataSource(filePath);
// messageAttachmentPart.setDataHandler(new DataHandler(source));
// messageAttachmentPart.setFileName(filePath);
// multipart.addBodyPart(messageAttachmentPart);
// }
message.setContent(multipart );
//modification ends here
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");
}
}
}
package com.mail.app;
import java.security.AccessController;
import java.security.Provider;
@SuppressWarnings("serial")
public final class JSSEProvider extends Provider {
public JSSEProvider() {
super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
public Void run() {
put("SSLContext.TLS",
"org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
put("Alg.Alias.SSLContext.TLSv1", "TLS");
put("KeyManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
put("TrustManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
return null;
}
});
}
}
package com.mail.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MailsenderActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent inte=new Intent(MailsenderActivity.this,email.class );
startActivity(inte);
}
});
}
}
email.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#363636">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/ET_Mailid"
android:layout_width="wrap_content"
android:layout_height="25dip"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="48dp"
android:background="@drawable/et_bgnd_med"
android:ems="10"
android:inputType="textEmailAddress"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/TV_Mailid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:text="@string/sn"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/ET_Mailmessage"
android:layout_width="wrap_content"
android:layout_height="100dip"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/ET_Mailid"
android:layout_marginTop="44dp"
android:background="@drawable/et_bgnd_med"
android:ems="10"
android:inputType="textMultiLine" />
<TextView
android:id="@+id/TV_MailMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ET_Mailid"
android:layout_marginTop="17dp"
android:text="@string/cntn"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/BT_Sendmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/ET_Mailmessage"
android:layout_marginTop="52dp"
android:clickable="true"
android:text="@string/s" />
</RelativeLayout>
</LinearLayout>
email.java
package com.mail.app;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class email extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.email);
Log.i("Inside","create");
final EditText to = (EditText) findViewById(R.id.ET_Mailid);
final EditText content = (EditText) findViewById(R.id.ET_Mailmessage);
Button b = (Button) findViewById(R.id.BT_Sendmail);
// Button b1 = (Button) findViewById(R.id.button2);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String rec = to.getText().toString();
String cntnt = content.getText().toString();
boolean sent = sendMail(rec, cntnt);
if(sent)
Toast.makeText(email.this, "mail sent to \n"+rec, Toast.LENGTH_SHORT).show();
else
Toast.makeText(email.this, "mail not sent", Toast.LENGTH_SHORT).show();
}
});
}
public boolean sendMail(String reciever, String body){
try{
new GMailSender("your email id", "myapplication").sendMail("subject", ""+body, "your email id", ""+reciever, "");
// to send mail with file attached use this
// new GMailSender("pushapp53@gmail.com", "myapplication").sendMail("subject", ""+body, "pushapp53@gmail.com", ""+reciever, "/sdcard/image.jpg");
return true;
}catch(Exception e){
return false;
}
}
}
Gmailsender.java
package com.mail.app;import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;
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 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, String filePath) 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);
//modification starts here
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(body, "text/plain");
multipart.addBodyPart(messageBodyPart);
//File Attachment part
// if(!filePath.equals("")){
// BodyPart messageAttachmentPart = new MimeBodyPart();
// DataSource source = new FileDataSource(filePath);
// messageAttachmentPart.setDataHandler(new DataHandler(source));
// messageAttachmentPart.setFileName(filePath);
// multipart.addBodyPart(messageAttachmentPart);
// }
message.setContent(multipart );
//modification ends here
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");
}
}
}
JSEEProvider.java
package com.mail.app;
import java.security.AccessController;
import java.security.Provider;
@SuppressWarnings("serial")
public final class JSSEProvider extends Provider {
public JSSEProvider() {
super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
public Void run() {
put("SSLContext.TLS",
"org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
put("Alg.Alias.SSLContext.TLSv1", "TLS");
put("KeyManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
put("TrustManagerFactory.X509",
"org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
return null;
}
});
}
}
mailsenderactivity.java
package com.mail.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MailsenderActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent inte=new Intent(MailsenderActivity.this,email.class );
startActivity(inte);
}
});
}
}
email.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#363636">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/ET_Mailid"
android:layout_width="wrap_content"
android:layout_height="25dip"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="48dp"
android:background="@drawable/et_bgnd_med"
android:ems="10"
android:inputType="textEmailAddress"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/TV_Mailid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:text="@string/sn"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/ET_Mailmessage"
android:layout_width="wrap_content"
android:layout_height="100dip"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/ET_Mailid"
android:layout_marginTop="44dp"
android:background="@drawable/et_bgnd_med"
android:ems="10"
android:inputType="textMultiLine" />
<TextView
android:id="@+id/TV_MailMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ET_Mailid"
android:layout_marginTop="17dp"
android:text="@string/cntn"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/BT_Sendmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/ET_Mailmessage"
android:layout_marginTop="52dp"
android:clickable="true"
android:text="@string/s" />
</RelativeLayout>
</LinearLayout>