Java library based on javax.mail. Used for sending and receiving mails with gmail account.You can implement it in project by adding to gradle:
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.gof2:gmail-client:v0.0.9'
}
To begin with, this Java library architecture based on callback system .This was done to help the user react on some actions such as Error while receiving , NoInternetConnection error. This allows to keep your program in soft flow of data with no critical errors.The problem of NoInternetConnection done by using the reconnect system.
final GmailClient client = getClient().auth();
private GmailClient getClient() {
return GmailClient.get()
.loginWith(Gmail.auth("your.email@gmail.com", "yourpass"))
.beforeLogin(() -> someActionBeforeLogin())
.reconnectIfError(millis, attempts)
.onLoginError(e -> someActionOnLoginError())
.onLoginSuccess(() -> someActionOnLoginSuccess());
}
private SendedMessage yourMessage() {
return new SendedMessage("Topic", "Text message")
.from("Your FC")
.to("test.mail1@gmail.com")
.to("test.mail2@gmail.com")
.attachFiles(fileName);
}
client.send(yourMessage(), new ISender.SendCallback() {
@Override
public void onError(MessagingException e) {
yourActionOnErrorWhileSending();
}
@Override
public void onSuccess() {
yourActionOnSuccessSending();
}
});
client.receive(new IReceiver.ReceiveCallback() {
@Override
public void onReceive(Set<ReceivedMessage> messages) {
System.out.println("Received messages: " + messages
.stream()
.map(m -> m.getMessage() + " => " + m.getDate())
.collect(Collectors.joining("\n"))
);
}
@Override
public void onUpdate(ReceivedMessage message) {
System.out.println("New message: " + message.getMessage() + " => " + message.getDate());
}
@Override
public void onError(MessagingException e) {
System.out.println("Error: " + e.getMessage());
}
});
}
Receiving messages starts the thread which would work on his own(listening your mail folder).