1 | package com.medias.mail; |
---|
2 | |
---|
3 | import javax.mail.*; |
---|
4 | import javax.mail.internet.*; |
---|
5 | import javax.activation.DataHandler; |
---|
6 | import javax.activation.FileDataSource; |
---|
7 | import javax.activation.DataSource; |
---|
8 | |
---|
9 | import java.util.Iterator; |
---|
10 | import java.util.Properties; |
---|
11 | import java.util.ArrayList; |
---|
12 | import java.util.regex.Matcher; |
---|
13 | import java.util.regex.Pattern; |
---|
14 | import java.io.File; |
---|
15 | |
---|
16 | /** |
---|
17 | * @author combaz |
---|
18 | * |
---|
19 | * Created on 15 mars 2005 |
---|
20 | */ |
---|
21 | public class MailFactory { |
---|
22 | |
---|
23 | private String host; |
---|
24 | |
---|
25 | |
---|
26 | |
---|
27 | /** |
---|
28 | * @param host |
---|
29 | */ |
---|
30 | public MailFactory(String host) { |
---|
31 | super(); |
---|
32 | this.host = host; |
---|
33 | } |
---|
34 | /** |
---|
35 | * @return Returns the host. |
---|
36 | */ |
---|
37 | public String getHost() { |
---|
38 | return host; |
---|
39 | } |
---|
40 | /** |
---|
41 | * @param host The host to set. |
---|
42 | */ |
---|
43 | public void setHost(String host) { |
---|
44 | this.host = host; |
---|
45 | } |
---|
46 | |
---|
47 | public void sendMails(ArrayList<Mail> mails) throws AddressException, MessagingException |
---|
48 | { |
---|
49 | Iterator<Mail> it = mails.iterator(); |
---|
50 | while(it.hasNext()) { |
---|
51 | Mail mail = it.next(); |
---|
52 | this.sendMail(mail); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * Envoie un com.medias.mail. |
---|
58 | * @param mail Le com.medias.mail à envoyer |
---|
59 | * @throws AddressException |
---|
60 | * @throws MessagingException |
---|
61 | */ |
---|
62 | public void sendMail(Mail mail) throws AddressException, MessagingException { |
---|
63 | this.sendMail(mail.getFrom(), mail.getTo(), mail.getBcc(), mail.getContent(), mail.getSubject()); |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * Envoie un com.medias.mail. |
---|
68 | * @param from |
---|
69 | * @param to |
---|
70 | * @param content |
---|
71 | * @param subject |
---|
72 | * @throws AddressException |
---|
73 | * @throws MessagingException |
---|
74 | */ |
---|
75 | public void sendMail(String from, String to, String bcc, String content, String subject) throws AddressException, MessagingException { |
---|
76 | sendMail(this.host, from, to, bcc, content, subject); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Envoie un com.medias.mail. |
---|
81 | * @param host Adresse du serveur com.medias.mail |
---|
82 | * @param from Adresse de l'expéditeur. |
---|
83 | * @param to Adresse du réceptionneur. |
---|
84 | * @param content Contenu du message. |
---|
85 | * @param subject Sujet du message. |
---|
86 | */ |
---|
87 | public static void sendMail(String host, String from, String to, String bcc, String content, String subject) throws AddressException, MessagingException { |
---|
88 | Properties props = System.getProperties(); |
---|
89 | props.put("mail.smtp.host", host); |
---|
90 | Session session = Session.getDefaultInstance(props, null); |
---|
91 | MimeMessage message = new MimeMessage(session); |
---|
92 | message.setFrom(new InternetAddress(from)); |
---|
93 | message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); |
---|
94 | if (bcc != null) |
---|
95 | message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc)); |
---|
96 | message.setSubject(subject); |
---|
97 | message.setText(content); |
---|
98 | Transport.send(message); |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * Envoie un com.medias.mail avec une piÚce jointe. |
---|
103 | * @param from Adresse de l'expéditeur. |
---|
104 | * @param to Adresse du réceptionneur. |
---|
105 | * @param content Contenu du message. |
---|
106 | * @param subject Sujet du message. |
---|
107 | * @param pj Fichier de piÚce jointe. |
---|
108 | * */ |
---|
109 | public static void sendMail(String host, String from, String to, String bcc, String content, String subject, File pj) { |
---|
110 | Properties props = System.getProperties(); |
---|
111 | props.put("mail.smtp.host", host); |
---|
112 | Session session = Session.getDefaultInstance(props, null); |
---|
113 | MimeMessage message = new MimeMessage(session); |
---|
114 | |
---|
115 | try { |
---|
116 | message.setFrom(new InternetAddress(from)); |
---|
117 | message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); |
---|
118 | message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc)); |
---|
119 | BodyPart messageBodyPart = new MimeBodyPart(); |
---|
120 | messageBodyPart.setText(content); |
---|
121 | Multipart multipart = new MimeMultipart(); |
---|
122 | multipart.addBodyPart(messageBodyPart);messageBodyPart = new MimeBodyPart(); |
---|
123 | DataSource source = new FileDataSource(pj); |
---|
124 | messageBodyPart.setDataHandler(new DataHandler(source)); |
---|
125 | messageBodyPart.setFileName(pj.getName()); |
---|
126 | multipart.addBodyPart(messageBodyPart); |
---|
127 | message.setContent(multipart); |
---|
128 | message.setSubject(subject); |
---|
129 | Transport.send(message); |
---|
130 | } catch (AddressException e) { |
---|
131 | System.out.println ("exception1_MailFactory"); |
---|
132 | e.printStackTrace(); |
---|
133 | } catch (MessagingException e) { |
---|
134 | System.out.println ("exception2_MailFactory"); |
---|
135 | e.printStackTrace(); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * Envoie un com.medias.mail avec plusieurs piÚces jointes. |
---|
141 | * @param from Adresse de l'expéditeur. |
---|
142 | * @param to Adresse du réceptionneur. |
---|
143 | * @param content Contenu du message. |
---|
144 | * @param subject Sujet du message. |
---|
145 | * @param pj Liste de piÚces jointes. |
---|
146 | */ |
---|
147 | public static void sendMail(String host, String from, String to, String bcc, String content, String subject, ArrayList<File> pj) { |
---|
148 | Properties props = System.getProperties(); |
---|
149 | props.put("mail.smtp.host", host); |
---|
150 | Session session = Session.getDefaultInstance(props, null); |
---|
151 | MimeMessage message = new MimeMessage(session); |
---|
152 | |
---|
153 | try { |
---|
154 | message.setFrom(new InternetAddress(from)); |
---|
155 | message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); |
---|
156 | message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc)); |
---|
157 | BodyPart messageBodyPart = new MimeBodyPart(); |
---|
158 | messageBodyPart.setText(content); |
---|
159 | Multipart multipart = new MimeMultipart(); |
---|
160 | multipart.addBodyPart(messageBodyPart); |
---|
161 | |
---|
162 | for(int i = 0; i < pj.size(); i++) { |
---|
163 | messageBodyPart = new MimeBodyPart(); |
---|
164 | File fic = pj.get(i); |
---|
165 | DataSource source = new FileDataSource(fic); |
---|
166 | messageBodyPart.setDataHandler(new DataHandler(source)); |
---|
167 | messageBodyPart.setFileName(fic.getName()); |
---|
168 | multipart.addBodyPart(messageBodyPart); |
---|
169 | } |
---|
170 | |
---|
171 | message.setContent(multipart); |
---|
172 | message.setSubject(subject); |
---|
173 | Transport.send(message); |
---|
174 | } catch (AddressException e) { |
---|
175 | System.out.println ("exception3_MailFactory"); |
---|
176 | e.printStackTrace(); |
---|
177 | } catch (MessagingException e) { |
---|
178 | System.out.println ("exception4_MailFactory"); |
---|
179 | e.printStackTrace(); |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * Test la validité syntaxique d'une adresse com.medias.mail. |
---|
185 | * @param adresse Adresse com.medias.mail à tester. |
---|
186 | * @return Vrai si l'adresse est valide. |
---|
187 | */ |
---|
188 | public static boolean isValid(String adresse) { |
---|
189 | String patternStr = "^[a-zA-Z0-9][a-zA-Z0-9_-]*(.[a-zA-Z0-9][a-zA-Z0-9_-]*)*@[a-zA-Z0-9]" + |
---|
190 | "[a-zA-Z0-9-]*(.[a-zA-Z0-9][a-zA-Z0-9-]*)*.[a-zA-Z]{2,4}$"; |
---|
191 | |
---|
192 | Pattern pattern = Pattern.compile(patternStr); |
---|
193 | Matcher matcher = pattern.matcher(adresse); |
---|
194 | boolean matchFound = matcher.matches(); |
---|
195 | return matchFound; |
---|
196 | } |
---|
197 | } |
---|