Jump to content

Erro JavaMail Gmail - Eclipse projeto Java


Recommended Posts

Olá, estou usando o Eclipse, projeto Java, e enfrentando problemas de conexão com o gmail, alguém saberia dizer o que é? já pesquisei bastante na encontrei. Usando: J2SE 1.5, javaSE 1.6, Javax.mail 1.6 e activation. Já testei as portas 587 e 465. Abaixo o código, os "*******" são as informações sensíveis do código, e abaixo o erro retornado na execução;

package mmn;

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

public class Gmailresponder {
    public static void main(String[] args) {
        
        String host = "*******";
        String username = "*******";
        String password = "*******";

        
        Properties props = new Properties();
        props.put("mail.store.protocol", "imaps");

        try {
            
            Session session = Session.getInstance(props, null);
            Store store = session.getStore();
            store.connect(host, username, password);

            
            Folder inbox = store.getFolder("inbox");
            inbox.open(Folder.READ_ONLY);

           
            Message[] messages = inbox.getMessages();
            for (Message message : messages) {
                String subject = message.getSubject();
                String sender = message.getFrom()[0].toString();

                if (is*******Email(subject)) {
                    
                    enviarResposta*******(sender);
                } else {
                    
                    enviarRespostaOutroEmail(sender);
                }
            }

            
            inbox.close(false);
            store.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    
    private static boolean is*******Email(String subject) {
        
        
        String[] palavrasChave******* = {"*******", "*******", "*******", "*******"};
        for (String palavra : palavrasChave*******) {
            if (subject.toLowerCase().contains(palavra)) {
                return true;
            }
        }
        return false;
    }

    
    private static void enviarResposta*******(String sender) {
        try {
            
            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");
            props.put("mail.smtp.ssl.protocols", "TLSv1.2");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            
            Session session = Session.getInstance(props, new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("*******", "*******");
                }
            });

            
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("*******"));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(sender));
            message.setSubject("Resposta automática: *******");
            message.setText("*******");

            
            Transport.send(message);

            System.out.println("Resposta enviada para: " + sender + " - Assunto: " + message.getSubject());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    
    private static void enviarRespostaOutroEmail(String sender) {
        try {
            
            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");
            props.put("mail.smtp.ssl.protocols", "TLSv1.2");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            
            Session session = Session.getInstance(props, new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("*******", "*******");
                }
            });

            
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("*******"));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(sender));
            message.setSubject("Resposta automática: *******");
            message.setText("*******");

            
            Transport.send(message);

            System.out.println("Resposta enviada para: " + sender + " - Assunto: " + message.getSubject());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 

 

 

Retorno:

javax.mail.MessagingException: Connection reset;
  nested exception is:
    java.net.SocketException: Connection reset
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:742)
    at javax.mail.Service.connect(Service.java:366)
    at javax.mail.Service.connect(Service.java:246)
    at mmn.GmailResponder.main(GmailResponder.java:30)
Caused by: java.net.SocketException: Connection reset
    at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:323)
    at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:350)
    at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:803)
    at java.base/java.net.Socket$SocketInputStream.read(Socket.java:966)
    at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:484)
    at java.base/sun.security.ssl.SSLSocketInputRecord.readHeader(SSLSocketInputRecord.java:478)
    at java.base/sun.security.ssl.SSLSocketInputRecord.decode(SSLSocketInputRecord.java:160)
    at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:111)
    at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1510)
    at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1425)
    at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:455)
    at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:426)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:626)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:400)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
    at com.sun.mail.iap.Protocol.<init>(Protocol.java:134)
    at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:131)
    at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:763)
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:698)
    ... 3 more

 

Edited by Bruno Evaristo
senha a mostra
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...