Erro JavaMail

6 respostas
M

Boa noite pessoal.
Estou tentando desenvolver uma classe para envio de e-mail. Porém o mesmo esta retornando o seguinte erro.
Eu testei para servidores não autenticados e funcionou, mas para o gmail não, pois o mesmo é autenticado. MAs eu estou passando o usuário e a senha e mesmo assim dá esse erro
Alguem pode me ajudar ?

530 5.7.0 Must issue a STARTTLS command first h19sm3080504wxd
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first h19sm3080504wxd

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
at jm.javamail.JMEnvioAutenticado.main(JMEnvioAutenticado.java:25)
QUIT
221 2.0.0 mx.gmail.com closing connection h19sm3080504wxd
ERRO =530 5.7.0 Must issue a STARTTLS command first h19sm3080504wxd

abaixo está o código.

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class JMEnvioAutenticado {
  public static void main(String[] args) {
    Properties props = new Properties();
		
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.auth", "true");
    Session session = Session.getInstance(props, new JMAuthenticator());

    session.setDebug(true);
    Message msg = new MimeMessage(session);
    try {
      msg.setRecipient(Message.RecipientType.TO, 
                 new InternetAddress("[email removido]"));
      msg.setFrom(new InternetAddress("[email removido]"));
      msg.setSubject("Teste da JavaMail GMAIL");
      msg.setText("Ola, como vai Gmailç?");

      // Envia mensagem.
      Transport.send(msg);
    } catch (MessagingException e) {
      System.out.println("ERRO ="+e.getMessage());
    }
  }	
}
package jm.javamail;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.mail.*; 

public class JMAuthenticator extends Authenticator {
        public String username = null;
        public String password = null;

 /*       public JMAuthenticator(String user, String pwd) {
            username = user;
            password = pwd;
        }

        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username,password);
        }*/
        
   public PasswordAuthentication getPasswordAuthentication(){
    String usuario = null, senha = null;

    // Lê informações de usuário e senha.
    try {
      BufferedReader in = 
          new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Digite usuario: ");
      usuario = in.readLine();
      System.out.println("Digite senha: ");
      senha = in.readLine();
    }
    catch (IOException e) { 
     System.out.println("ERRO Autenticação = "+e.getMessage());
    }
	    
    // Cria PasswordAuthentication.
    return new PasswordAuthentication(usuario, senha);
  }
    }

6 Respostas

R

Uma coisa que percebi é que você não definiu a porta do smtp. Veja meu exemplo:

import java.security.Security;
import java.util.Properties;

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 GmailTest {

	private static final String SMTP_HOSTNAME = "smtp.gmail.com";
	private static final String SMTP_PORT = "465";
	private static final String emailMsgTxt = "Teste de envio JavaMail/Gmail";
	private static final String emailSubjectTxt = "JavaMail GmailTest";
	private static final String emailFromAddress = "[email removido]";
	private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
	private static final String[] sendTo = { "[email removido]" };

	public static void main(String args[]) throws Exception {

		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

		new GmailTest().sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt,
				emailFromAddress);
		System.out.println("Sucessfully Sent mail to All Users");
	}

	public void sendSSLMessage(String recipients[], String subject,
			String message, String from) throws MessagingException {

		boolean debug = true;

		Properties props = new Properties();
		props.put("mail.smtp.host", SMTP_HOSTNAME);
		props.put("mail.smtp.auth", "true");
		props.put("mail.debug", "true");
		props.put("mail.smtp.port", SMTP_PORT);
		props.put("mail.smtp.socketFactory.port", SMTP_PORT);
		props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.put("mail.smtp.socketFactory.fallback", "false");

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {

					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication("USUARIO",
								"SENHA");
					}
				});

		session.setDebug(debug);

		Message msg = new MimeMessage(session);
		InternetAddress addressFrom = new InternetAddress(from);
		msg.setFrom(addressFrom);

		InternetAddress[] addressTo = new InternetAddress[recipients.length];
		for (int i = 0; i < recipients.length; i++) {
			addressTo[i] = new InternetAddress(recipients[i]);
		}
		msg.setRecipients(Message.RecipientType.TO, addressTo);

		// Setting the Subject and Content Type
		msg.setSubject(subject);
		msg.setContent(message, "text/plain");
		Transport.send(msg);
	}
}
M

Realmente eu não passei a porta. Porém o erro que dá, não me parece que ele esteja reclamando da porta. E outra eu testei para outros sevidores de email e não tive problema.

M

O Rangel, eu testei sua classe aqui e ela funcionou. Eu creio que deva ser o erro na autenticação.

Valeu pela força…

V

Must issue a STARTTLS
tb to com o mesmo erro alguem sabe o que é?

D

Ops…Mgem postada no topico errado…

Lindberg

esse do rangelPJ

testei aqui e funcionou
depois de muitas pesquisas

Criado 17 de junho de 2006
Ultima resposta 12 de fev. de 2011
Respostas 6
Participantes 5