很多邮件JAVA失败 - JAVAX.MAIL

时间:2015-02-03 17:40:35

标签: java email

我发送了很多带有循环的电子邮件,第一次成功发送了八封电邮,但是nexts电子邮件发送了每封电子邮件的错误,第二天我再次尝试并发出同样的问题。

javax.mail.AuthenticationFailedException
    at javax.mail.Service.connect(Service.java:319)
    at javax.mail.Service.connect(Service.java:169)
    at xx.xx.xx.xx.servicios.EmailSend.sendEmail(EmailSend.java:78)
    at xx.xx.xx.xx.temporalizador.TareasRetraso.run(TareasRetraso.java:95)
    at java.util.TimerThread.mainLoop(Timer.java:512)
    at java.util.TimerThread.run(Timer.java:462)

使用代码发送电子邮件:

import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailSend 
{
    public static void sendEmail(String to, String text) 
    {
        try 
        {
            System.out.println("enviar() -> Se asignarán los parámetros de correo");
            //log.debug("enviar() -> Se asignarán los parámetros de correo");
            Parametros param = Parametros.getParametrosInstance();
            String ruta = param.getDirectorioCorreo();
            String host = param.getHost();
            String auth = param.getAutentificacion();
            String smtpPropio = param.getSmtpPropio();

            final String cuentaUsuario = param.getCuentaUsuario();
            System.out.println("cuentaUasuario() "+cuentaUsuario);
            final String cuentaPass = param.getCuentaPass();
            String puerto = param.getPuerto();
            String ssl = param.getSsl();
            System.out.println("--------------------------> ssl "+ssl);

            Properties props = new Properties();
            props.put("mail.smtp.host", host);
            Session session = null;

            if(auth != null && auth.equals("si"))
            {
                //log.debug("utlizando autenticación propia");
                props.setProperty("mail.smtp.port", puerto);
                props.setProperty("mail.smtp.user", cuentaUsuario);
                props.setProperty("mail.smtp.auth", "true");
                if(ssl != null && ssl.equals("si")){
                    props.setProperty("mail.smtp.socketFactory.port", puerto);
                    props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                    props.setProperty("mail.smtp.socketFactory.fallback", "false");
                }
                session = Session.getDefaultInstance(props);
                /*session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(cuentaUsuario, cuentaPass);
                    }
                });*/
            }
            else
            {
                //log.debug("NO se utliza autenticación propia");
                session = Session.getDefaultInstance(props, null);
            }

            Message emailMessage = new MimeMessage(session);

            emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            //emailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
            emailMessage.setFrom(new InternetAddress(cuentaUsuario));
            //emailMessage.setSubject(subject);
            emailMessage.setContent(text, "text/html" );
            if(auth != null && auth.equals("si"))
            {
                //emailMessage.setText(text);

                //emailSession.setDebug(true);

                System.out.println("datos conexion "+host+" "+cuentaUsuario+" "+cuentaPass);
                Transport t = session.getTransport("smtp");
                System.out.println("datos conexion "+host+" "+cuentaUsuario+" "+cuentaPass);
                t.connect(host, cuentaUsuario, cuentaPass);
                t.sendMessage(emailMessage, emailMessage.getAllRecipients());
                //Transport.send(emailMessage);
            }
            else
            {
                Transport.send(emailMessage);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

如何正确发送所有电子邮件?

0 个答案:

没有答案