Skip to content

Java library based on javax.mail.Used for easier sending and receiving mails with gmail account.

License

Notifications You must be signed in to change notification settings

GOF2/gmail-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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("[email protected]", "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("[email protected]")
                .to("[email protected]")
                .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).

About

Java library based on javax.mail.Used for easier sending and receiving mails with gmail account.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages