Sample code for 30+ languages & platforms
Java

OAuth2 for a GMail using a P12 Service Account Key

See more GMail REST API Examples

Demonstrates how to use GMail with OAuth2 for a service account within a Google Workspace Account where your email domain is custom (e.g., @yourcompany.com).

Note: This example does not work for Personal Google Accounts where the email domain is @gmail.com

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.MailMan;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        // --------------------------------------------------------------------------------
        // For a step-by-step guide for setting up your Google Workspace service account,
        // see Setup Google Workspace Account for Sending SMTP GMail from a Service Account
        // --------------------------------------------------------------------------------
        // Begin by loading your Google service account key (.p12)
        Cert cert = new Cert();
        try {
            cert.loadPfxFile("c:/someDirectory/keys/chilkat25-cbd7b42afbd8.p12", "notasecret");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // The ISS is your service account email address ending in gserviceaccount.com.
        String iss = "chilkatsvc@chilkat25.iam.gserviceaccount.com";

        // The scope is always the following string:
        String scope = "https://mail.google.com/";

        // The sub is your company email address
        String oauth_sub = "bob@yourcompany.com";

        // The access token is valid for this number of seconds.
        int numSec = 3600;

        String accessToken;
        try {
            accessToken = http.gSvcOauthAccessToken(iss, scope, oauth_sub, numSec, cert);
            System.out.println("access token: " + accessToken);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // The access token allows us to send unlimited emails while it's valid. Once it expires, we must obtain and use a new one.

        // -----------------------------------------------------------------------
        MailMan mailman = new MailMan();

        // Set the properties for the GMail SMTP server:
        mailman.setSmtpHost("smtp.gmail.com");
        mailman.setSmtpPort(587);
        mailman.setStartTLS(true);

        mailman.setSmtpUsername("bob@yourcompany.com");
        mailman.setOAuth2AccessToken(accessToken);

        // Create a new email object
        Email email = new Email();

        email.setSubject("This is a test");
        email.setBody("This is a test");
        email.setFrom("Bob <bob@yourcompany.com>");
        email.addTo("Recipient", "recipient@example.com");
        // To add more recipients, call AddTo, AddCC, or AddBcc once per recipient.

        try {
            mailman.sendEmail(email);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        try {
            mailman.closeSmtpConnection();
        } catch (ChilkatException e) {
            System.out.println("Connection to SMTP server not closed cleanly.");
        }

        System.out.println("Successfully sent email using Gmail with a service account key.");
    }
}