Error while using Simpleemail

Asked

Viewed 201 times

0

public void enviarEmail() throws EmailException{
    SimpleEmail email = new SimpleEmail();
    email.setHostName("smtp.gmail.com");//o servidor SMTP para envio do e-mail
    email.addTo("[email protected]", "teste"); //destinatário
    email.setFrom("[email protected]", "Me"); // remetente
    email.setSubject("Mensagem de Teste");// assunto do e-mail
    email.setMsg("Teste de Email utilizando commons-email"); //conteudo do e-mail
    email.send(); //envia o e-mail
}

I’m trying to make a code that sends an email and is giving the following error:

Exception in thread "main" java.lang.Noclassdeffounderror: javax/mail/Authenticator

  • Which ones .jars you have in your build path?

  • Commons-email-1.5. jar Commons-email-1.5-javadoc.jar Commons-email-1.5-sources.jar Commons-email-1.5-tests.jar Commons-email-1.5-test-sources.jar

  • Go to the link: https://mvnrepository.com/artifact/javax.mail/mail/1.4.7 and add the class to the classpath.

  • Try adding this jar to your path: Activation.jar

  • I added this one. jar and is giving this error now: "Exception in thread "main" org.apache.Commons.mail.Emailexception: Sending the email to the following server failed : smtp.gmail.com:25"

  • configure: smtp.host=smtp.gmail.com smtp.port=587 smtp.ssl=yes smtp.user="[email protected]" smtp.password="myPassword"

  • or https://myaccount.google.com/lesssecureapps?pli=1

  • Still the same mistake

  • Already added and this with the following error: Exception in thread "main" org.apache.Commons.mail.Emailexception: Sending the email to the following server failed : smtp.gmail.com:465

  • Then it is best to open a new post, because the missing class error is solved. But I don’t know why I only used Google to answer your questions

  • Try setting door 587: email.setSmtpPort(587)

Show 6 more comments

1 answer

0

1. Add the following dependencies in pom.xml:

   <dependencies>      
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.2</version>
        </dependency>   

        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>     

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.5.6</version>
        </dependency> 
    </dependencies>

2. Create a shipping method (pay attention to the comments):

   public static void sendEmail() throws EmailException {

        SimpleEmail email = new SimpleEmail();
        // Utilize o hostname do seu provedor de email
        System.out.println("alterando hostname...");
        email.setHostName("smtp.gmail.com");

        // Quando a porta utilizada não é a padrão (gmail = 465)
        email.setSmtpPort(465);

        // Adicione os destinatários
        email.addTo("[email protected]");

        // Configure o seu email do qual enviará
        email.setFrom("[email protected]", "Karan User");

        // Adicione um assunto
        email.setSubject("Lembrete de senha");

        // Adicione a mensagem do email
        email.setMsg("Lembrete de senha karanalpe ");

        // Para autenticar no servidor é necessário chamar os dois métodos abaixo
        System.out.println("autenticando...");
        email.setSSL(true);
        email.setAuthentication("[email protected]", "suaSenha");
        System.out.println("enviando...");
        email.send();
        System.out.println("Email enviado!");
    }

Reference: http://karanalpe.com.br/tecnologia/back-end/enviando-email-com-o-java/

Source code: https://github.com/karanalpe/envio-email

Browser other questions tagged

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