Send mail via Gmail from Java

3

This module sends mail only authenticated in Hotmail, how do I authenticate in the same way in Gmail and Yahoo? ..... Try adding the line: props.setProperty ("mail.smtp.host", "smtp. gmail.com "), but does not authenticate via Gmail.

Properties props = new Properties();
            props.setProperty("mail.smtp.host", "smtp.live.com");
            //props.setProperty("mail.smtp.host", "smtp.gmail.com");
            props.setProperty("mail.smtp.starttls.enable", "true");
            props.setProperty("mail.smtp.port", "587");
            props.setProperty("mail.smtp.auth", "true");
            Session session = Session.getDefaultInstance(props);
            session.setDebug(false);
            BodyPart texto = new MimeBodyPart();
    
asked by César Landaeta 31.01.2017 в 14:49
source

2 answers

4

For Gmail you must first modify the access to the account for less secure apps, so follow these steps:

  • Go to the less secure apps section in your account
  • Where it says "Acceso de aplicaciones menos seguras" click on Activar .
  •   

    It does not hurt to remind you that this would make your Gmail account   unsafe from now on so use this with caution.

        
    answered by 31.01.2017 в 15:04
    4

    Using Java mail API 1.4.7 the following code works. The problem is that Google now requires two-step authentication.

    public class Mail {
    
    
        public static void main(String[] args) {
    
            Mail mail = new Mail("[email protected]", "....");
            mail.enviaStartTLS("[email protected]", "[email protected]", "test", "startTLSTest");
    
        }
    
        private String usuario;
        private String pass;
    
        public Mail(String usuario, String pass){
            this.usuario=usuario;
            this.pass=pass;
        }
    
        public void enviaStartTLS(String from, String to, String subject, String text){
            final String username = usuario;
            final String password = pass;
    
            Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "587");
    
            Session session = Session.getInstance(props,
              new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
              });
    
            try {
    
                Message message = new MimeMessage(session);
                message.setFrom(new InternetAddress(from));
                message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(to));
                message.setSubject(subject);
                message.setText(text);
    
                Transport.send(message);
    
                System.out.println("Enviado");
    
            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }
        }
    
    
    }
    

    As I receive negative votes for a correct, safe and proven solution without any indication because, or a question in the wedding that did not work, apparently I have to elaborate a little more:

    If you are referring to the use of a API , that implies having an implementation of the API in the build path . In the specific case that refers to javaee.jar and mail-1.4.7.jar . It is recommended to use the search engine of your confidence to download the correct libraries for reproduction.

    If you have an electrical problem, do you solve it by bridging the fuse with a nail? I have done it, but in desperate cases, only in the short term and knowing the risks.

        
    answered by 31.01.2017 в 15:10