Connection error send java email. Can anyone help me?

Asked

Viewed 35 times

0

I am making a program that sends email through java, using these two classes but when I run it is giving this error. Can anyone help me? Error: Exception in thread "AWT-Eventqueue-0" java.lang.Runtimeexception: com.sun.mail.util.Mailconnectexception: Couldn’t connect to host, port: smtp.gmail.com, 587; timeout -1; nested Exception is: java.net.Connectexception: Connection refused: connect

package javaapplication3;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MailApp extends JFrame
{
    Container cp;
    GridLayout gl;
    JPanel pl;

    JLabel mail_label;
    JTextField mail_text;

    JLabel sub_label;
    JTextField sub_text;

    JLabel msg_label;
    JTextArea msg_text;

    JButton send_button;

    public static void main(String[] args) 
    {
        MailApp app = new MailApp();
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.setSize(800,400);
        app.setVisible(true);        
    }

    public MailApp()
    {
        super("TechWorld3g - Send email using java");

        ButtonHandler handler_button=new ButtonHandler();

        cp=getContentPane();
        gl=new GridLayout(0,4);
        cp.setLayout(gl);
        pl=new JPanel();

        mail_label= new JLabel("Send to : ");
        mail_label.setPreferredSize(new Dimension(100,20));

        mail_text= new JTextField("...");
        mail_text.setPreferredSize(new Dimension(150,20));

        sub_label= new JLabel("Subject : ");
        sub_label.setPreferredSize(new Dimension(100,20));

        sub_text= new JTextField("...");
        sub_text.setPreferredSize(new Dimension(150,20));

        msg_label= new JLabel("Message : ");
        msg_label.setPreferredSize(new Dimension(100,20));

        JScrollPane scroll = new JScrollPane (msg_text);
        scroll
            .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scroll
        .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);  

        msg_text= new JTextArea("...",10,15);

        scroll.setViewportView(msg_text);

        send_button= new JButton("Send");
        send_button.setPreferredSize(new Dimension(150,30));
        send_button.addActionListener(handler_button);

        pl.add(mail_label);
        pl.add(mail_text);
        pl.add(sub_label);
        pl.add(sub_text);
        pl.add(msg_label);
        pl.add(scroll);
        pl.add(send_button);

        cp.add(pl);
    }

    class ButtonHandler implements ActionListener  
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource()==send_button)
            {
                String to = mail_text.getText();
                String subject = sub_text.getText();
                String message =  msg_text.getText();

                String user = "[email protected]";
                String pass "senha";

                SendMail.send(to,subject, message, user, pass);
            }           
        }
    }  
}

.

package javaapplication3;

import java.util.Properties;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.PasswordAuthentication;
import javax.mail.internet.MimeMessage;
import javax.swing.JOptionPane;

public class SendMail 
{
    public static void send(String to, String sub,String msg, final String user, final String pass) 
    {
        Properties props = new Properties();

        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587"); 
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        Session session = Session.getDefaultInstance(props,new Authenticator() 
        {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() 
            {
                return new PasswordAuthentication(user, pass);
            }
        });

        try 
        {
            Message message = new MimeMessage(session);

            message.setFrom(new InternetAddress(user));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject(sub);
            message.setText(msg);

            Transport.send(message);

            JOptionPane.showMessageDialog(null,"Email sended!");

        } catch (MessagingException e) 
        {
            JOptionPane.showMessageDialog(null,"Something happened!");

            throw new RuntimeException(e);
        }

    }
}

1 answer

0


Apparently the problem is that the connection port is wrong, besides this the properties of the socketFactory not configured, use the following settings:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.