Problem sending email by app

Asked

Viewed 89 times

0

I’m having trouble making a simple program to make my application send an email.

Main

public static void main(String[] args) throws EmailException, IOException {

        ConfiguracaoEmail emailConfig = new ConfiguracaoEmail(new Filial("matriz", true));
        emailConfig.setServidor("smtp.gmail.com");
        emailConfig.setRemetente("[email protected]");
        emailConfig.setTitulo("Teste");
        emailConfig.setCodificacao("utf-8");
        emailConfig.setAutenticacao("[email protected]");
        emailConfig.setSenha("senhaEmail");
        emailConfig.setPortaSMTP(465);
        emailConfig.setTLS(true);
        List<String> emails = new ArrayList<String>();
        emails.add("[email protected]");
        try {
            SendMail mail = new SendMail("Teste", " - envio email", emails, emailConfig);
            mail.start();
        } catch (EmailException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

The Configuracaoemail class is only an auxiliary class for storing configuration information.

Sendemail class

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

public class SendMail extends Thread {

    private HtmlEmail email;

    public SendMail(String subject, String message, List<String> mailTo, ConfiguracaoEmail config) throws EmailException, IOException {
        this.email = emailConfig(config);
        email.setSubject(subject);
        addTo(mailTo);
    }

    private void addTo(List<String> mailTo) throws EmailException {
        for (String mail : mailTo) {
            email.addTo(mail);
        }
    }

    public HtmlEmail getEmail() {
        return email;
    }

    public void setEmail(HtmlEmail email) {
        this.email = email;
    }

    private HtmlEmail emailConfig(ConfiguracaoEmail cfg) throws EmailException {
        HtmlEmail email = new HtmlEmail();
        email.setDebug(cfg.getDebug());
        email.setTLS(cfg.getTLS());
        email.setSSL(true);
        email.setHostName(cfg.getServidor());
        email.setFrom(cfg.getRemetente(), cfg.getTitulo());
        email.setCharset(cfg.getCodificacao());
        email.setAuthentication(cfg.getAutenticacao(), cfg.getSenha());
        email.setSmtpPort(cfg.getPortaSMTP());
        email.setSSL(false);
        return email;
    }

    @Override
    public void run() {
        try {
            email.send();
        } catch (EmailException e) {
            throw new RuntimeException(e);
        }
    }

}

Does anyone have any idea what might be going on? It does not accuse any errors but the email is not sent. (Note: The code is not quite that. I only took the part referring to the email)

  • Did the email was sent and ended up in the recipient email’s SPAM box?

  • I looked and it’s not being sent to the bin.

2 answers

1

It lacked calling the method send?

public SendMail(String subject, String message, List<String> mailTo, ConfiguracaoEmail config) throws EmailException, IOException {
        this.email = emailConfig(config);
        email.setSubject(subject);
        addTo(mailTo);
        Transport.send(email, this.email, "my-password");
    }

Documentation: https://javamail.java.net/nonav/docs/api/

0

I looked at another example code and the problem is the line that executes the following command: email.setSSL(false); in the emailConfig method. I commented on it and it worked. Thank you.

Browser other questions tagged

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