Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3487068
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:06:24+00:00 2026-05-18T11:06:24+00:00

I have a textfield on my app, and a button. I only want that

  • 0

I have a textfield on my app, and a button. I only want that when user
press the button, my app have to send a email with the text “Hello” to
the direction on the textfield.

Is there a easy way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:06:24+00:00Added an answer on May 18, 2026 at 11:06 am

    First way.
    If you don’t want to be linked to the native email program or gmail program (via intent) to send the mail, but have the email sent in the background, see the code below.

    Firstly you need to import the following library in Gradle File:

    implementation 'javax.mail:mail:1.4.7'
    

    Then you can use this helper class and adjust it to your needs.

    package com.myapp.android.model.service;
    
    import android.util.Log;
    import com.myapp.android.MyApp;
    
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Properties;
    
    import javax.activation.DataHandler;
    import javax.mail.Authenticator;
    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.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMultipart;
    
    public class MailService {
        // public static final String MAIL_SERVER = "localhost";
    
        private String toList;
        private String ccList;
        private String bccList;
        private String subject;
        final private static String SMTP_SERVER = DataService
                .getSetting(DataService.SETTING_SMTP_SERVER);
        private String from;
        private String txtBody;
        private String htmlBody;
        private String replyToList;
        private ArrayList<Attachment> attachments;
        private boolean authenticationRequired = false;
    
        public MailService(String from, String toList, String subject, String txtBody, String htmlBody,
                Attachment attachment) {
            this.txtBody = txtBody;
            this.htmlBody = htmlBody;
            this.subject = subject;
            this.from = from;
            this.toList = toList;
            this.ccList = null;
            this.bccList = null;
            this.replyToList = null;
            this.authenticationRequired = true;
    
            this.attachments = new ArrayList<Attachment>();
            if (attachment != null) {
                this.attachments.add(attachment);
            }
        }
    
        public MailService(String from, String toList, String subject, String txtBody, String htmlBody,
                ArrayList<Attachment> attachments) {
            this.txtBody = txtBody;
            this.htmlBody = htmlBody;
            this.subject = subject;
            this.from = from;
            this.toList = toList;
            this.ccList = null;
            this.bccList = null;
            this.replyToList = null;
            this.authenticationRequired = true;
            this.attachments = attachments == null ? new ArrayList<Attachment>()
                    : attachments;
        }
    
        public void sendAuthenticated() throws AddressException, MessagingException {
            authenticationRequired = true;
            send();
        }
    
        /**
         * Send an e-mail
         *
         * @throws MessagingException
         * @throws AddressException
         */
        public void send() throws AddressException, MessagingException {
            Properties props = new Properties();
    
            // set the host smtp address
            props.put("mail.smtp.host", SMTP_SERVER);
            props.put("mail.user", from);
    
            props.put("mail.smtp.starttls.enable", "true");  // needed for gmail
            props.put("mail.smtp.auth", "true"); // needed for gmail
            props.put("mail.smtp.port", "587");  // gmail smtp port
    
            /*Authenticator auth = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("mobile@mydomain.example", "mypassword");
                }
            };*/
    
            Session session;
    
            if (authenticationRequired) {
                Authenticator auth = new SMTPAuthenticator();
                props.put("mail.smtp.auth", "true");
                session = Session.getDefaultInstance(props, auth);
            } else {
                session = Session.getDefaultInstance(props, null);
            }
    
            // get the default session
            session.setDebug(true);
    
            // create message
            Message msg = new javax.mail.internet.MimeMessage(session);
    
            // set from and to address
            try {
                msg.setFrom(new InternetAddress(from, from));
                msg.setReplyTo(new InternetAddress[]{new InternetAddress(from,from)});
            } catch (Exception e) {
                msg.setFrom(new InternetAddress(from));
                msg.setReplyTo(new InternetAddress[]{new InternetAddress(from)});
            }
    
            // set send date
            msg.setSentDate(Calendar.getInstance().getTime());
    
            // parse the recipients TO address
            java.util.StringTokenizer st = new java.util.StringTokenizer(toList, ",");
            int numberOfRecipients = st.countTokens();
    
            javax.mail.internet.InternetAddress[] addressTo = new javax.mail.internet.InternetAddress[numberOfRecipients];
    
            int i = 0;
            while (st.hasMoreTokens()) {
                addressTo[i++] = new javax.mail.internet.InternetAddress(st
                        .nextToken());
            }
            msg.setRecipients(javax.mail.Message.RecipientType.TO, addressTo);
    
            // parse the replyTo addresses
            if (replyToList != null && !"".equals(replyToList)) {
                st = new java.util.StringTokenizer(replyToList, ",");
                int numberOfReplyTos = st.countTokens();
                javax.mail.internet.InternetAddress[] addressReplyTo = new javax.mail.internet.InternetAddress[numberOfReplyTos];
                i = 0;
                while (st.hasMoreTokens()) {
                    addressReplyTo[i++] = new javax.mail.internet.InternetAddress(
                            st.nextToken());
                }
                msg.setReplyTo(addressReplyTo);
            }
    
            // parse the recipients CC address
            if (ccList != null && !"".equals(ccList)) {
                st = new java.util.StringTokenizer(ccList, ",");
                int numberOfCCRecipients = st.countTokens();
    
                javax.mail.internet.InternetAddress[] addressCC = new javax.mail.internet.InternetAddress[numberOfCCRecipients];
    
                i = 0;
                while (st.hasMoreTokens()) {
                    addressCC[i++] = new javax.mail.internet.InternetAddress(st
                            .nextToken());
                }
    
                msg.setRecipients(javax.mail.Message.RecipientType.CC, addressCC);
            }
    
            // parse the recipients BCC address
            if (bccList != null && !"".equals(bccList)) {
                st = new java.util.StringTokenizer(bccList, ",");
                int numberOfBCCRecipients = st.countTokens();
    
                javax.mail.internet.InternetAddress[] addressBCC = new javax.mail.internet.InternetAddress[numberOfBCCRecipients];
    
                i = 0;
                while (st.hasMoreTokens()) {
                    addressBCC[i++] = new javax.mail.internet.InternetAddress(st
                            .nextToken());
                }
    
                msg.setRecipients(javax.mail.Message.RecipientType.BCC, addressBCC);
            }
    
            // set header
            msg.addHeader("X-Mailer", "MyAppMailer");
            msg.addHeader("Precedence", "bulk");
            // setting the subject and content type
            msg.setSubject(subject);
    
            Multipart mp = new MimeMultipart("related");
    
            // set body message
            MimeBodyPart bodyMsg = new MimeBodyPart();
            bodyMsg.setText(txtBody, "iso-8859-1");
            if (attachments.size()>0) htmlBody = htmlBody.replaceAll("#filename#",attachments.get(0).getFilename());
            if (htmlBody.indexOf("#header#")>=0) htmlBody = htmlBody.replaceAll("#header#",attachments.get(1).getFilename());
            if (htmlBody.indexOf("#footer#")>=0) htmlBody = htmlBody.replaceAll("#footer#",attachments.get(2).getFilename());
    
            bodyMsg.setContent(htmlBody, "text/html");
            mp.addBodyPart(bodyMsg);
    
            // set attachements if any
            if (attachments != null && attachments.size() > 0) {
                for (i = 0; i < attachments.size(); i++) {
                    Attachment a = attachments.get(i);
                    BodyPart att = new MimeBodyPart();
                    att.setDataHandler(new DataHandler(a.getDataSource()));
                    att.setFileName( a.getFilename() );
                    att.setHeader("Content-ID", "<" + a.getFilename() + ">");
                    mp.addBodyPart(att);
                }
            }
            msg.setContent(mp);
    
            // send it
            try {
                javax.mail.Transport.send(msg);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        /**
         * SimpleAuthenticator is used to do simple authentication when the SMTP
         * server requires it.
         */
        private static class SMTPAuthenticator extends javax.mail.Authenticator {
    
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
    
                String username = DataService
                        .getSetting(DataService.SETTING_SMTP_USER);
                String password = DataService
                        .getSetting(DataService.SETTING_SMTP_PASSWORD);
    
                return new PasswordAuthentication(username, password);
            }
        }
    
        public String getToList() {
            return toList;
        }
    
        public void setToList(String toList) {
            this.toList = toList;
        }
    
        public String getCcList() {
            return ccList;
        }
    
        public void setCcList(String ccList) {
            this.ccList = ccList;
        }
    
        public String getBccList() {
            return bccList;
        }
    
        public void setBccList(String bccList) {
            this.bccList = bccList;
        }
    
        public String getSubject() {
            return subject;
        }
    
        public void setSubject(String subject) {
            this.subject = subject;
        }
    
        public void setFrom(String from) {
            this.from = from;
        }
    
        public void setTxtBody(String body) {
            this.txtBody = body;
        }
    
        public void setHtmlBody(String body) {
            this.htmlBody = body;
        }
    
        public String getReplyToList() {
            return replyToList;
        }
    
        public void setReplyToList(String replyToList) {
            this.replyToList = replyToList;
        }
    
        public boolean isAuthenticationRequired() {
            return authenticationRequired;
        }
    
        public void setAuthenticationRequired(boolean authenticationRequired) {
            this.authenticationRequired = authenticationRequired;
        }
    
    }
    

    And use this class:

    MailService mailer = new MailService("from@mydomain.example","to@domain.example","Subject","TextBody", "<b>HtmlBody</b>", (Attachment) null);
    try {
        mailer.sendAuthenticated();
    } catch (Exception e) {
        Log.e(AskTingTing.APP, "Failed sending email.", e);
    }
    

    Second way.
    Another option, if you don’t mind using the native email client or gmail on Android for sending the mail (but the user actually has to hit the send button finally in the email client), you can do this:

    startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:to@gmail.com")));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a textfield where the user can type what ever he want into
I have put a button over a toolbar.But when i press that button,my selector
I have an iPhone app with this screen: When the user taps a textfield,
Would this be possible? The app will have a button and a Text Field
How do I manipulate textfield variables within an iPhone app? I want to have
I have a search field in my WPF app with a search button that
In my iPhone APP I have a view with a textfield and a button
I have a flash app and a textfield in it. I can change locale
In my iPad app I have two textfields. One displays the normal, default textfield
In my view i have Textfield.If i move that TextField it should open iphone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.