hello i have problem with sending emails i have good SMTP and port but the message cant be sendend gives me this error:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

and here is the code:

final String username = "stefanrafaa@gmail.com";
        final String password = "password";// i use my own ...
        Properties props = System.getProperties();
        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", "465");
        Session session;
        session = Session.getInstance(props,
                new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        try {
            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);
            // Set From: header field of the header.
            message.setFrom(new InternetAddress("stefanrafaa@gmail.com"));
            // Set To: header field of the header.
            message.setRecipient(Message.RecipientType.TO, 
                  new InternetAddress("stefanrafa0@gmail.com"));
            //subject message
            message.setSubject("Help Needed");
            // Now set the actual message
            message.setText("I need help please help me with this program.");
            message.setContent("<h:body style=background-color :white;font-family:verdana;color:#0066CC;>"
                    + "If you are seeing this its OK !!!<br/><br/>"
                    + "</body>", "text/html; charset=utf-8");
            // Send message
            Transport.send(message);
            System.out.println("Sent message successfully...");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
            //System.out.println("MessagingException: "+mex.getMessage());
            //JOptionPane.showMessageDialog(null, "[!]Cant connect to the database.");
        }

thanks

Edited 3 Years Ago by StefanRafa0

with that gives me this error:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: com.sun.mail.smtp.SMTPSendFailedException: 550 No SMTP server defined. Use real server address instead of 127.0.0.1 in your account.

:/

ehm ... I assume you know what "127.0.0.1" is? unless you have such a server running on the machine on which you run the application (which is very unlikely) you'll need to provide another server address.

how to proveide new server address and also does if i try it out on other machine will work ???

not unless you have that kind of server installed there.
127.0.0.1 is the ipAddress of localhost, the computer you're working on.
well, look into the code where that ip address is entered and replace it by a valid one.

i have also other JFrame's for this project and this is just one of them ...

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package passwordsaver1;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

/**
 *
 * @author EXTREME
 */
public final class Email extends javax.swing.JFrame {

    void centreWindow() {
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((dimension.getWidth() - getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - getHeight()) / 2);
        setLocation(x, y);
    }

    /**
     * Creates new form Email
     */
    public Email() {
        initComponents();
        centreWindow();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("To: stefanrafaa@gmail.com");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("SEND");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 121, Short.MAX_VALUE))
                    .addComponent(jScrollPane1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        final String username = "stefanrafaa@gmail.com";
        final String password = "my password";
        Properties props = System.getProperties();
        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 = Session.getInstance(props,
                new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        try {
            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);
            // Set From: header field of the header.
            message.setFrom(new InternetAddress("stefanrafa0@gmail.com"));
            // Set To: header field of the header.
            message.setRecipient(Message.RecipientType.TO,
                    new InternetAddress("stefanrafaa@gmail.com"));
            //subject message
            message.setSubject("Help Needed");
            // Now set the actual message
            message.setText("I need help please help me with this program");
            message.setContent("<h:body style=background-color :white;font-family:verdana;color:#0066CC;>"
                    + "If you are seeing this its OK !!!<br/><br/>"
                    + "</body>", "text/html; charset=utf-8");
            // Send message
            Transport.send(message);
            System.out.println("Sent message successfully...");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
            //System.out.println("MessagingException: "+mex.getMessage());
            //JOptionPane.showMessageDialog(null, "[!]Cant connect to the database.");
        }
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Email.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Email.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Email.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Email.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Email().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration                   
}

ALL ... !

Edited 3 Years Ago by StefanRafa0

This article has been dead for over six months. Start a new discussion instead.