Probleme qu'on j'envoi email avec javamail

codeibra Messages postés 9 Date d'inscription samedi 9 avril 2011 Statut Membre Dernière intervention 5 avril 2013 - 5 avril 2013 à 14:40
codeibra Messages postés 9 Date d'inscription samedi 9 avril 2011 Statut Membre Dernière intervention 5 avril 2013 - 5 avril 2013 à 20:11
j'ai utiliser se code pour envoie un email en utilisent l'API javamail et le serveur smtp que j'ai utiliser et : smtp.live.com
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
 
public class SendMailUsingAuthentication1 {
 
    private static final String SMTP_HOST_NAME = "smtp.live.com"; 
    private static final String SMTP_AUTH_USER = "mon email";
    private static final String SMTP_AUTH_PWD = "mot de passe email";
    private static final String emailMsgTxt = "Body";
    private static final String emailSubjectTxt = "Subject";
    private static final String emailFromAddress = "mon email";
    // Add List of Email address to who email needs to be sent to
    private static final String[] emailList = {"email destination "};
 
    public static void main(String args[]) throws Exception {
        SendMailUsingAuthentication1 smtpMailSender = new SendMailUsingAuthentication1();
        smtpMailSender.postMail(emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
        System.out.println("Sucessfully Sent mail to All Users");
    }
 
    public void postMail(String recipients[], String subject,
            String message, String from) throws MessagingException, AuthenticationFailedException {
        boolean debug = false;
 
        //Set the host smtp address
        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        //props.put("mail.smtp.port", "25");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getDefaultInstance(props, auth);
 
        session.setDebug(debug);
 
        // create a message
        Message msg = new MimeMessage(session);
 
        // set the from and to address
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);
 
        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);
 
        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        Transport.send(msg);
    }
 
    private class SMTPAuthenticator extends javax.mail.Authenticator {
 
        public PasswordAuthentication getPasswordAuthentication() {
            String username = SMTP_AUTH_USER;
            String password = SMTP_AUTH_PWD;
            return new PasswordAuthentication(username, password);
        }
    }
}

et en tennent en compte que mon mot de passe et nom d'utulisateur sachant que le port 25 sont tous juste mais j'ai un message d’erreur comme suite :
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.live.com, port: 25, response: 421
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1950)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at javax.mail.Service.connect(Service.java:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at SendMailUsingAuthentication1.postMail(SendMailUsingAuthentication1.java:54)
    at SendMailUsingAuthentication1.main(SendMailUsingAuthentication1.java:19)
Java Result: 1

j'ai pas trouver jusque se moment l'erreur et j'esper que vous m'aide merci

2 réponses

Bonjour Codeibra,
il y a deux port pour smtp 25 & 587. PC utilise le port 25 par défaut --> donc 587

public void sendmail() {
        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable","true");
        props.put("mail.smtp.auth", "true");   
        props.put("mail.smtp.port", 587);
        Session session = Session.getDefaultInstance(props,null);
                    }

        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("source@gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress
                    .parse("destination@email.com"));
            message.setSubject("Subject ");
            message.setText("BODY TEXT");

            //Transport.send(message);
            
            Transport tr = session.getTransport("smtp");
            tr.connect("smtp.gmail.com", "source@gmail.com",
                "password");
            message.saveChanges();      // don't forget this
            tr.sendMessage(message, message.getAllRecipients());
            tr.close();

            System.out.println("Done");
            
            System.out.println(message.toString());

        } catch (MessagingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        props.clear();
0
codeibra Messages postés 9 Date d'inscription samedi 9 avril 2011 Statut Membre Dernière intervention 5 avril 2013
5 avril 2013 à 20:11
merci pour votre reponce donc si mieux d’utiliser le port 587 mais j'ai changer le port et elle se compile normalement et envoi les email mais si j'essaye plusierus tentative d'envoi par exemple smtp.gmail.com et j'execute pour envoi 4 a 5 ou 6 mail les probleme de :can not connect to smtp........ port 25 .... caused by time out connection et vioci le code exacte :
......nested exception is:
java.net.ConnectException: Connection timed out: connect.......

as que il ya une limite d'envoi par le serveur et comment l'enlever
merci
0
Rejoignez-nous