Android: Send mails not intent!

how to send Email from the android application without using intent in Rx-isstyle!

You can just crash into my repo and see the code for the app to skip big ass explanation and check the code and figure out how the things are working.

Or

Welcome to the post. Follow along and enjoy!

In this project, I have used Java-Mail-API which is used to create-read-write emails. we would be consuming and packages to send an email to an admin account.

Project Setup :

Grab a copy of main.jar, activation.jar, additional.jar from https://code.google.com/archive/p/javamail-android/downloads

Since we would make use of reactive extensions, get latest libraries from https://github.com/ReactiveX/RxAndroid

io.reactivex.rxjava2:rxandroid:2.1.1
io.reactivex.rxjava2:rxjava:2.2.8

adding files to project :

For .jar dependencies

  1. Switch to project view.
  2. Expand to app folder and paste your into libs folder.
  3. Open project level gradle build and the following file dependencies:
// paste in dependencies of project level gradle build file
implementation files('libs/mail.jar')
implementation files('libs/activation.jar')
implementation files('libs/additional.jar')
or //but for some reason this wasn't including mail.jar (above one was working fine for me)
implementation fileTree(dir: 'libs', include: ['*.jar'])

For dependencies paste the Rx libraries in the same, project level gradle build file.

implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
implementation "io.reactivex.rxjava2:rxjava:2.2.8"

Implementing the Black-Mailer…

Java Mail API uses Four objects to send a mail,

  1. Properties: this object configured the email host and the port.
  2. Session: creates a session using properties and require username and password.
  3. Message: is type where you compose your email.
  4. Transport: Use to send the message.

Step 1

Make a configuration/util/constant storing class to store email and password of the admin.

object Config {    
val EMAIL = "YourADminEmail"
val PASSWORD = "YpurAdminPassword"
}

Step 2

Creating our Completable observer which handles mail sending mechanism.

In this demo, I will be using as my mailing server.

Step 3

Consuming our completable.

Mailer.sendMail("EmailTo@email.com","test-mail-subject",               "dummy test from android app")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ Toast.makeText(this@EmailActivity, "Mail send check e-mail", Toast.LENGTH_SHORT).show()},
{ Timber.e(it) // or log.e("BlackMailin",e)}
)

That’s All Folks!

P.s. checkout my blogger, maybe you discover something new!

Bonus:

NOTE: if you will use google email as admin you need to enable permission for security lesser-secure apps from the settings of the admin Gmail. Else Gmail will block the access and you will get authorization exception.

Here is the shortcut to enable that setting. (Make sure your login with admin email) https://www.google.com/settings/security/lesssecureapps

Coding enthusiast! love Android #kotlinAlltheWay, want to explore all opportunity around it! CodingMantra: #cleanCoder #TDD #SOLID #designpatterns

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store