1 year ago

#388662

test-img

vojvoda ljubinko

Java sent email

I am trying to send an email in any way, but to no avail. I get this exeption. Is there any other way to send an email or is this the only one? I can send via spring, but I need to send an email via Java with Ant.

package javaapplication5;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class SendEmailTLS {

    public static void main(String[] args) {

        final String username = "sistem.elektronske.biblioteke@gmail.com";
        final String password = "otbbxgrztdzzinxb";

        Properties prop = new Properties();
        prop.put("mail.smtp.host", "smtp.gmail.com");
        prop.put("mail.smtp.port", "587");
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.starttls.enable", "true"); //TLS
        
        Session session = Session.getInstance(prop,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from@gmail.com"));
            message.setRecipients(
                    Message.RecipientType.TO,
                    InternetAddress.parse("stojanovicljubinko01@gmail.com, breljubinko75@gmail.com")
            );
            message.setSubject("Testing Gmail TLS");
            message.setText("Dear Mail Crawler,"
                    + "\n\n Please do not spam my email!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}

Error:

An image showing the errors in the program

java

email

gmail

gmail-api

0 Answers

Your Answer

Accepted video resources