Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29935

Email without Intent in android

$
0
0
    I am new to android. And i want to send OTP to users email can anyone help?..    package com.example.emailtutorial;    import androidx.appcompat.app.AppCompatActivity;    import android.content.Context;    import android.content.Intent;    import android.net.ConnectivityManager;    import android.net.NetworkInfo;    import android.net.Uri;    import android.os.Bundle;    import android.util.Log;    import android.view.View;    import android.widget.Button;    import android.widget.EditText;    import android.widget.TextView;    import android.widget.Toast;    import java.net.PasswordAuthentication;    import java.util.Properties;    public class MainActivity extends AppCompatActivity {       // TextView emailTo,subject;        EditText mailTo,subjectEtxt,body;        Button btnSend;        @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.activity_main);            mailTo=findViewById(R.id.mailToEtxt);            subjectEtxt=findViewById(R.id.subjectEtxt);            body=findViewById(R.id.mailBody);            btnSend=findViewById(R.id.btnSend);            btnSend.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    if(isOnline())                    {    sendMail();                    }                }            });        }        public boolean isOnline() {            ConnectivityManager cm =                    (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);            NetworkInfo netInfo = cm.getActiveNetworkInfo();            if (netInfo != null && netInfo.isConnectedOrConnecting()) {                return true;            }            return false;        }        public void sendMail()        {            final String username = "username@gmail.com";            final String password = "password";            Properties props = new Properties();            props.put("mail.smtp.auth", "true");            props.put("mail.smtp.starttls.enable", "true");            props.put("mail.smtp.host", "smtp.gmail.com");            props.put("mail.smtp.port", "587");            Session session = Session.getInstance(props,                    new javax.mail.Authenticator() {                        protected PasswordAuthentication getPasswordAuthentication() {                            return new PasswordAuthentication(username, password);                        }                    });            try {                Message message = new MimeMessage(session);                message.setFrom(new InternetAddress("from-email@gmail.com"));                message.setRecipients(Message.RecipientType.TO,                        InternetAddress.parse("to-email@gmail.com"));                message.setSubject("Testing Subject");                message.setText("Dear Mail Crawler,"+"\n\n No spam to my email, please!");                MimeBodyPart messageBodyPart = new MimeBodyPart();                Multipart multipart = new MimeMultipart();                messageBodyPart = new MimeBodyPart();                String file = "path of file to be attached";                String fileName = "attachmentName"                DataSource source = new FileDataSource(file);                messageBodyPart.setDataHandler(new DataHandler(source));                messageBodyPart.setFileName(fileName);                multipart.addBodyPart(messageBodyPart);                message.setContent(multipart);                Transport.send(message);                System.out.println("Done");            }     catch (MessagingException e) {                throw new RuntimeException(e);            }        }    }This is the above code 

i have created 2 functions first to check the user has active internet connection in his/her device,which is done by isOnline method in the code.

then the crucial part sending the mail code. which is performed by sendMail function

can anyone suggest any changes and solve dependency issue for the above code in sendMail function

but I am not sure about the dependency check and permissions in new android studio. I am relatively new to android


Viewing all articles
Browse latest Browse all 29935

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>