Bonjour,
Je suis débutant.
J’aimerais réaliser une API qui me permettrait d’envoyer plusieurs pièces jointes.
Comment insérer plusieurs méthodes dans mon prog, pour envoyer d’un seul coup, les fichiers AttachFile.txt ; AttachFile1.txt ; AttachFile2.txt.
Ci-dessous le code.
Merci d’avance pour votre aide.
--------
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 package com.as.email; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.SendFailedException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class AttachExample { public static void main (String args[]) throws Exception { //smtp de l'opérateur String host = "smtp.orange.fr"; // émail de l'expéditeur String from = "infos@assmail.fr"; // émail du destnataire String[] to = {"assmail@assmail.fr"}; //nom du fichier, Attention!! il faut indiquer le chemin ou se trouve le fichier String filename = "d:/pj/AttachFile.txt"; // Get system properties Properties props = System.getProperties(); props.put("mail.smtp.host", host); Session session = Session.getInstance(props, null); System.out.println(session.getProperties()); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); InternetAddress[] toAddress = new InternetAddress[to.length]; for (int i = 0; i < to.length; i++) toAddress[i] = new InternetAddress(to[i]); message.setRecipients(Message.RecipientType.TO, toAddress); // indiquer l'objet du mail message.setSubject("Hello JavaMail Attachment"); BodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setText("Here's the file"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); try{ Transport.send(message); } catch(SendFailedException sfe) { message.setRecipients(Message.RecipientType.TO, sfe.getValidUnsentAddresses()); Transport.send(message); } } }
Partager