Skip to content

Latest commit

 

History

History
78 lines (69 loc) · 2.97 KB

File metadata and controls

78 lines (69 loc) · 2.97 KB

gmail-client

SonarCloud Build Status

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.

So, how to start using it?

1.Account authentication

    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());
    }

2.Create and send message

 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();
            }
 });

3.Receive messages

 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).