J'essais d'envoyer un mail en utilisant le paquet 'java.mail.*' mais malheuresement sans succes.Voici la reponse du compilateur:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.mail.yahoo.fr, port: 25, response: -1
et Voici le code que j'uitilise:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 import java.util.*; import javax.mail.*; import javax.mail.internet.*; public class MailModel { protected String from; protected String to; protected String subject; protected String text ; /** Creates a new instance of MailModel */ public MailModel(String from ,String to, String subject, String text ) { this.from = from ; this.to = to ; this.subject = subject; this.text = text; } public void send(){ try{ Properties props= System.getProperties(); props.put("mail.smtp.host", "smtp.mail.yahoo.fr"); Session s = Session.getInstance(props,null); InternetAddress from = new InternetAddress("achillle2000@yahoo.fr"); InternetAddress to = new InternetAddress(this.to); MimeMessage message = new MimeMessage(s); message.setFrom(from); message.addRecipient(Message.RecipientType.TO, to); message.setSubject(this.subject); message.setText(this.text); Transport.send(message); } catch (AddressException ea) { System.out.println(ea); }catch(MessagingException me) { System.out.println(me); } } public void setFrom(String from){ this.from = from ; } public void setTo(String to ){ this.to = to ; } public void setSubject(String subject){ this.subject = subject ; } public void setText(String text){ this.text = text ; } public static void main (String [] args){ MailModel mail = new MailModel("achillle2000@yahoo.fr", "achillle2000@yahoo.fr", "essai", "juste un essai"); mail.send() ; System.out.println("Envoi ok"); } }
Partager