Problem sending email by application

0

I'm having trouble making a simple program to get my application to 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 E-mailConfiguration class is just an auxiliary class to store the 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);
        }
    }

}

Anyone have any idea what might be happening? It does not acknowledge any error but the email is not sent. (Note: The code is not all that. I just took the part about the email)

    
asked by anonymous 09.10.2014 / 19:35

2 answers

1

Send method ?

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: link

    
09.10.2014 / 20:59
0

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

    
09.10.2014 / 21:08