简体   繁体   中英

Trouble when i'm trying to send an email throught my aplication

I'm having some trouble with my aplication to send an email.

Main code

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

        ConfiguracaoEmail emailConfig = new ConfiguracaoEmail(new Filial("matriz", true));
        emailConfig.setServidor("smtp.gmail.com");
        emailConfig.setRemetente("emailFrom@gmail.com");
        emailConfig.setTitulo("Teste");
        emailConfig.setCodificacao("utf-8");
        emailConfig.setAutenticacao("emailFrom@gmail.com");
        emailConfig.setSenha("senhaEmail");
        emailConfig.setPortaSMTP(465);
        emailConfig.setTLS(true);
        List<String> emails = new ArrayList<String>();
        emails.add("emailTo@gmail.com");
        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 class ConfiguracaoEmail is just to auxiliate to pass the information.

Class SendEmail

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);
        }
    }

}

Someone have any idea what could possible be happening? It doesn't show any error. The email just isn't sent. (Obs: The code is not just it. I just pick the part revelant to the email)

The issue was in the emailConfig method and specifically the following line:

email.setSSL(false); 

Removing this line solved the problem in my case.

The following code may solve your problem, its worked for me

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

 public class Email {

private static String USER_NAME = "xxxxx";  // GMail user name (just the part before "@gmail.com")
private static String PASSWORD = "xxxxx"; // GMail password
private static String RECIPIENT = "xxxxx@gmail.com";

public static void main(String[] args) {
    String from = USER_NAME;
    String pass = PASSWORD;
    String[] to = { RECIPIENT }; // list of recipient email addresses
    String subject = "Java send mail example";
    String body = "Welcome to JavaMail!";

    sendFromGMail(from, pass, to, subject, body);
}

private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
    Properties props = System.getProperties();
  String host = "smtp.gmail.com";

    props.put("mail.smtp.starttls.enable", "true");

   props.put("mail.smtp.ssl.trust", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");//587
    props.put("mail.smtp.auth", "true");


    Session session = Session.getDefaultInstance(props);
    MimeMessage message = new MimeMessage(session);

    try {


        message.setFrom(new InternetAddress(from));
        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for( int i = 0; i < to.length; i++ ) {
            toAddress[i] = new InternetAddress(to[i]);
        }

        for( int i = 0; i < toAddress.length; i++) {
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }



        message.setSubject(subject);
        message.setText(body);


        Transport transport = session.getTransport("smtp");


        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();

    }
    catch (AddressException ae) {
        ae.printStackTrace();
    }
    catch (MessagingException me) {
        me.printStackTrace();
    }
 }
}

if its not working ,check your jar files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM