Sending Email in Android using JavaMail API without using the default/built-in app

0 votes

I am trying to create a mail sending application in Android.

If I use:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

This will launch the built-in Android application; I'm trying to send the mail on button click directly without using this application.

Dec 4, 2018 in Java by Sushmita
• 6,840 points

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Send e-mail in Android using the JavaMail API using Gmail authentication.

Steps to create a sample Project:

public class MailSenderActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {

        final Button send = (Button) this.findViewById(;
        send.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                try {   
                    GMailSender sender = new GMailSender("", "password");
                    sender.sendMail("This is Subject",   
                            "This is Body",   
                } catch (Exception e) {   
                    Log.e("SendMail", e.getMessage(), e);   



public class GMailSender extends javax.mail.Authenticator {   
    private String mailhost = "";   
    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("", 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.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 {   
        MimeMessage message = new MimeMessage(session);   
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));   
        message.setSender(new InternetAddress(sender));   
        if (recipients.indexOf(',') > 0)   
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
        }catch(Exception e){


    public class ByteArrayDataSource implements DataSource {   
        private byte[] data;   
        private String type;   

        public ByteArrayDataSource(byte[] data, String type) {   
   = data;   
            this.type = type;   

        public ByteArrayDataSource(byte[] data) {   
   = data;   

        public void setType(String type) {   
            this.type = type;   

        public String getContentType() {   
            if (type == null)   
                return "application/octet-stream";   
                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");   

answered Dec 4, 2018 by Daisy
• 8,040 points

Related Questions In Java

0 votes
1 answer

To use JavaMail API for sending mails in Android without using the default/built-in App

ADD 3 jars found in the following ...READ MORE

answered Jun 6, 2018 in Java by sridhar
• 160 points
0 votes
1 answer

What are the Foreign key constraints in Android using SQLite

You can use, Foreign key constraints with ...READ MORE

answered Aug 24, 2018 in Java by geek.erkami
• 2,320 points
0 votes
1 answer

How can we upload the files using JSP/ Servlets in Java?

<form action="upload" method="post" enctype="multipart/form-data"> ...READ MORE

answered Jun 8, 2018 in Java by Sushmita
• 6,840 points
0 votes
1 answer

How to call a method after a delay in Android using Java?

final Handler handler = new Handler(); handler.postDelayed(new Runnable() ...READ MORE

answered Jun 11, 2018 in Java by Akrati
• 3,150 points
0 votes
1 answer

How can we get the current location in Android?

First you need to define a LocationListener to handle ...READ MORE

answered Sep 25, 2018 in Java by parth295
• 4,600 points
0 votes
1 answer

Can't find class CognitoUserPoolsSignInProvider: Issue with Sign In integration

CognitoUserPoolsSignInProvider is ditributed as part of aws-android-sdk-auth-userpools library. Please import ...READ MORE

answered Sep 28, 2018 in AWS by Priyaj
• 56,140 points
0 votes
1 answer

How to Read/Write String from a File in Android

Writing a File in android: private void writeToFile(String ...READ MORE

answered Oct 3, 2018 in Java by sharth
• 3,310 points
0 votes
1 answer

simple HTTP server in Java using only Java SE API

The solution is not portable across JREs. Its ...READ MORE

answered Jan 4 in Java by Daisy
• 8,040 points
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.