P
P
P_Alexander2018-01-29 17:47:07
Java
P_Alexander, 2018-01-29 17:47:07

How to send email from java code?

Properties p = new Properties();
            p.put("mail.smtp.host", "smtp.gmail.com");
            p.put("mail.smtp.socketFactory.port", 465);
            p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            p.put("mail.smtp.auth", "true");
            p.put("mail.smtp.port", 465);

            Session s = Session.getInstance(p,

                    new javax.mail.Authenticator(){


                        protected PasswordAuthentication getPasswordAuthentication(){

                            return new PasswordAuthentication("[email protected]", "root");

                        }


                    }
            );
            
            Message message = new MimeMessage(s);
            try {
                message.setFrom(new InternetAddress("[email protected]"));
                message.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
                message.setSubject("Тема письма");
                message.setText("Сообщение в письме");

                Transport.send(message);

                System.out.println("Письмо успешно отправлено");

            } catch (MessagingException e) {
                System.out.println("Письмо не отправилось");
                e.printStackTrace();
            }


What to do, an error occurs when compiling, as I understand that this is Google blocking, when I went to my mail, a letter from Google - we blocked unauthorized access to the mail!
how to solve it? maybe somewhere in the settings to climb?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Alexandrov, 2018-01-29
@P_Alexander

Google 2 factor authentication.

A
Alexander Oparin, 2018-01-29
@losse_narmo

when compiling
google doesn't really matter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question