You can use this code MailSender.java
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
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 android.os.Environment;
import android.text.Html;
import android.util.Log;
public class MailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String password;
private Session session;
String to="";
String sub="";
String msg="";
String file1="";
String file2="";
String user="";
String pass="";
public MailSender(String to, String sub, String msg,String file1, String file2,String user, String pass) {
this.to=to;
this.sub=sub;
this.msg=msg;
this.file1=file1;
this.file2=file2;
this.user=user;
this.pass=pass;
// TODO Auto-generated constructor stub
}
public MailSender(String user, String pass)
{
this.user=user;
this.pass=pass;
// TODO Auto-generated constructor stub
}
public synchronized void sendMail() {
File mediaStorageDir = Environment.getExternalStorageDirectory();
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
if(user.trim().length()!=0 || pass.trim().length()!=0)
{
Session s1 = Session.getInstance(props,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,pass);//change accordingly
}
});
try {
MimeMessage message = new MimeMessage(s1);
InternetAddress fromAddress=new InternetAddress(user,"set your from text");
message.setFrom(fromAddress);//change accordingly
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(sub);
// message.setContent("<h1>This is actual message</h1>","text/html" );
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setContent(msg,"text/html");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart1);
MimeBodyPart messageBodyPart2 = null;
MimeBodyPart messageBodyPart3= null;
if(file1.trim().length()>0)
{
messageBodyPart2 = new MimeBodyPart();
String filename =file1;//change accordingly
DataSource source = new FileDataSource(filename);
messageBodyPart2.setDataHandler(new DataHandler(source));
messageBodyPart2.setFileName(filename);
multipart.addBodyPart(messageBodyPart2);
}
if(file2.trim().length()>0)
{
messageBodyPart3 = new MimeBodyPart();
String filename =file2;//change accordingly
DataSource source = new FileDataSource(filename);
messageBodyPart3.setDataHandler(new DataHandler(source));
messageBodyPart3.setFileName(filename);
multipart.addBodyPart(messageBodyPart3);
}
message.setContent(multipart );
Transport.send(message);
//Log.i("Status", "Message Sent Successfully");
} catch (MessagingException e)
{
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
//Log.i("mail", user+pass);
}
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
}
In your onclick method call asyntask like
RetrieveFeedTask eft=new RetrieveFeedTask(receiver_email,sub,header,msg,"");
eft.execute();
Add RetrieveFeedTask1 as inner class in your MainActivity and replace values of username and password
class RetrieveFeedTask extends AsyncTask<String, Void, Void> {
private Exception exception;
String to="";
String sub="hii";
String msg="";
String file1="";
String file2="";
String header="";
String password="";
// String user="";
SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
String user="from@gmail.com";//your sender email id
String pass="password";//sender password
// String reversepass = new StringBuffer(pass).reverse().toString();
//String text = new String(pass, "UTF-8");
public RetrieveFeedTask(String to, String sub, String header,String msg,String file1) {
this.to=to;
this.sub=sub;
this.header=header;
this.msg=msg;
this.file1=file1;
// TODO Auto-generated constructor stub
}
public RetrieveFeedTask(String to, String sub, String header,String msg, String file1, String file2) {
this.to=to;
this.sub=sub;
this.header=header;
this.msg=msg;
this.file1=file1;
this.file2=file2;
// TODO Auto-generated constructor stub
}
protected Void doInBackground(String... urls) {
MailSender ms=new MailSender(to,sub,header+msg,file1,file2,user,pass);
ms.sendMail();
//ms.sendStastical(stastical_email, sub, header+msg);
return null;
}
@Override
protected void onPostExecute(Void result) {
// Log.d("postexecute", "p execute");
// pDialog.dismiss();
Toast.makeText(getApplicationContext(), "Mail Sent Successfully", Toast.LENGTH_LONG ).show();
File newfile=new File(file1);
newfile.delete();
File newfile1=new File(file2);
newfile1.delete();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(getApplicationContext(), "Please wait while sending email", Toast.LENGTH_LONG).show();
}
}