Bonjour,
j'ai utiliser la classe suivante pour envoyer des email:
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 import email.Mailer; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; public class MailWithPasswordAuthentication { public static void main(String[] args) throws MessagingException { new MailWithPasswordAuthentication().run(); } private void run() throws MessagingException { Message message = new MimeMessage(getSession()); message.addRecipient(RecipientType.TO, new InternetAddress("myEmail@gmail.com")); message.addFrom(new InternetAddress[] { new InternetAddress("myEmail@gmail.com") }); message.setSubject("the subject"); message.setContent("the body", "text/plain"); Transport.send(message); } private Session getSession() { Authenticator authenticator = new Authenticator(); //System.out.println("Start ... "); Properties properties = new Properties(); properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName()); properties.setProperty("mail.smtp.auth", "true"); // properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); // properties.setProperty("mail.smtp.socketFactory.fallback", "javax.net.ssl.SSLSocketFactory"); properties.setProperty("mail.smtp.starttls.enable", "false"); properties.setProperty("mail.smtp.host", "smtp.offshorevalley.net"); properties.setProperty("mail.smtp.port", "25"); return Session.getInstance(properties, authenticator); } private class Authenticator extends javax.mail.Authenticator { private PasswordAuthentication authentication; public Authenticator() { String username = "myEmail@gmail.comt"; String password = "myPassWord"; authentication = new PasswordAuthentication(username, password); } protected PasswordAuthentication getPasswordAuthentication() { return authentication; } } }
le problème est le suivant;
le code ca marche bien dans un premier temps et après quelque utilisation je rencontre l'erreur suivant :
j'ai essayer avec plusieurs code et toujours les mêmes résultat ( tout ça marche bien dans un premier temps,et en suite l'erreur
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 Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.offshorevalley.net, port: 25; nested exception is: java.net.ConnectException: Connection timed out: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638) 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 Test.MailWithPasswordAuthentication.run(MailWithPasswordAuthentication.java:31) at Test.MailWithPasswordAuthentication.main(MailWithPasswordAuthentication.java:17) Caused by: java.net.ConnectException: Connection timed out: connect at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:337) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391) at java.net.Socket.connect(Socket.java:579) at java.net.Socket.connect(Socket.java:528) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900) ... 8 more Java Result: 1![]()
ya quelqu’un qui peut me dire pourquoi cette erreur ??
Merci en avance !!
Partager