Send Email with Beautiful Form in Android | by Himanshu Verma | Towards Data Science

As we all already know that the use of E-mail has become necessary and important for all of us. There are various e-mail service providers such as Yahoo, Outlook, iCloud and Gmail, etc. These tech giants provide free email service for every single user. If you use the internet then there is no way to ignore it because whenever you visit any website/app to buy/sell products online then. First of all, you’ll have to signup on those sites/apps to prove your identity in the real world.
In this article, we are going to learn how to send e-mail in Gmail inbox to the specified email address using JavaMail API. We will send common user data such as username, email address, mobile no. etc.
So let’s get started.
First of all create android project with empty activity and create java class with the name MailSender.java and JSSEProvider.java in the root directory.
Now we will need to import the Java Mail API jar files into our project. Download the jar files from HERE, unzip it and paste it inside the libs folder.
If your project doesn’t recognize your library then restart the Android studio.
Create the assets folder as right-click on app ->New -> Folder -> Assets Folder. We will put our HTML file here to display the data on Gmail.
To send an email your app will need an internet connection, so declare this permission in your AndroidManifest.xml file.
<uses-permission android:name="android.permission.INTERNET"/>
Create some instance variables inside the MailSender.java class
// change this host name accordinglyprivate String mailhost = "webmail.xyz.in";private String user;private String password;private Session session;Context context;private Multipart _multipart = new MimeMultipart();
Now create the static block inside the MailSender.java file to call the JSSEProvider.java class. JSSEProvider is used to link the modified versions of SSLContext, KeyManagerFactory for X509, TrustManagerFactory for X509 and the AndroidCAstore.
static {    Security.addProvider(new JSSEProvider());}
Create the parameterized constructor(pass the context, “sender email id” and ”password” etc.) to initialize the instance variables and create an object of Properties class to set the properties inside the constructor. pass the context, user email id, and password in it. Take a look at the constructor code.
public MailSender(Context context,String user, String password) {    this.user = user;    this.password = password;    this.context = context;    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);}
Create the password authenticator method to authenticate your account.
protected PasswordAuthentication getPasswordAuthentication() {    return new PasswordAuthentication(user, password);}
Finally, create the user-defined method sendUserDetailWithImage() to send the data to the mail. The following piece of code picks up the user_profile.html from the assets folder, converts it into a buffer then reads it from the buffer and finally converts it into a string object to replace the keywords “$$username$$” with the actual username and so on. See the full sendUserDetailWithImage() code.
Have a look at the complete MainSender.java code
To show the data in the form, create the HTML file with name user_profile.html inside the assets folder and paste the following code.
The output of the HTML code would look something like this
Finally, paste the following code into your MainActivity.
Note: If everything is right from your side and still you don’t get emails then please check your spam folder and if the email is there, click on “mark as not spam”.
I have created an android project to send user data in the form of e-mail. I have used JavaMail API to send emails. In this article, I sent HTML form along with data.
If you have any query or doubt regarding this article, then please feel free to ask me in the comment section. Also, you can find full source code for this article on my Github account.
Subscribe my mailing list to get the early access of my articles directly in your inbox or follow my own publication on Medium The Code Monster to polish your technical knowledge.
Himanshu Verma has graduated from the Dr. APJ Abdul Kalam University, Lucknow (India). He is an Android & IOS Developer and Machine Learning & Data Science Learner, a Financial Advisor, and a Blogger.