Sadaf Sadaf - 11 months ago 108
Java Question

Sending Email using Gmail API in java

I want to write a Java program that can send emails to any Gmail Accounts.
I am searching API's for this purpose. Till now I have found Gmail API useful. I also I've created APP on Google's Developer Console. I could not find any example of Oauth Authorization and email sending of Gmail API. Can anyone refer any material or link?


try JavaMail API to use this you will need two dependencies :javaee.jar and mail.jar

Properties props = new Properties();
    props.put("", "");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session = Session.getDefaultInstance(props,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("username","password");

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(""));
        message.setSubject("Testing Subject");
        message.setText("Dear Mail Crawler," +
                "\n\n No spam to my email, please!");



    } catch (MessagingException e) {
        throw new RuntimeException(e);