JavaMail Shipping problem

0

I'm having a problem sending an email from an application made in Java (netbeans). The problem is that I want to use a mail "webmail" instead of gmail (where it works) the issue is that by making the corresponding changes, the frame freezes and I can not do anything else, for quite some time. I share the code:

public void SendMail() {

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "mail.smartsim.cl");  // modificar por smtp.gmail.com
    props.put("mail.smtp.port", "465"); // puerto 587
    //props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
    //props.put("mail.smtp.starttls.enable", "true");

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

    try {

          // Define message
          MimeMessage message = new MimeMessage(session);
          message.setFrom(new InternetAddress(username));
          message.setSubject("Recuperacion de Contraseña");
          message.addRecipient(Message.RecipientType.TO,new InternetAddress("[email protected]"));
          message.setText("Nombre Usuario: " +Usuario+" " +"Email de contacto: "+ Mensaje);

          // Envia el mensaje
          Transport.send(message);

          JOptionPane.showMessageDialog(this, "Mensaje Enviado Correctamente");
    } catch (MessagingException e) {
        Logger.getLogger(Enviarcorreo.class.getName()).log(Level.SEVERE, null, e);
    }
        }
    
asked by Juan Pablo 06.11.2018 в 19:32
source

1 answer

0

Try this method. Make the appropriate modifications (smtp server, account, pass, body of the message ...)

I hope it helps you.

public void enviarMail(String destinatario, String asunto, String cuerpo) {

    String remitente = "tu cuenta"; // Para la dirección [email protected]

    Properties props = System.getProperties();
    props.put("mail.smtp.host", "smtp.gmail.com"); // El servidor SMTP de Google
    props.put("mail.smtp.user", remitente);
    props.put("mail.smtp.clave", "tu pass"); // Pass cuenta
    props.put("mail.smtp.auth", "true"); // Autenticación mediante usuario y pass
    props.put("mail.smtp.starttls.enable", "true"); // Conexió segura al servidor
    props.put("mail.smtp.port", "587"); // Puerto SMTP seguro de Google

    Session session = Session.getDefaultInstance(props);
    MimeMessage message = new MimeMessage(session);

    try {
        message.setFrom(new InternetAddress(remitente));
        message.addRecipients(Message.RecipientType.TO, destinatario);
        message.setSubject(asunto);
        //message.setText(cuerpo);
        message.setContent(cuerpo, "text");
        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.gmail.com", remitente, "tu pass");
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    } catch (MessagingException me) {
        me.printStackTrace();
    }
}
    
answered by 06.11.2018 в 23:43