salut tout le monde
j'ai un problème dans l'API JAVAmail;je veux envoyer un message avec une pièce jointe pour cela j'ai fait un petit bout de code le voici
mais ça m'affiche l'erreur suivant
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 import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.sql.DataSource; public class test { public static void main(String[] args) { final String username = "moi@gmail.com"; final String password = "********"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("moi@gmail.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("detinataire@gmail.com")); message.setSubject("salutation"); MimeBodyPart m1=new MimeBodyPart(); m1.setText("salut!"); // creation et ajout de la piece jointe MimeBodyPart m2=new MimeBodyPart(); FileDataSource source = new FileDataSource("c:/fichier.txt"); m2.setDataHandler(new DataHandler(source)); m2.setFileName(source.getName()); Multipart mp = new MimeMultipart(); mp.addBodyPart(m1); mp.addBodyPart(m2); message.setContent(mp); Transport.send(message); System.out.println("Done"); } catch (MessagingException mex) { mex.printStackTrace(); Exception ex = null; if ((ex = mex.getNextException()) != null) { ex.printStackTrace(); } } } }
je sait pas si c'est une tentative échoué à l’accès d'un serveur c'est à dire un problème de connections internet ou bien le problème réside dans le code lui même.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 javax.mail.MessagingException: Exception reading response; nested exception is: java.net.SocketException: Connection reset at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2153)
cordiialement
Partager