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
Which ones
.jars
you have in your build path?– Zulian
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
– Guilherme Wayne
Go to the link: https://mvnrepository.com/artifact/javax.mail/mail/1.4.7 and add the class to the classpath.
– Claudio Lopes
Try adding this jar to your path: Activation.jar
– Zulian
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"
– Guilherme Wayne
configure: smtp.host=smtp.gmail.com smtp.port=587 smtp.ssl=yes smtp.user="[email protected]" smtp.password="myPassword"
– Claudio Lopes
or https://myaccount.google.com/lesssecureapps?pli=1
– Claudio Lopes
Still the same mistake
– Guilherme Wayne
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
– Guilherme Wayne
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
– Claudio Lopes
Try setting door 587:
email.setSmtpPort(587)
– Zulian