JavaMail Shipping problem


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("", "");  // modificar por
    props.put("mail.smtp.port", "465"); // puerto 587
    //props.put("", "");
    //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

          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

1 answer


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("", ""); // 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.setContent(cuerpo, "text");
        Transport transport = session.getTransport("smtp");
        transport.connect("", remitente, "tu pass");
        transport.sendMessage(message, message.getAllRecipients());
    } catch (MessagingException me) {
answered by 06.11.2018 в 23:43